|
| 1 | +import { act } from '@testing-library/react' |
| 2 | +import { renderHook } from '@testing-library/react-hooks' |
| 3 | +import React from 'react' |
| 4 | +import DataLoaderProvider from '../DataLoaderProvider' |
| 5 | +import { KEY_IS_NOT_STRING_ERROR } from '../constants' |
| 6 | +import { |
| 7 | + UsePaginatedDataLoaderConfig, |
| 8 | + UsePaginatedDataLoaderMethodParams, |
| 9 | + UsePaginatedDataLoaderResult, |
| 10 | +} from '../types' |
| 11 | +import usePaginatedDataLoader from '../usePaginatedDataLoader' |
| 12 | + |
| 13 | +type UseDataLoaderHookProps = { |
| 14 | + config: UsePaginatedDataLoaderConfig<unknown> |
| 15 | + key: string |
| 16 | + method: (params: UsePaginatedDataLoaderMethodParams) => Promise<unknown> |
| 17 | +} |
| 18 | + |
| 19 | +const PROMISE_TIMEOUT = 5 |
| 20 | + |
| 21 | +const initialProps = { |
| 22 | + config: { |
| 23 | + enabled: true, |
| 24 | + }, |
| 25 | + key: 'test', |
| 26 | + method: jest.fn( |
| 27 | + ({ page, perPage }: UsePaginatedDataLoaderMethodParams) => |
| 28 | + new Promise(resolve => { |
| 29 | + setTimeout(() => resolve(`${page}-${perPage}`), PROMISE_TIMEOUT) |
| 30 | + }), |
| 31 | + ), |
| 32 | +} |
| 33 | +// eslint-disable-next-line react/prop-types |
| 34 | +const wrapper = ({ children }: { children?: React.ReactNode }) => ( |
| 35 | + <DataLoaderProvider>{children}</DataLoaderProvider> |
| 36 | +) |
| 37 | + |
| 38 | +describe('useDataLoader', () => { |
| 39 | + test('should render correctly without options', async () => { |
| 40 | + const { result, waitForNextUpdate } = renderHook< |
| 41 | + UseDataLoaderHookProps, |
| 42 | + UsePaginatedDataLoaderResult |
| 43 | + >(props => usePaginatedDataLoader(props.key, props.method), { |
| 44 | + initialProps, |
| 45 | + wrapper, |
| 46 | + }) |
| 47 | + expect(result.current.data).toStrictEqual({}) |
| 48 | + expect(result.current.pageData).toBe(undefined) |
| 49 | + expect(result.current.isLoading).toBe(true) |
| 50 | + await waitForNextUpdate() |
| 51 | + expect(initialProps.method).toBeCalledTimes(1) |
| 52 | + expect(result.current.isLoading).toBe(false) |
| 53 | + expect(result.current.isSuccess).toBe(true) |
| 54 | + expect(result.current.data).toStrictEqual({ 1: '1-1' }) |
| 55 | + expect(result.current.pageData).toBe('1-1') |
| 56 | + }) |
| 57 | + |
| 58 | + test('should render correctly without request enabled then enable it', async () => { |
| 59 | + const method = jest.fn( |
| 60 | + () => |
| 61 | + new Promise(resolve => { |
| 62 | + setTimeout(() => resolve(true), PROMISE_TIMEOUT) |
| 63 | + }), |
| 64 | + ) |
| 65 | + let enabled = false |
| 66 | + const { rerender, result, waitForNextUpdate } = renderHook< |
| 67 | + UseDataLoaderHookProps, |
| 68 | + UsePaginatedDataLoaderResult |
| 69 | + >( |
| 70 | + props => |
| 71 | + usePaginatedDataLoader(props.key, props.method, { |
| 72 | + enabled, |
| 73 | + }), |
| 74 | + { |
| 75 | + initialProps: { |
| 76 | + ...initialProps, |
| 77 | + key: 'test-not-enabled-then-reload', |
| 78 | + method, |
| 79 | + }, |
| 80 | + wrapper, |
| 81 | + }, |
| 82 | + ) |
| 83 | + expect(result.current.pageData).toBe(undefined) |
| 84 | + expect(result.current.isLoading).toBe(false) |
| 85 | + expect(method).toBeCalledTimes(0) |
| 86 | + enabled = true |
| 87 | + rerender() |
| 88 | + expect(method).toBeCalledTimes(1) |
| 89 | + expect(result.current.pageData).toBe(undefined) |
| 90 | + expect(result.current.isLoading).toBe(true) |
| 91 | + await waitForNextUpdate() |
| 92 | + expect(result.current.isLoading).toBe(false) |
| 93 | + expect(result.current.isSuccess).toBe(true) |
| 94 | + expect(result.current.pageData).toBe(true) |
| 95 | + }) |
| 96 | + |
| 97 | + test('should render correctly without valid key', () => { |
| 98 | + const { result } = renderHook< |
| 99 | + UseDataLoaderHookProps, |
| 100 | + UsePaginatedDataLoaderResult |
| 101 | + >(props => usePaginatedDataLoader(props.key, props.method), { |
| 102 | + initialProps: { |
| 103 | + ...initialProps, |
| 104 | + // @ts-expect-error used because we test with bad key |
| 105 | + key: 2, |
| 106 | + }, |
| 107 | + wrapper, |
| 108 | + }) |
| 109 | + expect(result.error?.message).toBe(KEY_IS_NOT_STRING_ERROR) |
| 110 | + }) |
| 111 | + |
| 112 | + test('should render correctly with result null', async () => { |
| 113 | + const { result, waitForNextUpdate } = renderHook< |
| 114 | + UseDataLoaderHookProps, |
| 115 | + UsePaginatedDataLoaderResult |
| 116 | + >(props => usePaginatedDataLoader(props.key, props.method, props.config), { |
| 117 | + initialProps: { |
| 118 | + ...initialProps, |
| 119 | + key: 'test-3', |
| 120 | + method: () => |
| 121 | + new Promise(resolve => { |
| 122 | + setTimeout(() => resolve(null), PROMISE_TIMEOUT) |
| 123 | + }), |
| 124 | + }, |
| 125 | + wrapper, |
| 126 | + }) |
| 127 | + expect(result.current.pageData).toBe(undefined) |
| 128 | + expect(result.current.isLoading).toBe(true) |
| 129 | + await waitForNextUpdate() |
| 130 | + expect(result.current.pageData).toBe(undefined) |
| 131 | + expect(result.current.isSuccess).toBe(true) |
| 132 | + expect(result.current.isLoading).toBe(false) |
| 133 | + }) |
| 134 | + |
| 135 | + test('should render correctly then change page', async () => { |
| 136 | + const { result, waitForNextUpdate } = renderHook< |
| 137 | + UseDataLoaderHookProps, |
| 138 | + UsePaginatedDataLoaderResult |
| 139 | + >(props => usePaginatedDataLoader(props.key, props.method), { |
| 140 | + initialProps: { |
| 141 | + ...initialProps, |
| 142 | + key: 'test-4', |
| 143 | + }, |
| 144 | + wrapper, |
| 145 | + }) |
| 146 | + expect(result.current.data).toStrictEqual({}) |
| 147 | + expect(result.current.pageData).toBe(undefined) |
| 148 | + expect(result.current.isLoading).toBe(true) |
| 149 | + await waitForNextUpdate() |
| 150 | + expect(result.current.isLoading).toBe(false) |
| 151 | + expect(result.current.isSuccess).toBe(true) |
| 152 | + expect(result.current.data).toStrictEqual({ 1: '1-1' }) |
| 153 | + expect(result.current.pageData).toBe('1-1') |
| 154 | + |
| 155 | + act(() => { |
| 156 | + result.current.goToNextPage() |
| 157 | + }) |
| 158 | + expect(result.current.page).toBe(2) |
| 159 | + expect(result.current.pageData).toBe(undefined) |
| 160 | + expect(result.current.isLoading).toBe(true) |
| 161 | + await waitForNextUpdate() |
| 162 | + expect(result.current.isLoading).toBe(false) |
| 163 | + expect(result.current.isSuccess).toBe(true) |
| 164 | + expect(result.current.data).toStrictEqual({ 1: '1-1', 2: '2-1' }) |
| 165 | + expect(result.current.pageData).toBe('2-1') |
| 166 | + act(() => { |
| 167 | + result.current.goToPreviousPage() |
| 168 | + result.current.goToPreviousPage() |
| 169 | + result.current.goToPreviousPage() |
| 170 | + result.current.goToPreviousPage() |
| 171 | + }) |
| 172 | + expect(result.current.page).toBe(1) |
| 173 | + expect(result.current.pageData).toBe('1-1') |
| 174 | + act(() => { |
| 175 | + result.current.goToPage(2) |
| 176 | + result.current.goToPage(-21) |
| 177 | + result.current.goToPage(0) |
| 178 | + }) |
| 179 | + expect(result.current.page).toBe(1) |
| 180 | + expect(result.current.pageData).toBe('1-1') |
| 181 | + }) |
| 182 | + |
| 183 | + test('should render correctly go to next page, change key and should be on page 1', async () => { |
| 184 | + const hookProps = { |
| 185 | + ...initialProps, |
| 186 | + key: 'test-5', |
| 187 | + } |
| 188 | + const { rerender, result, waitForNextUpdate } = renderHook< |
| 189 | + UseDataLoaderHookProps, |
| 190 | + UsePaginatedDataLoaderResult |
| 191 | + >(props => usePaginatedDataLoader(props.key, props.method), { |
| 192 | + initialProps: hookProps, |
| 193 | + wrapper, |
| 194 | + }) |
| 195 | + await waitForNextUpdate() |
| 196 | + act(() => { |
| 197 | + result.current.goToNextPage() |
| 198 | + }) |
| 199 | + await waitForNextUpdate() |
| 200 | + expect(result.current.data).toStrictEqual({ 1: '1-1', 2: '2-1' }) |
| 201 | + hookProps.key = 'test-5-bis' |
| 202 | + rerender() |
| 203 | + expect(result.current.isLoading).toBe(true) |
| 204 | + expect(result.current.pageData).toBe(undefined) |
| 205 | + expect(result.current.data).toStrictEqual({}) |
| 206 | + await waitForNextUpdate() |
| 207 | + expect(result.current.data).toStrictEqual({ 1: '1-1' }) |
| 208 | + expect(result.current.pageData).toBe('1-1') |
| 209 | + expect(result.current.isSuccess).toBe(true) |
| 210 | + expect(result.current.isLoading).toBe(false) |
| 211 | + }) |
| 212 | +}) |
0 commit comments