1
1
import { createApi } from '@reduxjs/toolkit/query/react'
2
2
import { setupApiStore , waitMs } from './helpers'
3
3
import React from 'react'
4
- import { render , screen , getByTestId , waitFor } from '@testing-library/react'
4
+ import {
5
+ render ,
6
+ screen ,
7
+ getByTestId ,
8
+ waitFor ,
9
+ act ,
10
+ } from '@testing-library/react'
5
11
6
12
describe ( 'fixedCacheKey' , ( ) => {
7
13
const api = createApi ( {
@@ -55,7 +61,9 @@ describe('fixedCacheKey', () => {
55
61
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
56
62
expect ( getByTestId ( c2 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
57
63
58
- getByTestId ( c1 , 'trigger' ) . click ( )
64
+ act ( ( ) => {
65
+ getByTestId ( c1 , 'trigger' ) . click ( )
66
+ } )
59
67
60
68
await waitFor ( ( ) =>
61
69
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -77,7 +85,9 @@ describe('fixedCacheKey', () => {
77
85
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
78
86
expect ( getByTestId ( c2 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
79
87
80
- getByTestId ( c1 , 'trigger' ) . click ( )
88
+ act ( ( ) => {
89
+ getByTestId ( c1 , 'trigger' ) . click ( )
90
+ } )
81
91
82
92
await waitFor ( ( ) => {
83
93
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -87,8 +97,9 @@ describe('fixedCacheKey', () => {
87
97
} )
88
98
89
99
// test reset from the other component
90
- getByTestId ( c2 , 'reset' ) . click ( )
91
-
100
+ act ( ( ) => {
101
+ getByTestId ( c2 , 'reset' ) . click ( )
102
+ } )
92
103
await waitFor ( ( ) => {
93
104
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
94
105
expect ( getByTestId ( c1 , 'data' ) . textContent ) . toBe ( '' )
@@ -117,7 +128,10 @@ describe('fixedCacheKey', () => {
117
128
expect ( getByTestId ( c4 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
118
129
119
130
// trigger with a component using the first cache key
120
- getByTestId ( c1 , 'trigger' ) . click ( )
131
+
132
+ act ( ( ) => {
133
+ getByTestId ( c1 , 'trigger' ) . click ( )
134
+ } )
121
135
122
136
await waitFor ( ( ) =>
123
137
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -136,7 +150,10 @@ describe('fixedCacheKey', () => {
136
150
expect ( getByTestId ( c4 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
137
151
138
152
// trigger with a component using the second cache key
139
- getByTestId ( c3 , 'trigger' ) . click ( )
153
+
154
+ act ( ( ) => {
155
+ getByTestId ( c3 , 'trigger' ) . click ( )
156
+ } )
140
157
141
158
await waitFor ( ( ) =>
142
159
expect ( getByTestId ( c3 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -157,7 +174,10 @@ describe('fixedCacheKey', () => {
157
174
} )
158
175
159
176
// test reset from the component that triggered the mutation for the first cache key
160
- getByTestId ( c1 , 'reset' ) . click ( )
177
+
178
+ act ( ( ) => {
179
+ getByTestId ( c1 , 'reset' ) . click ( )
180
+ } )
161
181
162
182
await waitFor ( ( ) => {
163
183
// the components with the first cache key should be affected
@@ -187,7 +207,9 @@ describe('fixedCacheKey', () => {
187
207
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
188
208
expect ( getByTestId ( c2 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
189
209
190
- getByTestId ( c1 , 'trigger' ) . click ( )
210
+ act ( ( ) => {
211
+ getByTestId ( c1 , 'trigger' ) . click ( )
212
+ } )
191
213
192
214
await waitFor ( ( ) =>
193
215
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -203,7 +225,9 @@ describe('fixedCacheKey', () => {
203
225
let c1 = screen . getByTestId ( 'C1' )
204
226
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
205
227
206
- getByTestId ( c1 , 'trigger' ) . click ( )
228
+ act ( ( ) => {
229
+ getByTestId ( c1 , 'trigger' ) . click ( )
230
+ } )
207
231
208
232
await waitFor ( ( ) =>
209
233
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -232,7 +256,9 @@ describe('fixedCacheKey', () => {
232
256
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
233
257
expect ( getByTestId ( c2 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
234
258
235
- getByTestId ( c1 , 'trigger' ) . click ( )
259
+ act ( ( ) => {
260
+ getByTestId ( c1 , 'trigger' ) . click ( )
261
+ } )
236
262
237
263
await waitFor ( ( ) =>
238
264
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'fulfilled' )
@@ -251,7 +277,9 @@ describe('fixedCacheKey', () => {
251
277
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
252
278
expect ( getByTestId ( c1 , 'originalArgs' ) . textContent ) . toBe ( 'undefined' )
253
279
254
- getByTestId ( c1 , 'trigger' ) . click ( )
280
+ act ( ( ) => {
281
+ getByTestId ( c1 , 'trigger' ) . click ( )
282
+ } )
255
283
256
284
expect ( getByTestId ( c1 , 'originalArgs' ) . textContent ) . toBe ( 'C1' )
257
285
} )
@@ -264,7 +292,9 @@ describe('fixedCacheKey', () => {
264
292
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
265
293
expect ( getByTestId ( c1 , 'originalArgs' ) . textContent ) . toBe ( 'undefined' )
266
294
267
- getByTestId ( c1 , 'trigger' ) . click ( )
295
+ await act ( async ( ) => {
296
+ getByTestId ( c1 , 'trigger' ) . click ( )
297
+ } )
268
298
269
299
expect ( getByTestId ( c1 , 'originalArgs' ) . textContent ) . toBe ( 'undefined' )
270
300
} )
@@ -289,24 +319,32 @@ describe('fixedCacheKey', () => {
289
319
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
290
320
expect ( getByTestId ( c2 , 'status' ) . textContent ) . toBe ( 'uninitialized' )
291
321
292
- getByTestId ( c1 , 'trigger' ) . click ( )
322
+ act ( ( ) => {
323
+ getByTestId ( c1 , 'trigger' ) . click ( )
324
+ } )
293
325
294
326
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'pending' )
295
327
expect ( getByTestId ( c1 , 'data' ) . textContent ) . toBe ( '' )
296
328
297
- getByTestId ( c2 , 'trigger' ) . click ( )
329
+ act ( ( ) => {
330
+ getByTestId ( c2 , 'trigger' ) . click ( )
331
+ } )
298
332
299
333
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'pending' )
300
334
expect ( getByTestId ( c1 , 'data' ) . textContent ) . toBe ( '' )
301
335
302
- resolve1 ! ( 'this should not show up any more' )
336
+ act ( ( ) => {
337
+ resolve1 ! ( 'this should not show up any more' )
338
+ } )
303
339
304
340
await waitMs ( )
305
341
306
342
expect ( getByTestId ( c1 , 'status' ) . textContent ) . toBe ( 'pending' )
307
343
expect ( getByTestId ( c1 , 'data' ) . textContent ) . toBe ( '' )
308
344
309
- resolve2 ! ( 'this should be visible' )
345
+ act ( ( ) => {
346
+ resolve2 ! ( 'this should be visible' )
347
+ } )
310
348
311
349
await waitMs ( )
312
350
0 commit comments