@@ -38,8 +38,22 @@ jest.mock('@apollo/client', () => {
38
38
const givenQueryResult = response => {
39
39
useLazyQuery . mockReset ( ) ;
40
40
runQuery . mockReset ( ) ;
41
+ const resolve = jest . fn ( ) . mockName ( 'resolve' ) ;
42
+ const reject = jest . fn ( ) . mockName ( 'reject' ) ;
41
43
useLazyQuery . mockImplementation ( ( ) => {
42
- return [ runQuery , response ] ;
44
+ // Create a promise that resolves to the mocked response
45
+ const queryPromise = new Promise ( ( resolve , reject ) => {
46
+ // Immediately resolve with the response
47
+ resolve ( response ) ;
48
+ } ) ;
49
+
50
+ // Return a tuple where the first item is an async function (runQuery) and the second is the response object
51
+ return [
52
+ async ( ) => {
53
+ return await queryPromise ; // Return the response when the promise resolves
54
+ } ,
55
+ response
56
+ ] ;
43
57
} ) ;
44
58
} ;
45
59
@@ -243,7 +257,8 @@ describe('returns NOT_FOUND when queries come back empty', () => {
243
257
} ) ;
244
258
245
259
describe ( 'returns REDIRECT after receiving a redirect code' , ( ) => {
246
- test ( 'redirect code 301' , async ( ) => {
260
+ test ( 'redirect code 301' , ( ) => {
261
+ // Arrange: Set up the query result
247
262
givenQueryResult ( {
248
263
data : {
249
264
route : {
@@ -256,15 +271,24 @@ describe('returns REDIRECT after receiving a redirect code', () => {
256
271
loading : false
257
272
} ) ;
258
273
259
- await act ( ( ) => {
260
- create ( < Component /> ) ;
261
- } ) ;
262
-
263
- expect ( replace ) . toHaveBeenCalledTimes ( 1 ) ;
264
- expect ( log ) . toHaveBeenCalledTimes ( 1 ) ;
265
- expect ( log ) . toHaveBeenNthCalledWith ( 1 , {
266
- isRedirect : true ,
267
- relativeUrl : '/foo.html'
274
+ // Create a new Promise to handle `act`
275
+ return new Promise ( resolve => {
276
+ // Use `act` to render the component
277
+ act ( ( ) => {
278
+ // Render the component and resolve the promise
279
+ create ( < Component /> ) ;
280
+ resolve ( ) ;
281
+ } ) ;
282
+ } ) . then ( ( ) => {
283
+ // Assert that `replace` was called once
284
+ expect ( replace ) . toHaveBeenCalledTimes ( 1 ) ;
285
+ // Assert that `log` was called once
286
+ expect ( log ) . toHaveBeenCalledTimes ( 1 ) ;
287
+ // Assert that `log` was called with the expected arguments
288
+ expect ( log ) . toHaveBeenNthCalledWith ( 1 , {
289
+ isRedirect : true ,
290
+ relativeUrl : '/foo.html'
291
+ } ) ;
268
292
} ) ;
269
293
} ) ;
270
294
0 commit comments