|
| 1 | +import React, { Suspense } from 'react'; |
| 2 | + |
| 3 | +import { renderHook, waitFor } from '@/test-utils/rtl'; |
| 4 | + |
| 5 | +import { type UseSuspenseConfigValueResult } from '@/hooks/use-config-value/use-config-value.types'; |
| 6 | +import useSuspenseConfigValue from '@/hooks/use-config-value/use-suspense-config-value'; |
| 7 | +import usePageQueryParams from '@/hooks/use-page-query-params/use-page-query-params'; |
| 8 | + |
| 9 | +import useArchivalInputType from '../use-archival-input-type'; |
| 10 | + |
| 11 | +jest.mock('@/hooks/use-page-query-params/use-page-query-params'); |
| 12 | +jest.mock('@/hooks/use-config-value/use-suspense-config-value'); |
| 13 | + |
| 14 | +const mockUsePageQueryParams = usePageQueryParams as jest.MockedFunction<any>; |
| 15 | +const mockUseSuspenseConfigValue = |
| 16 | + useSuspenseConfigValue as jest.MockedFunction<any>; |
| 17 | + |
| 18 | +describe(useArchivalInputType.name, () => { |
| 19 | + it('should return forceQueryInputOnly as false when archival default search is enabled', async () => { |
| 20 | + const { result } = setup({ archivalDefaultSearchEnabled: true }); |
| 21 | + |
| 22 | + await waitFor(() => { |
| 23 | + expect(result.current.forceQueryInputOnly).toBe(false); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return forceQueryInputOnly as true when archival default search is disabled', async () => { |
| 28 | + const { result } = setup({ archivalDefaultSearchEnabled: false }); |
| 29 | + |
| 30 | + await waitFor(() => { |
| 31 | + expect(result.current.forceQueryInputOnly).toBe(true); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return inputType from queryParams.inputTypeArchival when archival default search is enabled', async () => { |
| 36 | + const { result } = setup({ |
| 37 | + archivalDefaultSearchEnabled: true, |
| 38 | + inputTypeArchivalQueryParamValue: 'search', |
| 39 | + }); |
| 40 | + |
| 41 | + await waitFor(() => { |
| 42 | + expect(result.current.inputType).toBe('search'); |
| 43 | + }); |
| 44 | + |
| 45 | + const { result: result2 } = setup({ |
| 46 | + archivalDefaultSearchEnabled: true, |
| 47 | + inputTypeArchivalQueryParamValue: 'query', |
| 48 | + }); |
| 49 | + |
| 50 | + await waitFor(() => { |
| 51 | + expect(result2.current.inputType).toBe('query'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should always return inputType as query when archival default search is disabled', async () => { |
| 56 | + const { result } = setup({ |
| 57 | + archivalDefaultSearchEnabled: false, |
| 58 | + inputTypeArchivalQueryParamValue: 'search', |
| 59 | + }); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(result.current.inputType).toBe('query'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should ignore queryParams.inputTypeArchivalQueryParamValue when archival default search is disabled', async () => { |
| 67 | + const { result } = setup({ |
| 68 | + archivalDefaultSearchEnabled: false, |
| 69 | + inputTypeArchivalQueryParamValue: 'query', |
| 70 | + }); |
| 71 | + |
| 72 | + await waitFor(() => { |
| 73 | + expect(result.current.inputType).toBe('query'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return an object with forceQueryInputOnly and inputType properties', async () => { |
| 78 | + const { result } = setup({ |
| 79 | + archivalDefaultSearchEnabled: true, |
| 80 | + inputTypeArchivalQueryParamValue: 'search', |
| 81 | + }); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(result.current).toHaveProperty('forceQueryInputOnly'); |
| 85 | + expect(result.current).toHaveProperty('inputType'); |
| 86 | + expect(typeof result.current.forceQueryInputOnly).toBe('boolean'); |
| 87 | + expect(['search', 'query']).toContain(result.current.inputType); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +function setup({ |
| 93 | + archivalDefaultSearchEnabled = true, |
| 94 | + inputTypeArchivalQueryParamValue = 'search', |
| 95 | +}: { |
| 96 | + archivalDefaultSearchEnabled?: boolean; |
| 97 | + inputTypeArchivalQueryParamValue?: 'search' | 'query'; |
| 98 | +}) { |
| 99 | + mockUsePageQueryParams.mockReturnValue([ |
| 100 | + { |
| 101 | + inputTypeArchival: inputTypeArchivalQueryParamValue, |
| 102 | + }, |
| 103 | + jest.fn(), |
| 104 | + ]); |
| 105 | + |
| 106 | + mockUseSuspenseConfigValue.mockReturnValue({ |
| 107 | + data: archivalDefaultSearchEnabled, |
| 108 | + } satisfies Pick< |
| 109 | + UseSuspenseConfigValueResult<'ARCHIVAL_DEFAULT_SEARCH_ENABLED'>, |
| 110 | + 'data' |
| 111 | + >); |
| 112 | + |
| 113 | + const { result } = renderHook(() => useArchivalInputType(), undefined, { |
| 114 | + wrapper: ({ children }: { children: React.ReactNode }) => ( |
| 115 | + <Suspense>{children}</Suspense> |
| 116 | + ), |
| 117 | + }); |
| 118 | + |
| 119 | + return { result }; |
| 120 | +} |
0 commit comments