Skip to content

Commit efc2063

Browse files
committed
Switch to more robust wait conditions in live tests instead of hardcoded sleeps
1 parent d9d9902 commit efc2063

File tree

5 files changed

+19
-40
lines changed

5 files changed

+19
-40
lines changed

packages/react/spec/liveQueries.spec.tsx

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Client } from "@gadget-client/related-products-example";
22
import { diff } from "@n1ru4l/json-patch-plus";
3-
import { render, renderHook } from "@testing-library/react";
3+
import { render, renderHook, waitFor } from "@testing-library/react";
44
import React from "react";
55
import { useFindMany } from "../src/useFindMany.js";
66
import { MockGraphQLWSClientWrapper, mockGraphQLWSClient } from "./testWrappers.js";
7-
import { sleep } from "./utils.js";
87

98
describe("live queries", () => {
109
let api: Client;
@@ -37,9 +36,7 @@ describe("live queries", () => {
3736
revision: 1,
3837
} as any);
3938

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));
4340
expect(result.current[0].fetching).toBe(false);
4441
expect(result.current[0].error).toBeFalsy();
4542

@@ -61,10 +58,9 @@ describe("live queries", () => {
6158
revision: 2,
6259
} as any);
6360

64-
await sleep(100);
61+
await waitFor(() => expect(result.current[0].data![0].email).toEqual("[email protected]"));
6562

6663
expect(result.current[0].data![0].id).toEqual("123");
67-
expect(result.current[0].data![0].email).toEqual("[email protected]");
6864
expect(result.current[0].fetching).toBe(false);
6965
expect(result.current[0].error).toBeFalsy();
7066
});
@@ -101,10 +97,9 @@ describe("live queries", () => {
10197
revision: 1,
10298
} as any);
10399

104-
await sleep(100);
100+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
105101

106102
expect(result.current[0].data!.length).toEqual(1);
107-
expect(result.current[0].fetching).toBe(false);
108103
expect(result.current[0].error).toBeFalsy();
109104

110105
const next = {
@@ -118,9 +113,7 @@ describe("live queries", () => {
118113
revision: 2,
119114
} as any);
120115

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));
124117
expect(result.current[0].fetching).toBe(false);
125118
expect(result.current[0].error).toBeFalsy();
126119
});
@@ -185,9 +178,7 @@ describe("live queries", () => {
185178
revision: 1,
186179
} as any);
187180

188-
await sleep(100);
189-
190-
expect(container).toHaveTextContent("Users: 2, Products: 1");
181+
await waitFor(() => expect(container).toHaveTextContent("Users: 2, Products: 1"));
191182

192183
products.push({
193184
data: {
@@ -198,8 +189,6 @@ describe("live queries", () => {
198189
revision: 1,
199190
} as any);
200191

201-
await sleep(100);
202-
203-
expect(container).toHaveTextContent("Users: 2, Products: 0");
192+
await waitFor(() => expect(container).toHaveTextContent("Users: 2, Products: 0"));
204193
});
205194
});

packages/react/spec/useFindBy.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { GadgetRecord } from "@gadgetinc/api-client-core";
22
import { diff } from "@n1ru4l/json-patch-plus";
3-
import { renderHook } from "@testing-library/react";
3+
import { renderHook, waitFor } from "@testing-library/react";
44
import type { IsExact } from "conditional-type-checks";
55
import { assert } from "conditional-type-checks";
66
import { useFindBy } from "../src/index.js";
77
import type { ErrorWrapper } from "../src/utils.js";
88
import { relatedProductsApi } from "./apis.js";
99
import { MockClientWrapper, MockGraphQLWSClientWrapper, mockGraphQLWSClient, mockUrqlClient } from "./testWrappers.js";
10-
import { sleep } from "./utils.js";
1110

1211
describe("useFindBy", () => {
1312
// these functions are typechecked but never run to avoid actually making API calls
@@ -205,11 +204,10 @@ describe("useFindBy", () => {
205204
revision: 1,
206205
} as any);
207206

208-
await sleep(30);
207+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
209208

210209
expect(result.current[0].data!.id).toEqual("123");
211210
expect(result.current[0].data!.email).toEqual("[email protected]");
212-
expect(result.current[0].fetching).toBe(false);
213211
expect(result.current[0].error).toBeFalsy();
214212

215213
const next = {
@@ -229,10 +227,9 @@ describe("useFindBy", () => {
229227
revision: 2,
230228
} as any);
231229

232-
await sleep(50);
230+
await waitFor(() => expect(result.current[0].data!.email).toEqual("[email protected]"));
233231

234232
expect(result.current[0].data!.id).toEqual("123");
235-
expect(result.current[0].data!.email).toEqual("[email protected]");
236233
expect(result.current[0].fetching).toBe(false);
237234
expect(result.current[0].error).toBeFalsy();
238235
});

packages/react/spec/useFindFirst.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { GadgetRecord } from "@gadgetinc/api-client-core";
22
import { diff } from "@n1ru4l/json-patch-plus";
3-
import { renderHook } from "@testing-library/react";
3+
import { renderHook, waitFor } from "@testing-library/react";
44
import type { IsExact } from "conditional-type-checks";
55
import { assert } from "conditional-type-checks";
66
import { useFindFirst } from "../src/index.js";
77
import type { ErrorWrapper } from "../src/utils.js";
88
import { relatedProductsApi } from "./apis.js";
99
import { MockClientWrapper, MockGraphQLWSClientWrapper, mockGraphQLWSClient, mockUrqlClient } from "./testWrappers.js";
10-
import { sleep } from "./utils.js";
1110

1211
describe("useFindFirst", () => {
1312
// these functions are typechecked but never run to avoid actually making API calls
@@ -255,11 +254,10 @@ describe("useFindFirst", () => {
255254
revision: 1,
256255
} as any);
257256

258-
await sleep(30);
257+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
259258

260259
expect(result.current[0].data!.id).toEqual("123");
261260
expect(result.current[0].data!.email).toEqual("[email protected]");
262-
expect(result.current[0].fetching).toBe(false);
263261
expect(result.current[0].error).toBeFalsy();
264262

265263
const next = {
@@ -279,10 +277,9 @@ describe("useFindFirst", () => {
279277
revision: 2,
280278
} as any);
281279

282-
await sleep(50);
280+
await waitFor(() => expect(result.current[0].data!.email).toEqual("[email protected]"));
283281

284282
expect(result.current[0].data!.id).toEqual("123");
285-
expect(result.current[0].data!.email).toEqual("[email protected]");
286283
expect(result.current[0].fetching).toBe(false);
287284
expect(result.current[0].error).toBeFalsy();
288285
});

packages/react/spec/useFindMany.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { GadgetRecordList } from "@gadgetinc/api-client-core";
22
import { diff } from "@n1ru4l/json-patch-plus";
3-
import { renderHook } from "@testing-library/react";
3+
import { renderHook, waitFor } from "@testing-library/react";
44
import type { IsExact } from "conditional-type-checks";
55
import { assert } from "conditional-type-checks";
66
import { useFindMany } from "../src/useFindMany.js";
77
import type { ErrorWrapper } from "../src/utils.js";
88
import { relatedProductsApi } from "./apis.js";
99
import { MockClientWrapper, MockGraphQLWSClientWrapper, mockGraphQLWSClient, mockUrqlClient } from "./testWrappers.js";
10-
import { sleep } from "./utils.js";
1110

1211
describe("useFindMany", () => {
1312
// all these functions are typechecked but never run to avoid actually making API calls
@@ -308,13 +307,12 @@ describe("useFindMany", () => {
308307
revision: 1,
309308
} as any);
310309

311-
await sleep(30);
310+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
312311

313312
expect(result.current[0].data![0].id).toEqual("123");
314313
expect(result.current[0].data![0].email).toEqual("[email protected]");
315314
expect(result.current[0].data![1].id).toEqual("456");
316315
expect(result.current[0].data![1].email).toEqual("[email protected]");
317-
expect(result.current[0].fetching).toBe(false);
318316
expect(result.current[0].error).toBeFalsy();
319317

320318
const next = {
@@ -347,7 +345,7 @@ describe("useFindMany", () => {
347345
revision: 2,
348346
} as any);
349347

350-
await sleep(50);
348+
await waitFor(() => expect(result.current[0].data![2]).toBeDefined());
351349

352350
expect(result.current[0].data![0].id).toEqual("123");
353351
expect(result.current[0].data![0].email).toEqual("[email protected]");

packages/react/spec/useFindOne.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { GadgetRecord } from "@gadgetinc/api-client-core";
2-
import { renderHook } from "@testing-library/react";
2+
import { renderHook, waitFor } from "@testing-library/react";
33
import type { IsExact } from "conditional-type-checks";
44
import { assert } from "conditional-type-checks";
55
import { useFindOne } from "../src/index.js";
66
import type { ErrorWrapper } from "../src/utils.js";
77
import { relatedProductsApi } from "./apis.js";
88
import { MockClientWrapper, MockGraphQLWSClientWrapper, mockGraphQLWSClient, mockUrqlClient } from "./testWrappers.js";
9-
import { sleep } from "./utils.js";
109

1110
describe("useFindOne", () => {
1211
// these functions are typechecked but never run to avoid actually making API calls
@@ -190,11 +189,10 @@ describe("useFindOne", () => {
190189
revision: 1,
191190
} as any);
192191

193-
await sleep(30);
192+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
194193

195194
expect(result.current[0].data!.id).toEqual("123");
196195
expect(result.current[0].data!.email).toEqual("[email protected]");
197-
expect(result.current[0].fetching).toBe(false);
198196
expect(result.current[0].error).toBeFalsy();
199197

200198
subscription.push({
@@ -206,7 +204,7 @@ describe("useFindOne", () => {
206204
revision: 2,
207205
} as any);
208206

209-
await sleep(50);
207+
await waitFor(() => expect(result.current[0].data!.email).toEqual("[email protected]"));
210208

211209
expect(result.current[0].data!.id).toEqual("123");
212210
expect(result.current[0].data!.email).toEqual("[email protected]");

0 commit comments

Comments
 (0)