1
1
import { Client } from "@gadget-client/related-products-example" ;
2
2
import { diff } from "@n1ru4l/json-patch-plus" ;
3
- import { render , renderHook } from "@testing-library/react" ;
3
+ import { render , renderHook , waitFor } from "@testing-library/react" ;
4
4
import React from "react" ;
5
5
import { useFindMany } from "../src/useFindMany.js" ;
6
6
import { MockGraphQLWSClientWrapper , mockGraphQLWSClient } from "./testWrappers.js" ;
7
- import { sleep } from "./utils.js" ;
8
7
9
8
describe ( "live queries" , ( ) => {
10
9
let api : Client ;
@@ -37,9 +36,7 @@ describe("live queries", () => {
37
36
revision : 1 ,
38
37
} as any ) ;
39
38
40
- await sleep ( 100 ) ;
41
-
42
- expect ( result . current [ 0 ] . data ! . length ) . toEqual ( 0 ) ;
39
+ await waitFor ( ( ) => expect ( result . current [ 0 ] . data ! . length ) . toEqual ( 0 ) ) ;
43
40
expect ( result . current [ 0 ] . fetching ) . toBe ( false ) ;
44
41
expect ( result . current [ 0 ] . error ) . toBeFalsy ( ) ;
45
42
@@ -61,10 +58,9 @@ describe("live queries", () => {
61
58
revision : 2 ,
62
59
} as any ) ;
63
60
64
- await sleep ( 100 ) ;
61
+ await waitFor ( ( ) => expect ( result . current [ 0 ] . data ! [ 0 ] . email ) . toEqual ( "[email protected] " ) ) ;
65
62
66
63
expect ( result . current [ 0 ] . data ! [ 0 ] . id ) . toEqual ( "123" ) ;
67
- expect ( result . current [ 0 ] . data ! [ 0 ] . email ) . toEqual ( "[email protected] " ) ;
68
64
expect ( result . current [ 0 ] . fetching ) . toBe ( false ) ;
69
65
expect ( result . current [ 0 ] . error ) . toBeFalsy ( ) ;
70
66
} ) ;
@@ -101,10 +97,9 @@ describe("live queries", () => {
101
97
revision : 1 ,
102
98
} as any ) ;
103
99
104
- await sleep ( 100 ) ;
100
+ await waitFor ( ( ) => expect ( result . current [ 0 ] . fetching ) . toBe ( false ) ) ;
105
101
106
102
expect ( result . current [ 0 ] . data ! . length ) . toEqual ( 1 ) ;
107
- expect ( result . current [ 0 ] . fetching ) . toBe ( false ) ;
108
103
expect ( result . current [ 0 ] . error ) . toBeFalsy ( ) ;
109
104
110
105
const next = {
@@ -118,9 +113,7 @@ describe("live queries", () => {
118
113
revision : 2 ,
119
114
} as any ) ;
120
115
121
- await sleep ( 100 ) ;
122
-
123
- expect ( result . current [ 0 ] . data ! . length ) . toEqual ( 0 ) ;
116
+ await waitFor ( ( ) => expect ( result . current [ 0 ] . data ! . length ) . toEqual ( 0 ) ) ;
124
117
expect ( result . current [ 0 ] . fetching ) . toBe ( false ) ;
125
118
expect ( result . current [ 0 ] . error ) . toBeFalsy ( ) ;
126
119
} ) ;
@@ -185,9 +178,7 @@ describe("live queries", () => {
185
178
revision : 1 ,
186
179
} as any ) ;
187
180
188
- await sleep ( 100 ) ;
189
-
190
- expect ( container ) . toHaveTextContent ( "Users: 2, Products: 1" ) ;
181
+ await waitFor ( ( ) => expect ( container ) . toHaveTextContent ( "Users: 2, Products: 1" ) ) ;
191
182
192
183
products . push ( {
193
184
data : {
@@ -198,8 +189,6 @@ describe("live queries", () => {
198
189
revision : 1 ,
199
190
} as any ) ;
200
191
201
- await sleep ( 100 ) ;
202
-
203
- expect ( container ) . toHaveTextContent ( "Users: 2, Products: 0" ) ;
192
+ await waitFor ( ( ) => expect ( container ) . toHaveTextContent ( "Users: 2, Products: 0" ) ) ;
204
193
} ) ;
205
194
} ) ;
0 commit comments