Open History to see the last commit
1import React, { useState } from 'react'
2import {
3 act,
4 fireEvent,
5 render,
6 waitFor,
7 cleanup,
8} from '@testing-library/react'
9import {
10 ReactQueryConfigProvider,
11 useQuery,
12 queryCache,
13 ReactQueryCacheProvider,
14} from '../index'
15β
16import { sleep } from './utils'
17β
18describe('config', () => {
19 afterEach(() => {
20 queryCache.clear()
21 })
22β
23 // See https://github.com/tannerlinsley/react-query/issues/105
24 it('should allow overriding the config with ReactQueryConfigProvider', async () => {
25 const onSuccess = jest.fn()
26β
27 const config = {
28 onSuccess,
29 }
30β
31 function Page() {
32 const { status } = useQuery('test', async () => {
33 await sleep(10)
34 return 'data'
35 })
36β
37 return (
38 <div>
39 <h1>Status: {status}</h1>
40 </div>
41 )
42 }
43β
44 const rendered = render(
45 <ReactQueryConfigProvider config={config}>
46 <ReactQueryCacheProvider>
47 <Page />
48 </ReactQueryCacheProvider>
49 </ReactQueryConfigProvider>
50 )
51β
52 await waitFor(() => rendered.getByText('Status: success'))
53β
54 expect(onSuccess).toHaveBeenCalledWith('data')
55 })
56β
57 it('should reset to defaults when all providers are unmounted', async () => {
58 const onSuccess = jest.fn()
59β
60 const config = {
61 refetchAllOnWindowFocus: false,
62 refetchOnMount: false,
63 retry: false,
64 manual: true,
65 }
66β
67 const queryFn = async () => {
68 await sleep(10)
69 return 'data'
70 }
71β
72 function Page() {
73 const { data } = useQuery('test', queryFn)
74β
75 return (
76 <div>
77 <h1>Data: {data || 'none'}</h1>
78 </div>
79 )
80 }
81β
82 const rendered = render(
83 <ReactQueryConfigProvider config={config}>
84 <Page />
85 </ReactQueryConfigProvider>
86 )
87β
88 await rendered.findByText('Data: none')
89β
90 act(() => {
91 queryCache.prefetchQuery('test', queryFn, { force: true })
92 })
93β
94 await rendered.findByText('Data: data')
95β
96 // tear down and unmount
97 cleanup()
98β
99 // wipe query cache/stored config
100 await act(() => queryCache.clear())
101 onSuccess.mockClear()
102β
103 // rerender WITHOUT config provider,
104 // so we are NOT passing the config above (refetchOnMount should be `true` by default)
105 const rerendered = render(<Page />)
106β
107 await rerendered.findByText('Data: data')
108 })
109β
110 it('should reset to previous config when nested provider is unmounted', async () => {
111 let counterRef = 0
112 const parentOnSuccess = jest.fn()
113β
114 const parentConfig = {
115 refetchOnMount: false,
116 onSuccess: parentOnSuccess,
117 }
118β
119 const childConfig = {
120 refetchOnMount: true,
121β
122 // Override onSuccess of parent, making it a no-op
123 onSuccess: undefined,
124 }
125β
126 const queryFn = async () => {
127 await sleep(10)
128 counterRef += 1
129 return String(counterRef)
130 }
131β
132 function Component() {
133 const { data, refetch } = useQuery('test', queryFn)
134β
135 return (
136 <div>
137 <h1>Data: {data}</h1>
138 <button data-testid="refetch" onClick={() => refetch()}>
139 Refetch
140 </button>
141 </div>
142 )
143 }
144β
145 function Page() {
146 const [childConfigEnabled, setChildConfigEnabled] = useState(true)
147β
148 return (
149 <div>
150 {childConfigEnabled && (
151 <ReactQueryConfigProvider config={childConfig}>
152 <Component />
153 </ReactQueryConfigProvider>
154 )}
155 {!childConfigEnabled && <Component />}
156 <button
157 data-testid="disableChildConfig"
158 onClick={() => setChildConfigEnabled(false)}
159 >
160 Disable Child Config
161 </button>
162 </div>
163 )
164 }
165β
166 const rendered = render(
167 <ReactQueryConfigProvider config={parentConfig}>
168 <ReactQueryCacheProvider>
169 <Page />
170 </ReactQueryCacheProvider>
171 </ReactQueryConfigProvider>
172 )
173β
174 await rendered.findByText('Data: 1')
175β
176 expect(parentOnSuccess).not.toHaveBeenCalled()
177β
178 fireEvent.click(rendered.getByTestId('refetch'))
179β
180 await rendered.findByText('Data: 2')
181β
182 expect(parentOnSuccess).not.toHaveBeenCalled()
183β
184 parentOnSuccess.mockReset()
185β
186 fireEvent.click(rendered.getByTestId('disableChildConfig'))
187β
188 await rendered.findByText('Data: 2')
189β
190 // it should not refetch on mount
191 expect(parentOnSuccess).not.toHaveBeenCalled()
192β
193 fireEvent.click(rendered.getByTestId('refetch'))
194β
195 await rendered.findByText('Data: 3')
196β
197 expect(parentOnSuccess).toHaveBeenCalledTimes(1)
198 })
199})