|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { CustomerModel } from "../_generated_sdk.js"; |
| 3 | +import { PlainClient } from "../client.js"; |
| 4 | +import { PlainConnection } from "../connection.js"; |
| 5 | +import { getRequestBody, graphqlResponse, mockFetch } from "./helpers.js"; |
| 6 | + |
| 7 | +describe("connection pagination", () => { |
| 8 | + let fetchMock: ReturnType<typeof mockFetch>; |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + function makeCustomersResponse( |
| 15 | + customers: Array<{ id: string; fullName: string; shortName: string }>, |
| 16 | + pageInfo: { |
| 17 | + hasNextPage: boolean; |
| 18 | + hasPreviousPage: boolean; |
| 19 | + startCursor: string | null; |
| 20 | + endCursor: string | null; |
| 21 | + }, |
| 22 | + ) { |
| 23 | + return { |
| 24 | + customers: { |
| 25 | + edges: customers.map((c) => ({ |
| 26 | + cursor: `cursor_${c.id}`, |
| 27 | + node: { |
| 28 | + id: c.id, |
| 29 | + fullName: c.fullName, |
| 30 | + shortName: c.shortName, |
| 31 | + externalId: null, |
| 32 | + status: "ACTIVE", |
| 33 | + isAnonymous: false, |
| 34 | + avatarUrl: null, |
| 35 | + assignedAt: null, |
| 36 | + lastIdleAt: null, |
| 37 | + markedAsSpamAt: null, |
| 38 | + statusChangedAt: null, |
| 39 | + company: { id: "comp_1" }, |
| 40 | + createdAt: { unixTimestamp: "1700000000", iso8601: "2023-11-14T22:13:20Z" }, |
| 41 | + updatedAt: { unixTimestamp: "1700000001", iso8601: "2023-11-14T22:13:21Z" }, |
| 42 | + }, |
| 43 | + })), |
| 44 | + pageInfo, |
| 45 | + }, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + it("returns a PlainConnection with model instances", async () => { |
| 50 | + fetchMock = mockFetch(); |
| 51 | + fetchMock.mockResolvedValueOnce( |
| 52 | + graphqlResponse( |
| 53 | + makeCustomersResponse( |
| 54 | + [ |
| 55 | + { id: "c_1", fullName: "Alice Smith", shortName: "Alice" }, |
| 56 | + { id: "c_2", fullName: "Bob Jones", shortName: "Bob" }, |
| 57 | + ], |
| 58 | + { |
| 59 | + hasNextPage: false, |
| 60 | + hasPreviousPage: false, |
| 61 | + startCursor: "cursor_c_1", |
| 62 | + endCursor: "cursor_c_2", |
| 63 | + }, |
| 64 | + ), |
| 65 | + ), |
| 66 | + ); |
| 67 | + const client = new PlainClient({ apiKey: "test-key" }); |
| 68 | + |
| 69 | + const connection = await client.query.customers({ first: 10 }); |
| 70 | + |
| 71 | + expect(connection).toBeInstanceOf(PlainConnection); |
| 72 | + expect(connection.nodes).toHaveLength(2); |
| 73 | + expect(connection.nodes[0]).toBeInstanceOf(CustomerModel); |
| 74 | + expect(connection.nodes[0].id).toBe("c_1"); |
| 75 | + expect(connection.nodes[0].fullName).toBe("Alice Smith"); |
| 76 | + expect(connection.nodes[1].id).toBe("c_2"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("populates pageInfo correctly", async () => { |
| 80 | + fetchMock = mockFetch(); |
| 81 | + fetchMock.mockResolvedValueOnce( |
| 82 | + graphqlResponse( |
| 83 | + makeCustomersResponse([{ id: "c_1", fullName: "Alice Smith", shortName: "Alice" }], { |
| 84 | + hasNextPage: true, |
| 85 | + hasPreviousPage: false, |
| 86 | + startCursor: "cursor_start", |
| 87 | + endCursor: "cursor_end", |
| 88 | + }), |
| 89 | + ), |
| 90 | + ); |
| 91 | + const client = new PlainClient({ apiKey: "test-key" }); |
| 92 | + |
| 93 | + const connection = await client.query.customers({ first: 1 }); |
| 94 | + |
| 95 | + expect(connection.hasNextPage).toBe(true); |
| 96 | + expect(connection.hasPreviousPage).toBe(false); |
| 97 | + expect(connection.pageInfo.startCursor).toBe("cursor_start"); |
| 98 | + expect(connection.pageInfo.endCursor).toBe("cursor_end"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("fetchNext sends request with after cursor", async () => { |
| 102 | + fetchMock = mockFetch(); |
| 103 | + // First page |
| 104 | + fetchMock.mockResolvedValueOnce( |
| 105 | + graphqlResponse( |
| 106 | + makeCustomersResponse([{ id: "c_1", fullName: "Alice Smith", shortName: "Alice" }], { |
| 107 | + hasNextPage: true, |
| 108 | + hasPreviousPage: false, |
| 109 | + startCursor: "cursor_start", |
| 110 | + endCursor: "cursor_end", |
| 111 | + }), |
| 112 | + ), |
| 113 | + ); |
| 114 | + // Second page |
| 115 | + fetchMock.mockResolvedValueOnce( |
| 116 | + graphqlResponse( |
| 117 | + makeCustomersResponse([{ id: "c_2", fullName: "Bob Jones", shortName: "Bob" }], { |
| 118 | + hasNextPage: false, |
| 119 | + hasPreviousPage: true, |
| 120 | + startCursor: "cursor_c_2", |
| 121 | + endCursor: "cursor_c_2", |
| 122 | + }), |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + const client = new PlainClient({ apiKey: "test-key" }); |
| 127 | + const firstPage = await client.query.customers({ first: 1 }); |
| 128 | + const secondPage = await firstPage.fetchNext(); |
| 129 | + |
| 130 | + expect(secondPage).toBeInstanceOf(PlainConnection); |
| 131 | + expect(secondPage!.nodes).toHaveLength(1); |
| 132 | + expect(secondPage!.nodes[0].id).toBe("c_2"); |
| 133 | + |
| 134 | + const secondCallBody = getRequestBody(fetchMock, 1); |
| 135 | + expect(secondCallBody.variables.after).toBe("cursor_end"); |
| 136 | + }); |
| 137 | + |
| 138 | + it("fetchNext returns undefined when hasNextPage is false", async () => { |
| 139 | + fetchMock = mockFetch(); |
| 140 | + fetchMock.mockResolvedValueOnce( |
| 141 | + graphqlResponse( |
| 142 | + makeCustomersResponse([{ id: "c_1", fullName: "Alice Smith", shortName: "Alice" }], { |
| 143 | + hasNextPage: false, |
| 144 | + hasPreviousPage: false, |
| 145 | + startCursor: "cursor_start", |
| 146 | + endCursor: "cursor_end", |
| 147 | + }), |
| 148 | + ), |
| 149 | + ); |
| 150 | + const client = new PlainClient({ apiKey: "test-key" }); |
| 151 | + |
| 152 | + const connection = await client.query.customers({ first: 10 }); |
| 153 | + const nextPage = await connection.fetchNext(); |
| 154 | + |
| 155 | + expect(nextPage).toBeUndefined(); |
| 156 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 157 | + }); |
| 158 | + |
| 159 | + it("fetchPrevious sends request with before cursor", async () => { |
| 160 | + fetchMock = mockFetch(); |
| 161 | + // First call returns a page with hasPreviousPage: true |
| 162 | + fetchMock.mockResolvedValueOnce( |
| 163 | + graphqlResponse( |
| 164 | + makeCustomersResponse([{ id: "c_2", fullName: "Bob Jones", shortName: "Bob" }], { |
| 165 | + hasNextPage: false, |
| 166 | + hasPreviousPage: true, |
| 167 | + startCursor: "cursor_start", |
| 168 | + endCursor: "cursor_end", |
| 169 | + }), |
| 170 | + ), |
| 171 | + ); |
| 172 | + // Second call: previous page |
| 173 | + fetchMock.mockResolvedValueOnce( |
| 174 | + graphqlResponse( |
| 175 | + makeCustomersResponse([{ id: "c_1", fullName: "Alice Smith", shortName: "Alice" }], { |
| 176 | + hasNextPage: true, |
| 177 | + hasPreviousPage: false, |
| 178 | + startCursor: "cursor_first", |
| 179 | + endCursor: "cursor_first", |
| 180 | + }), |
| 181 | + ), |
| 182 | + ); |
| 183 | + |
| 184 | + const client = new PlainClient({ apiKey: "test-key" }); |
| 185 | + const page = await client.query.customers({ first: 1 }); |
| 186 | + const prevPage = await page.fetchPrevious(); |
| 187 | + |
| 188 | + expect(prevPage).toBeInstanceOf(PlainConnection); |
| 189 | + expect(prevPage!.nodes[0].id).toBe("c_1"); |
| 190 | + |
| 191 | + const secondCallBody = getRequestBody(fetchMock, 1); |
| 192 | + expect(secondCallBody.variables.before).toBe("cursor_start"); |
| 193 | + }); |
| 194 | +}); |
0 commit comments