|
| 1 | +import type { RequestParameters } from "relay-runtime"; |
| 2 | + |
| 3 | +import { createFetchMultipartSubscription } from "@apollo/client/utilities/subscriptions/relay"; |
| 4 | + |
| 5 | +const mockRequestParameters: RequestParameters = { |
| 6 | + cacheID: "test-cache-id", |
| 7 | + id: null, |
| 8 | + text: "subscription { test }", |
| 9 | + name: "TestSubscription", |
| 10 | + operationKind: "subscription", |
| 11 | + metadata: {}, |
| 12 | +} as const; |
| 13 | + |
| 14 | +describe("createFetchMultipartSubscription", () => { |
| 15 | + describe("abort controller support", () => { |
| 16 | + it("should pass an abort signal to fetch", () => { |
| 17 | + let receivedSignal: AbortSignal | undefined; |
| 18 | + |
| 19 | + const mockFetch = jest.fn( |
| 20 | + (_url: string, options: RequestInit) => |
| 21 | + new Promise<Response>(() => { |
| 22 | + // Capture the signal for verification |
| 23 | + receivedSignal = options.signal as AbortSignal; |
| 24 | + }) |
| 25 | + ); |
| 26 | + |
| 27 | + const subscribe = createFetchMultipartSubscription("/graphql", { |
| 28 | + fetch: mockFetch as typeof fetch, |
| 29 | + }); |
| 30 | + |
| 31 | + const observable = subscribe(mockRequestParameters, {}); |
| 32 | + |
| 33 | + observable.subscribe({ |
| 34 | + next: () => {}, |
| 35 | + error: () => {}, |
| 36 | + complete: () => {}, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(mockFetch).toHaveBeenCalled(); |
| 40 | + expect(receivedSignal).toBeDefined(); |
| 41 | + expect(receivedSignal?.aborted).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should abort the fetch when unsubscribe is called", () => { |
| 45 | + let receivedSignal: AbortSignal | undefined; |
| 46 | + |
| 47 | + const mockFetch = jest.fn( |
| 48 | + (_url: string, options: RequestInit) => |
| 49 | + new Promise<Response>(() => { |
| 50 | + receivedSignal = options.signal as AbortSignal; |
| 51 | + }) |
| 52 | + ); |
| 53 | + |
| 54 | + const subscribe = createFetchMultipartSubscription("/graphql", { |
| 55 | + fetch: mockFetch as typeof fetch, |
| 56 | + }); |
| 57 | + |
| 58 | + const observable = subscribe(mockRequestParameters, {}); |
| 59 | + |
| 60 | + const subscription = observable.subscribe({ |
| 61 | + next: () => {}, |
| 62 | + error: () => {}, |
| 63 | + complete: () => {}, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(receivedSignal?.aborted).toBe(false); |
| 67 | + |
| 68 | + subscription.unsubscribe(); |
| 69 | + |
| 70 | + expect(receivedSignal?.aborted).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should not call sink.error when fetch is aborted", async () => { |
| 74 | + const errorSpy = jest.fn(); |
| 75 | + |
| 76 | + const mockFetch = jest.fn((_url: string, options: RequestInit) => { |
| 77 | + return new Promise<Response>((_resolve, reject) => { |
| 78 | + options.signal?.addEventListener("abort", () => { |
| 79 | + const abortError = new Error("The operation was aborted."); |
| 80 | + abortError.name = "AbortError"; |
| 81 | + reject(abortError); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + const subscribe = createFetchMultipartSubscription("/graphql", { |
| 87 | + fetch: mockFetch as typeof fetch, |
| 88 | + }); |
| 89 | + |
| 90 | + const observable = subscribe(mockRequestParameters, {}); |
| 91 | + |
| 92 | + const subscription = observable.subscribe({ |
| 93 | + next: () => {}, |
| 94 | + error: errorSpy, |
| 95 | + complete: () => {}, |
| 96 | + }); |
| 97 | + |
| 98 | + subscription.unsubscribe(); |
| 99 | + |
| 100 | + // Allow any pending promises to resolve |
| 101 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 102 | + |
| 103 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should still call sink.error for non-abort errors", async () => { |
| 107 | + const errorSpy = jest.fn(); |
| 108 | + const networkError = new Error("Network failure"); |
| 109 | + |
| 110 | + const mockFetch = jest.fn(() => Promise.reject(networkError)); |
| 111 | + |
| 112 | + const subscribe = createFetchMultipartSubscription("/graphql", { |
| 113 | + fetch: mockFetch as typeof fetch, |
| 114 | + }); |
| 115 | + |
| 116 | + const observable = subscribe(mockRequestParameters, {}); |
| 117 | + |
| 118 | + observable.subscribe({ |
| 119 | + next: () => {}, |
| 120 | + error: errorSpy, |
| 121 | + complete: () => {}, |
| 122 | + }); |
| 123 | + |
| 124 | + // Allow any pending promises to resolve |
| 125 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 126 | + |
| 127 | + expect(errorSpy).toHaveBeenCalledWith(networkError); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments