|
| 1 | +/// <reference types="bun-types/test-globals" /> |
| 2 | +import { test, expect, describe, jest, afterEach, beforeEach } from "bun:test"; |
| 3 | +import { collect } from "./APIService"; |
| 4 | + |
| 5 | +// --- Code to Test --- |
| 6 | +// (Assuming this is imported from './collect.ts') |
| 7 | +// I'm including it here for a self-contained example. |
| 8 | + |
| 9 | +// Helper types (assuming definitions based on the function) |
| 10 | +declare namespace API { |
| 11 | + export interface PaginatedResponse { |
| 12 | + links?: { |
| 13 | + next?: string | null; |
| 14 | + }; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// This is a guess at the RequestResult type based on the function's usage |
| 19 | +type RequestResult<TData, TError, ThrowOnError> = |
| 20 | + | { data: TData; error?: undefined } |
| 21 | + | { data: undefined; error: TError }; |
| 22 | + |
| 23 | +// --- Test Setup --- |
| 24 | + |
| 25 | +// Define a concrete type for our tests |
| 26 | +interface Device { |
| 27 | + id: number; |
| 28 | + name: string; |
| 29 | +} |
| 30 | + |
| 31 | +// Define concrete response types for our mock |
| 32 | +type DeviceResponse = API.PaginatedResponse & { data: Device[] }; |
| 33 | +type PaginatedData = Record<number, DeviceResponse>; |
| 34 | +type PaginatedError = Record<number, { message: string }>; |
| 35 | + |
| 36 | +// Mock the non-standard URL.parse function |
| 37 | +// We'll make it behave like `new URL()` for the test |
| 38 | +const mockUrlParse = jest.fn((url: string) => { |
| 39 | + // Handle the '?? ""' case, which would throw in `new URL()` |
| 40 | + if (!url) { |
| 41 | + return { searchParams: new URLSearchParams() }; |
| 42 | + } |
| 43 | + // Use the standard URL object to parse the URL string |
| 44 | + return new URL(url); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("collect", () => { |
| 48 | + // Spy on the global URL object to mock 'parse' |
| 49 | + let urlParseSpy: any; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + // Mock URL.parse before each test |
| 53 | + urlParseSpy = jest |
| 54 | + .spyOn(globalThis.URL, "parse") |
| 55 | + .mockImplementation(mockUrlParse as any); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + // Restore the original implementation and clear mocks |
| 60 | + urlParseSpy.mockRestore(); |
| 61 | + jest.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + // Test 1: Single page of data |
| 65 | + test("should collect data from a single page", async () => { |
| 66 | + const page1Data: Device[] = [{ id: 1, name: "Device A" }]; |
| 67 | + const mockCall = jest.fn().mockResolvedValue({ |
| 68 | + data: { |
| 69 | + data: page1Data, |
| 70 | + links: { next: null }, // No next page |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const result = await collect( |
| 75 | + mockCall as any, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(result).toEqual(page1Data); |
| 79 | + expect(mockCall).toHaveBeenCalledTimes(1); |
| 80 | + expect(mockCall).toHaveBeenCalledWith(undefined); // First call has no cursor |
| 81 | + }); |
| 82 | + |
| 83 | + // Test 2: Multiple pages of data |
| 84 | + test("should collect and concatenate data from multiple pages", async () => { |
| 85 | + const page1Data: Device[] = [{ id: 1, name: "Device A" }]; |
| 86 | + const page2Data: Device[] = [{ id: 2, name: "Device B" }]; |
| 87 | + |
| 88 | + const mockCall = jest |
| 89 | + .fn() |
| 90 | + // First call response |
| 91 | + .mockResolvedValueOnce({ |
| 92 | + data: { |
| 93 | + data: page1Data, |
| 94 | + links: { next: "https://api.example.com/devices?cursor=page2" }, |
| 95 | + }, |
| 96 | + }) |
| 97 | + // Second call response |
| 98 | + .mockResolvedValueOnce({ |
| 99 | + data: { |
| 100 | + data: page2Data, |
| 101 | + links: { next: null }, // No more pages |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + const result = await collect( |
| 106 | + mockCall as any, |
| 107 | + ); |
| 108 | + |
| 109 | + // Check final concatenated data |
| 110 | + expect(result).toEqual([ |
| 111 | + { id: 1, name: "Device A" }, |
| 112 | + { id: 2, name: "Device B" }, |
| 113 | + ]); |
| 114 | + |
| 115 | + // Check that the mock was called correctly |
| 116 | + expect(mockCall).toHaveBeenCalledTimes(2); |
| 117 | + expect(mockCall).toHaveBeenNthCalledWith(1, undefined); |
| 118 | + expect(mockCall).toHaveBeenNthCalledWith(2, "page2"); |
| 119 | + |
| 120 | + // Check that our URL.parse mock was used |
| 121 | + expect(mockUrlParse).toHaveBeenCalledWith( |
| 122 | + "https://api.example.com/devices?cursor=page2", |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + // Test 3: Empty response |
| 127 | + test("should return an empty array if no data is found", async () => { |
| 128 | + const mockCall = jest.fn().mockResolvedValue({ |
| 129 | + data: { |
| 130 | + data: [], // Empty data array |
| 131 | + links: { next: null }, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + const result = await collect( |
| 136 | + mockCall as any, |
| 137 | + ); |
| 138 | + |
| 139 | + expect(result).toEqual([]); |
| 140 | + expect(mockCall).toHaveBeenCalledTimes(1); |
| 141 | + }); |
| 142 | + |
| 143 | + // Test 4: API Error |
| 144 | + test("should return an Error if the API call fails", async () => { |
| 145 | + const apiError = { message: "Internal Server Error" }; |
| 146 | + const mockCall = jest.fn().mockResolvedValue({ |
| 147 | + data: undefined, |
| 148 | + error: apiError, |
| 149 | + }); |
| 150 | + |
| 151 | + const result = await collect( |
| 152 | + mockCall as any, |
| 153 | + ); |
| 154 | + |
| 155 | + expect(result).toBeInstanceOf(Error); |
| 156 | + expect(mockCall).toHaveBeenCalledTimes(1); |
| 157 | + }); |
| 158 | + |
| 159 | + // Test 5: Gracefully handles empty 'next' string |
| 160 | + test("should stop paginating if 'next' link is an empty string", async () => { |
| 161 | + const page1Data: Device[] = [{ id: 1, name: "Device A" }]; |
| 162 | + const mockCall = jest.fn().mockResolvedValue({ |
| 163 | + data: { |
| 164 | + data: page1Data, |
| 165 | + links: { next: "" }, // Empty string |
| 166 | + }, |
| 167 | + }); |
| 168 | + |
| 169 | + const result = await collect( |
| 170 | + mockCall as any, |
| 171 | + ); |
| 172 | + |
| 173 | + expect(result).toEqual(page1Data); |
| 174 | + expect(mockCall).toHaveBeenCalledTimes(1); |
| 175 | + expect(mockUrlParse).toHaveBeenCalledWith(""); // Our mock handles this |
| 176 | + }); |
| 177 | +}); |
0 commit comments