|
1 | 1 | import { configureStore } from '@reduxjs/toolkit' |
2 | 2 | import { createApi } from '@reduxjs/toolkit/query/react' |
3 | | - |
4 | | -import { renderHook } from '@testing-library/react-hooks' |
| 3 | +import { renderHook, waitFor } from '@testing-library/react' |
5 | 4 | import type { BaseQueryApi } from '../baseQueryTypes' |
6 | 5 | import { withProvider } from './helpers' |
7 | 6 |
|
@@ -91,117 +90,112 @@ describe('re-triggering behavior on arg change', () => { |
91 | 90 | beforeEach(() => void spy.mockClear()) |
92 | 91 |
|
93 | 92 | test('re-trigger on literal value change', async () => { |
94 | | - const { result, rerender, waitForNextUpdate } = renderHook( |
| 93 | + const { result, rerender } = renderHook( |
95 | 94 | (props) => getUser.useQuery(props), |
96 | 95 | { |
97 | 96 | wrapper: withProvider(store), |
98 | 97 | initialProps: 5, |
99 | 98 | } |
100 | 99 | ) |
101 | 100 |
|
102 | | - while (result.current.status === 'pending') { |
103 | | - await waitForNextUpdate() |
104 | | - } |
| 101 | + await waitFor(() => { |
| 102 | + expect(result.current.status).not.toBe('pending') |
| 103 | + }) |
| 104 | + |
105 | 105 | expect(spy).toHaveBeenCalledTimes(1) |
106 | 106 |
|
107 | 107 | for (let x = 1; x < 3; x++) { |
108 | 108 | rerender(6) |
109 | | - // @ts-ignore |
110 | | - while (result.current.status === 'pending') { |
111 | | - await waitForNextUpdate() |
112 | | - } |
| 109 | + await waitFor(() => { |
| 110 | + expect(result.current.status).not.toBe('pending') |
| 111 | + }) |
113 | 112 | expect(spy).toHaveBeenCalledTimes(2) |
114 | 113 | } |
115 | 114 |
|
116 | 115 | for (let x = 1; x < 3; x++) { |
117 | 116 | rerender(7) |
118 | | - // @ts-ignore |
119 | | - while (result.current.status === 'pending') { |
120 | | - await waitForNextUpdate() |
121 | | - } |
| 117 | + await waitFor(() => { |
| 118 | + expect(result.current.status).not.toBe('pending') |
| 119 | + }) |
122 | 120 | expect(spy).toHaveBeenCalledTimes(3) |
123 | 121 | } |
124 | 122 | }) |
125 | 123 |
|
126 | 124 | test('only re-trigger on shallow-equal arg change', async () => { |
127 | | - const { result, rerender, waitForNextUpdate } = renderHook( |
| 125 | + const { result, rerender } = renderHook( |
128 | 126 | (props) => getUser.useQuery(props), |
129 | 127 | { |
130 | 128 | wrapper: withProvider(store), |
131 | 129 | initialProps: { name: 'Bob', likes: 'iceCream' }, |
132 | 130 | } |
133 | 131 | ) |
134 | 132 |
|
135 | | - while (result.current.status === 'pending') { |
136 | | - await waitForNextUpdate() |
137 | | - } |
| 133 | + await waitFor(() => { |
| 134 | + expect(result.current.status).not.toBe('pending') |
| 135 | + }) |
138 | 136 | expect(spy).toHaveBeenCalledTimes(1) |
139 | 137 |
|
140 | 138 | for (let x = 1; x < 3; x++) { |
141 | 139 | rerender({ name: 'Bob', likes: 'waffles' }) |
142 | | - // @ts-ignore |
143 | | - while (result.current.status === 'pending') { |
144 | | - await waitForNextUpdate() |
145 | | - } |
| 140 | + await waitFor(() => { |
| 141 | + expect(result.current.status).not.toBe('pending') |
| 142 | + }) |
146 | 143 | expect(spy).toHaveBeenCalledTimes(2) |
147 | 144 | } |
148 | 145 |
|
149 | 146 | for (let x = 1; x < 3; x++) { |
150 | 147 | rerender({ name: 'Alice', likes: 'waffles' }) |
151 | | - // @ts-ignore |
152 | | - while (result.current.status === 'pending') { |
153 | | - await waitForNextUpdate() |
154 | | - } |
| 148 | + await waitFor(() => { |
| 149 | + expect(result.current.status).not.toBe('pending') |
| 150 | + }) |
155 | 151 | expect(spy).toHaveBeenCalledTimes(3) |
156 | 152 | } |
157 | 153 | }) |
158 | 154 |
|
159 | 155 | test('re-triggers every time on deeper value changes', async () => { |
160 | 156 | const name = 'Tim' |
161 | 157 |
|
162 | | - const { result, rerender, waitForNextUpdate } = renderHook( |
| 158 | + const { result, rerender } = renderHook( |
163 | 159 | (props) => getUser.useQuery(props), |
164 | 160 | { |
165 | 161 | wrapper: withProvider(store), |
166 | 162 | initialProps: { person: { name } }, |
167 | 163 | } |
168 | 164 | ) |
169 | 165 |
|
170 | | - while (result.current.status === 'pending') { |
171 | | - await waitForNextUpdate() |
172 | | - } |
| 166 | + await waitFor(() => { |
| 167 | + expect(result.current.status).not.toBe('pending') |
| 168 | + }) |
173 | 169 | expect(spy).toHaveBeenCalledTimes(1) |
174 | 170 |
|
175 | 171 | for (let x = 1; x < 3; x++) { |
176 | 172 | rerender({ person: { name: name + x } }) |
177 | | - // @ts-ignore |
178 | | - while (result.current.status === 'pending') { |
179 | | - await waitForNextUpdate() |
180 | | - } |
| 173 | + await waitFor(() => { |
| 174 | + expect(result.current.status).not.toBe('pending') |
| 175 | + }) |
181 | 176 | expect(spy).toHaveBeenCalledTimes(x + 1) |
182 | 177 | } |
183 | 178 | }) |
184 | 179 |
|
185 | 180 | test('do not re-trigger if the order of keys change while maintaining the same values', async () => { |
186 | | - const { result, rerender, waitForNextUpdate } = renderHook( |
| 181 | + const { result, rerender } = renderHook( |
187 | 182 | (props) => getUser.useQuery(props), |
188 | 183 | { |
189 | 184 | wrapper: withProvider(store), |
190 | 185 | initialProps: { name: 'Tim', likes: 'Bananas' }, |
191 | 186 | } |
192 | 187 | ) |
193 | 188 |
|
194 | | - while (result.current.status === 'pending') { |
195 | | - await waitForNextUpdate() |
196 | | - } |
| 189 | + await waitFor(() => { |
| 190 | + expect(result.current.status).not.toBe('pending') |
| 191 | + }) |
197 | 192 | expect(spy).toHaveBeenCalledTimes(1) |
198 | 193 |
|
199 | 194 | for (let x = 1; x < 3; x++) { |
200 | 195 | rerender({ likes: 'Bananas', name: 'Tim' }) |
201 | | - // @ts-ignore |
202 | | - while (result.current.status === 'pending') { |
203 | | - await waitForNextUpdate() |
204 | | - } |
| 196 | + await waitFor(() => { |
| 197 | + expect(result.current.status).not.toBe('pending') |
| 198 | + }) |
205 | 199 | expect(spy).toHaveBeenCalledTimes(1) |
206 | 200 | } |
207 | 201 | }) |
|
0 commit comments