|
2 | 2 | import { renderHook, waitFor } from '@testing-library/react' |
3 | 3 | import { ReactNode } from 'react' |
4 | 4 | import DataLoaderProvider, { useDataLoaderContext } from '../DataLoaderProvider' |
5 | | -import { KEY_IS_NOT_STRING_ERROR } from '../constants' |
6 | | -import { UseDataLoaderConfig } from '../types' |
| 5 | +import { KeyType, UseDataLoaderConfig } from '../types' |
7 | 6 | import useDataLoader from '../useDataLoader' |
8 | 7 |
|
9 | 8 | type UseDataLoaderHookProps = { |
10 | 9 | config: UseDataLoaderConfig<unknown, unknown> |
11 | | - key: string |
| 10 | + key: KeyType |
12 | 11 | method: () => Promise<unknown> |
13 | 12 | children?: ReactNode |
14 | 13 | } |
@@ -61,6 +60,52 @@ describe('useDataLoader', () => { |
61 | 60 | expect(result.current.previousData).toBe(undefined) |
62 | 61 | }) |
63 | 62 |
|
| 63 | + test('should render correctly with a complexe key', async () => { |
| 64 | + const key = [ |
| 65 | + 'baseKey', |
| 66 | + ['null', null], |
| 67 | + ['boolean', false], |
| 68 | + ['number', 10], |
| 69 | + ].flat() |
| 70 | + |
| 71 | + const method = jest.fn( |
| 72 | + () => |
| 73 | + new Promise(resolve => { |
| 74 | + setTimeout(() => resolve(true), PROMISE_TIMEOUT) |
| 75 | + }), |
| 76 | + ) |
| 77 | + |
| 78 | + const initProps = { |
| 79 | + ...initialProps, |
| 80 | + key, |
| 81 | + method, |
| 82 | + } |
| 83 | + |
| 84 | + const { result, rerender } = renderHook( |
| 85 | + props => useDataLoader(props.key, props.method), |
| 86 | + { |
| 87 | + initialProps: initProps, |
| 88 | + wrapper, |
| 89 | + }, |
| 90 | + ) |
| 91 | + expect(result.current.data).toBe(undefined) |
| 92 | + expect(result.current.isLoading).toBe(true) |
| 93 | + expect(result.current.previousData).toBe(undefined) |
| 94 | + expect(initialProps.method).toBeCalledTimes(1) |
| 95 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)) |
| 96 | + expect(initialProps.method).toBeCalledTimes(1) |
| 97 | + expect(result.current.data).toBe(true) |
| 98 | + expect(result.current.isLoading).toBe(false) |
| 99 | + expect(result.current.previousData).toBe(undefined) |
| 100 | + |
| 101 | + rerender({ ...initProps }) |
| 102 | + |
| 103 | + expect(initialProps.method).toBeCalledTimes(1) |
| 104 | + expect(result.current.data).toBe(true) |
| 105 | + expect(result.current.isLoading).toBe(false) |
| 106 | + expect(result.current.previousData).toBe(undefined) |
| 107 | + }) |
| 108 | + |
64 | 109 | test('should render correctly without request enabled then enable it', async () => { |
65 | 110 | const method = jest.fn( |
66 | 111 | () => |
@@ -96,28 +141,6 @@ describe('useDataLoader', () => { |
96 | 141 | expect(result.current.data).toBe(true) |
97 | 142 | }) |
98 | 143 |
|
99 | | - test('should render correctly without valid key', () => { |
100 | | - const orignalConsoleError = console.error |
101 | | - console.error = jest.fn |
102 | | - try { |
103 | | - renderHook( |
104 | | - // @ts-expect-error used because we test with bad key |
105 | | - props => useDataLoader(props.key, props.method), |
106 | | - { |
107 | | - initialProps: { |
108 | | - ...initialProps, |
109 | | - key: 2, |
110 | | - }, |
111 | | - wrapper, |
112 | | - }, |
113 | | - ) |
114 | | - fail('It shoulded fail with a bad key') |
115 | | - } catch (error) { |
116 | | - expect((error as Error)?.message).toBe(KEY_IS_NOT_STRING_ERROR) |
117 | | - } |
118 | | - console.error = orignalConsoleError |
119 | | - }) |
120 | | - |
121 | 144 | test('should render correctly without keepPreviousData', async () => { |
122 | 145 | const { result } = renderHook( |
123 | 146 | props => useDataLoader(props.key, props.method, props.config), |
|
0 commit comments