|
| 1 | +import * as React from 'react'; |
| 2 | +import { FetchMock } from 'jest-fetch-mock'; |
| 3 | +import { render, wait } from '@testing-library/react'; |
| 4 | +import { createDataClient, DataProvider } from '../../common'; |
| 5 | +import useData from '../useData'; |
| 6 | + |
| 7 | +const fetchMock = fetch as FetchMock; |
| 8 | + |
| 9 | +describe('useData hook tests', () => { |
| 10 | + const onError = (e: Event) => { |
| 11 | + e.preventDefault(); |
| 12 | + }; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // to suppress error logs from error boundaries |
| 16 | + // https://github.com/facebook/react/issues/11098#issuecomment-412682721 |
| 17 | + window.addEventListener('error', onError); |
| 18 | + fetchMock.resetMocks(); |
| 19 | + jest.useFakeTimers(); |
| 20 | + }); |
| 21 | + afterEach(() => { |
| 22 | + window.removeEventListener('error', onError); |
| 23 | + jest.useRealTimers(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should fetch data correctly', async () => { |
| 27 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 28 | + |
| 29 | + const client = createDataClient(); |
| 30 | + |
| 31 | + const Comp = () => { |
| 32 | + const { data, loading } = useData( |
| 33 | + 'http://localhost/somewhere', |
| 34 | + {}, |
| 35 | + {}, |
| 36 | + { skip: false, ssr: true }, |
| 37 | + ); |
| 38 | + |
| 39 | + return loading || !data ? ( |
| 40 | + <div>loading...</div> |
| 41 | + ) : ( |
| 42 | + <div>{data.message}</div> |
| 43 | + ); |
| 44 | + }; |
| 45 | + const App = ( |
| 46 | + <DataProvider client={client}> |
| 47 | + <Comp /> |
| 48 | + </DataProvider> |
| 49 | + ); |
| 50 | + |
| 51 | + const { findByText } = render(App); |
| 52 | + |
| 53 | + expect(await findByText('loading...')).toBeDefined(); |
| 54 | + |
| 55 | + await wait(); |
| 56 | + |
| 57 | + expect(await findByText('Hello world!')).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should fetch data correctly; fetchPolicy: "network-only"', async () => { |
| 61 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 62 | + |
| 63 | + const client = createDataClient(); |
| 64 | + |
| 65 | + const Comp = () => { |
| 66 | + const { data, loading } = useData( |
| 67 | + 'http://localhost/somewhere', |
| 68 | + {}, |
| 69 | + {}, |
| 70 | + { skip: false, ssr: true, fetchPolicy: 'network-only' }, |
| 71 | + ); |
| 72 | + |
| 73 | + return loading || !data ? ( |
| 74 | + <div>loading...</div> |
| 75 | + ) : ( |
| 76 | + <div>{data.message}</div> |
| 77 | + ); |
| 78 | + }; |
| 79 | + const App = ( |
| 80 | + <DataProvider client={client}> |
| 81 | + <Comp /> |
| 82 | + </DataProvider> |
| 83 | + ); |
| 84 | + |
| 85 | + const { findByText } = render(App); |
| 86 | + |
| 87 | + expect(await findByText('loading...')).toBeDefined(); |
| 88 | + |
| 89 | + await wait(); |
| 90 | + |
| 91 | + expect(await findByText('Hello world!')).toBeDefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw an error when not wrapped with DataProvider', async () => { |
| 95 | + fetchMock.mockResponse(JSON.stringify({ message: 'Hello world!' })); |
| 96 | + |
| 97 | + class ErrorBoundary extends React.Component { |
| 98 | + state = { error: false, errorObject: null }; |
| 99 | + |
| 100 | + static getDerivedStateFromError(error: Error) { |
| 101 | + return { error: true, errorObject: error }; |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + return !this.state.error ? ( |
| 106 | + this.props.children |
| 107 | + ) : ( |
| 108 | + <div>Error happened!</div> |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const Comp = () => { |
| 114 | + const { data, loading } = useData('http://localhost/somewhere'); |
| 115 | + |
| 116 | + return loading || !data ? ( |
| 117 | + <div>loading...</div> |
| 118 | + ) : ( |
| 119 | + <div>{data.message}</div> |
| 120 | + ); |
| 121 | + }; |
| 122 | + const App = ( |
| 123 | + <ErrorBoundary> |
| 124 | + <Comp /> |
| 125 | + </ErrorBoundary> |
| 126 | + ); |
| 127 | + |
| 128 | + const { findByText } = render(App); |
| 129 | + |
| 130 | + expect(await findByText('Error happened!')).toBeDefined(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments