|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import type { Operation } from "@urql/core"; |
| 3 | +import { gql, makeOperation } from "@urql/core"; |
| 4 | +import { fromArray, pipe, toArray } from "wonka"; |
| 5 | +import { liveQueryExchange } from "../../src/exchanges/liveQueryExchange.js"; |
| 6 | + |
| 7 | +describe("liveQueryExchange", () => { |
| 8 | + const mockForward = jest.fn((ops$: any) => ops$); |
| 9 | + let exchange: any; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + exchange = liveQueryExchange({ forward: mockForward, client: {} as any, dispatchDebug: jest.fn() }); |
| 14 | + }); |
| 15 | + |
| 16 | + const createLiveQuery = (key = 1, variables: any = {}): Operation => { |
| 17 | + return makeOperation( |
| 18 | + "query", |
| 19 | + { |
| 20 | + key, |
| 21 | + query: gql` |
| 22 | + query TestQuery @live { |
| 23 | + users { |
| 24 | + id |
| 25 | + name |
| 26 | + } |
| 27 | + } |
| 28 | + `, |
| 29 | + variables, |
| 30 | + }, |
| 31 | + {} as any |
| 32 | + ); |
| 33 | + }; |
| 34 | + |
| 35 | + const createRegularQuery = (key = 2): Operation => { |
| 36 | + return makeOperation( |
| 37 | + "query", |
| 38 | + { |
| 39 | + key, |
| 40 | + query: gql` |
| 41 | + query TestQuery { |
| 42 | + users { |
| 43 | + id |
| 44 | + name |
| 45 | + } |
| 46 | + } |
| 47 | + `, |
| 48 | + variables: {}, |
| 49 | + }, |
| 50 | + {} as any |
| 51 | + ); |
| 52 | + }; |
| 53 | + |
| 54 | + const createTeardown = (key = 1, variables: any = {}): Operation => { |
| 55 | + return makeOperation( |
| 56 | + "teardown", |
| 57 | + { |
| 58 | + key, |
| 59 | + query: gql` |
| 60 | + query TestQuery @live { |
| 61 | + users { |
| 62 | + id |
| 63 | + name |
| 64 | + } |
| 65 | + } |
| 66 | + `, |
| 67 | + variables, |
| 68 | + }, |
| 69 | + {} as any |
| 70 | + ); |
| 71 | + }; |
| 72 | + |
| 73 | + test("allows first live query to pass through", () => { |
| 74 | + const liveQuery = createLiveQuery(1); |
| 75 | + const operations$ = fromArray([liveQuery]); |
| 76 | + |
| 77 | + const result$ = exchange(operations$); |
| 78 | + const results = pipe(result$, toArray); |
| 79 | + |
| 80 | + expect(results).toHaveLength(1); |
| 81 | + expect(results[0]).toBe(liveQuery); |
| 82 | + }); |
| 83 | + |
| 84 | + test("blocks duplicate live query execution with same variables", () => { |
| 85 | + const liveQuery1 = createLiveQuery(1, { filter: { id: "1" } }); |
| 86 | + const liveQuery2 = createLiveQuery(1, { filter: { id: "1" } }); // Same key and variables |
| 87 | + const operations$ = fromArray([liveQuery1, liveQuery2]); |
| 88 | + |
| 89 | + const result$ = exchange(operations$); |
| 90 | + const results = pipe(result$, toArray); |
| 91 | + |
| 92 | + expect(results).toHaveLength(1); |
| 93 | + expect(results[0]).toBe(liveQuery1); |
| 94 | + }); |
| 95 | + |
| 96 | + test("allows regular queries to pass through unchanged", () => { |
| 97 | + const regularQuery1 = createRegularQuery(1); |
| 98 | + const regularQuery2 = createRegularQuery(1); // Same key but not live |
| 99 | + const operations$ = fromArray([regularQuery1, regularQuery2]); |
| 100 | + |
| 101 | + const result$ = exchange(operations$); |
| 102 | + const results = pipe(result$, toArray); |
| 103 | + |
| 104 | + expect(results).toHaveLength(2); |
| 105 | + expect(results[0]).toBe(regularQuery1); |
| 106 | + expect(results[1]).toBe(regularQuery2); |
| 107 | + }); |
| 108 | + |
| 109 | + test("allows teardown operations to pass through", () => { |
| 110 | + const liveQuery = createLiveQuery(1); |
| 111 | + const teardown = createTeardown(1); |
| 112 | + const operations$ = fromArray([liveQuery, teardown]); |
| 113 | + |
| 114 | + const result$ = exchange(operations$); |
| 115 | + const results = pipe(result$, toArray); |
| 116 | + |
| 117 | + expect(results).toHaveLength(2); |
| 118 | + expect(results[0]).toBe(liveQuery); |
| 119 | + expect(results[1]).toBe(teardown); |
| 120 | + }); |
| 121 | + |
| 122 | + test("allows live query re-execution after teardown", () => { |
| 123 | + const variables = { filter: { id: "1" } }; |
| 124 | + const liveQuery1 = createLiveQuery(1, variables); |
| 125 | + const teardown = createTeardown(1, variables); // Same variables as the query |
| 126 | + const liveQuery2 = createLiveQuery(1, variables); // Same key and variables after teardown |
| 127 | + const operations$ = fromArray([liveQuery1, teardown, liveQuery2]); |
| 128 | + |
| 129 | + const result$ = exchange(operations$); |
| 130 | + const results = pipe(result$, toArray); |
| 131 | + |
| 132 | + expect(results).toHaveLength(3); |
| 133 | + expect(results[0]).toBe(liveQuery1); |
| 134 | + expect(results[1]).toBe(teardown); |
| 135 | + expect(results[2]).toBe(liveQuery2); |
| 136 | + }); |
| 137 | + |
| 138 | + test("handles multiple live queries with different keys", () => { |
| 139 | + const liveQuery1 = createLiveQuery(1, { filter: { id: "1" } }); |
| 140 | + const liveQuery2 = createLiveQuery(2, { filter: { id: "2" } }); |
| 141 | + const operations$ = fromArray([liveQuery1, liveQuery2]); |
| 142 | + |
| 143 | + const result$ = exchange(operations$); |
| 144 | + const results = pipe(result$, toArray); |
| 145 | + |
| 146 | + expect(results).toHaveLength(2); |
| 147 | + expect(results[0]).toBe(liveQuery1); |
| 148 | + expect(results[1]).toBe(liveQuery2); |
| 149 | + }); |
| 150 | + |
| 151 | + test("allows non-query operations for live queries to pass through", () => { |
| 152 | + const liveQuery = createLiveQuery(1); |
| 153 | + const mutation = makeOperation( |
| 154 | + "mutation", |
| 155 | + { |
| 156 | + key: 1, |
| 157 | + query: gql` |
| 158 | + mutation TestMutation @live { |
| 159 | + updateUser(id: "1", user: { name: "test" }) { |
| 160 | + id |
| 161 | + name |
| 162 | + } |
| 163 | + } |
| 164 | + `, |
| 165 | + variables: {}, |
| 166 | + }, |
| 167 | + {} as any |
| 168 | + ); |
| 169 | + const operations$ = fromArray([liveQuery, mutation]); |
| 170 | + |
| 171 | + const result$ = exchange(operations$); |
| 172 | + const results = pipe(result$, toArray); |
| 173 | + |
| 174 | + expect(results).toHaveLength(2); |
| 175 | + expect(results[0]).toBe(liveQuery); |
| 176 | + expect(results[1]).toBe(mutation); |
| 177 | + }); |
| 178 | +}); |
0 commit comments