Skip to content

Commit 9c7e0bf

Browse files
committed
Add a test ensuring live useFindMany's return an error from the server
[no-changelog-required]
1 parent 674caf0 commit 9c7e0bf

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

packages/react/spec/useFindMany.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { Client } from "@gadget-client/related-products-example";
12
import type { GadgetRecordList } from "@gadgetinc/api-client-core";
23
import { diff } from "@n1ru4l/json-patch-plus";
34
import { renderHook, waitFor } from "@testing-library/react";
45
import type { IsExact } from "conditional-type-checks";
56
import { assert } from "conditional-type-checks";
7+
import { GraphQLError } from "graphql";
68
import { useFindMany } from "../src/useFindMany.js";
79
import type { ErrorWrapper } from "../src/utils.js";
810
import { relatedProductsApi } from "./apis.js";
@@ -269,6 +271,12 @@ describe("useFindMany", () => {
269271
});
270272

271273
describe("live queries", () => {
274+
let relatedProductsApi: Client;
275+
276+
beforeEach(() => {
277+
relatedProductsApi = new Client({ environment: "Development" });
278+
});
279+
272280
test("can query for live data", async () => {
273281
const { result } = renderHook(() => useFindMany(relatedProductsApi.user, { live: true }), {
274282
wrapper: MockGraphQLWSClientWrapper(relatedProductsApi),
@@ -356,5 +364,29 @@ describe("useFindMany", () => {
356364
expect(result.current[0].fetching).toBe(false);
357365
expect(result.current[0].error).toBeFalsy();
358366
});
367+
368+
test("live queries return server errors", async () => {
369+
const { result } = renderHook(() => useFindMany(relatedProductsApi.user, { live: true }), {
370+
wrapper: MockGraphQLWSClientWrapper(relatedProductsApi),
371+
});
372+
373+
expect(result.current[0].data).toBeFalsy();
374+
expect(result.current[0].fetching).toBe(true);
375+
expect(result.current[0].error).toBeFalsy();
376+
377+
await Promise.resolve();
378+
expect(mockGraphQLWSClient.subscribe.subscriptions).toHaveLength(1);
379+
const subscription = mockGraphQLWSClient.subscribe.subscriptions[0];
380+
381+
subscription.push({
382+
data: null,
383+
errors: [new GraphQLError("backend exploded")],
384+
});
385+
386+
await waitFor(() => expect(result.current[0].fetching).toBe(false));
387+
388+
expect(result.current[0].data).toBeFalsy();
389+
expect(result.current[0].error!.message).toMatchInlineSnapshot(`"[GraphQL] backend exploded"`);
390+
});
359391
});
360392
});

0 commit comments

Comments
 (0)