|
| 1 | +import { Client } from "@gadget-client/related-products-example"; |
1 | 2 | import type { GadgetRecordList } from "@gadgetinc/api-client-core";
|
2 | 3 | import { diff } from "@n1ru4l/json-patch-plus";
|
3 | 4 | import { renderHook, waitFor } from "@testing-library/react";
|
4 | 5 | import type { IsExact } from "conditional-type-checks";
|
5 | 6 | import { assert } from "conditional-type-checks";
|
| 7 | +import { GraphQLError } from "graphql"; |
6 | 8 | import { useFindMany } from "../src/useFindMany.js";
|
7 | 9 | import type { ErrorWrapper } from "../src/utils.js";
|
8 | 10 | import { relatedProductsApi } from "./apis.js";
|
@@ -269,6 +271,12 @@ describe("useFindMany", () => {
|
269 | 271 | });
|
270 | 272 |
|
271 | 273 | describe("live queries", () => {
|
| 274 | + let relatedProductsApi: Client; |
| 275 | + |
| 276 | + beforeEach(() => { |
| 277 | + relatedProductsApi = new Client({ environment: "Development" }); |
| 278 | + }); |
| 279 | + |
272 | 280 | test("can query for live data", async () => {
|
273 | 281 | const { result } = renderHook(() => useFindMany(relatedProductsApi.user, { live: true }), {
|
274 | 282 | wrapper: MockGraphQLWSClientWrapper(relatedProductsApi),
|
@@ -356,5 +364,29 @@ describe("useFindMany", () => {
|
356 | 364 | expect(result.current[0].fetching).toBe(false);
|
357 | 365 | expect(result.current[0].error).toBeFalsy();
|
358 | 366 | });
|
| 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 | + }); |
359 | 391 | });
|
360 | 392 | });
|
0 commit comments