|
| 1 | +import { Endpoint } from '@data-client/endpoint'; |
| 2 | +import { NetworkError } from '@data-client/rest'; |
| 3 | +import { renderHook, act } from '@data-client/test'; |
| 4 | +import { render, waitFor } from '@testing-library/react'; |
| 5 | +import React, { ReactElement, StrictMode, useTransition } from 'react'; |
| 6 | + |
| 7 | +import AsyncBoundary from '../../components/AsyncBoundary'; |
| 8 | +import DataProvider from '../../components/DataProvider'; |
| 9 | +import { useSuspense } from '../../hooks'; |
| 10 | +import useDebounce from '../useDebounce'; |
| 11 | + |
| 12 | +describe('useDebounce()', () => { |
| 13 | + beforeAll(() => { |
| 14 | + jest.useFakeTimers(); |
| 15 | + }); |
| 16 | + afterAll(() => { |
| 17 | + jest.useRealTimers(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should not update until delay has passed', () => { |
| 21 | + const { result, rerender } = renderHook( |
| 22 | + ({ value }: { value: string }) => { |
| 23 | + return useDebounce(value, 100); |
| 24 | + }, |
| 25 | + { initialProps: { value: 'initial' } }, |
| 26 | + ); |
| 27 | + expect(result.current).toEqual(['initial', false]); |
| 28 | + jest.advanceTimersByTime(10); |
| 29 | + rerender({ value: 'next' }); |
| 30 | + rerender({ value: 'third' }); |
| 31 | + expect(result.current).toEqual(['initial', false]); |
| 32 | + act(() => { |
| 33 | + jest.advanceTimersByTime(100); |
| 34 | + }); |
| 35 | + expect(result.current).toEqual(['third', false]); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should never update when updatable is false', () => { |
| 39 | + const { result, rerender } = renderHook( |
| 40 | + ({ value, updatable }: { value: string; updatable: boolean }) => { |
| 41 | + return useDebounce(value, 100, updatable); |
| 42 | + }, |
| 43 | + { initialProps: { value: 'initial', updatable: false } }, |
| 44 | + ); |
| 45 | + expect(result.current).toEqual(['initial', false]); |
| 46 | + jest.advanceTimersByTime(10); |
| 47 | + rerender({ value: 'next', updatable: false }); |
| 48 | + act(() => { |
| 49 | + jest.advanceTimersByTime(100); |
| 50 | + }); |
| 51 | + expect(result.current).toEqual(['initial', false]); |
| 52 | + rerender({ value: 'third', updatable: true }); |
| 53 | + expect(result.current).toEqual(['initial', false]); |
| 54 | + jest.advanceTimersByTime(10); |
| 55 | + expect(result.current).toEqual(['initial', false]); |
| 56 | + act(() => { |
| 57 | + jest.advanceTimersByTime(100); |
| 58 | + }); |
| 59 | + expect(result.current).toEqual(['third', false]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should be pending while async operation is performed based on debounced value', async () => { |
| 63 | + const issueQuery = new Endpoint( |
| 64 | + ({ q }: { q: string }) => Promise.resolve({ q, text: 'hi' }), |
| 65 | + { name: 'issueQuery' }, |
| 66 | + ); |
| 67 | + function IssueList({ q }: { q: string }) { |
| 68 | + const response = useSuspense(issueQuery, { q }); |
| 69 | + return <div>{response.q}</div>; |
| 70 | + } |
| 71 | + function Search({ query }: { query: string }) { |
| 72 | + const [debouncedQuery, isPending] = useDebounce(query, 100); |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {isPending ? |
| 76 | + <span>loading</span> |
| 77 | + : null} |
| 78 | + <AsyncBoundary fallback={<div>searching...</div>}> |
| 79 | + <IssueList q={debouncedQuery} /> |
| 80 | + </AsyncBoundary> |
| 81 | + </div> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const tree = ( |
| 86 | + <DataProvider> |
| 87 | + <Search query="initial" /> |
| 88 | + </DataProvider> |
| 89 | + ); |
| 90 | + const { queryByText, rerender, getByText } = render(tree); |
| 91 | + expect(queryByText(/loading/i)).toBeNull(); |
| 92 | + expect(getByText(/searching/i)).not.toBeNull(); |
| 93 | + |
| 94 | + await waitFor(() => expect(queryByText(/searching/i)).toBeNull()); |
| 95 | + expect(getByText(/initial/i)).not.toBeNull(); |
| 96 | + rerender( |
| 97 | + <DataProvider> |
| 98 | + <Search query="second" /> |
| 99 | + </DataProvider>, |
| 100 | + ); |
| 101 | + rerender( |
| 102 | + <DataProvider> |
| 103 | + <Search query="third" /> |
| 104 | + </DataProvider>, |
| 105 | + ); |
| 106 | + act(() => { |
| 107 | + jest.advanceTimersByTime(100); |
| 108 | + }); |
| 109 | + // only check in react 18 |
| 110 | + if ('useTransition' in React) { |
| 111 | + // isPending |
| 112 | + expect(getByText(/loading/i)).not.toBeNull(); |
| 113 | + } |
| 114 | + // keep showing previous values |
| 115 | + expect(getByText(/initial/i)).not.toBeNull(); |
| 116 | + // only check in react 18 |
| 117 | + if ('useTransition' in React) { |
| 118 | + expect(queryByText(/searching/i)).toBeNull(); |
| 119 | + } |
| 120 | + |
| 121 | + await waitFor(() => expect(queryByText(/loading/i)).toBeNull()); |
| 122 | + expect(getByText(/third/i)).not.toBeNull(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments