|
| 1 | +import { describe, it, expect, beforeEach, jest } from "@jest/globals"; |
| 2 | +import { createRegionCountApi } from "./region-count-api.ts"; |
| 3 | +import type { RegionCount } from "../../../../domain/types.ts"; |
| 4 | + |
| 5 | +type MockResponse = Pick<Response, "ok" | "status" | "json">; |
| 6 | + |
| 7 | +let fetchSpy: jest.MockedFunction<typeof fetch>; |
| 8 | + |
| 9 | +const API_BASE = "http://localhost:8700"; |
| 10 | +const ENDPOINT = `${API_BASE}/api/v1/heritages/region-count`; |
| 11 | + |
| 12 | +const makeOkResponse = (body: unknown): MockResponse => ({ |
| 13 | + ok: true, |
| 14 | + status: 200, |
| 15 | + json: async () => body, |
| 16 | +}); |
| 17 | + |
| 18 | +const makeNgResponse = (status: number): MockResponse => ({ |
| 19 | + ok: false, |
| 20 | + status, |
| 21 | + json: async () => ({}), |
| 22 | +}); |
| 23 | + |
| 24 | +const makeRegionCountResponse = (data: RegionCount[]) => ({ |
| 25 | + status: "success", |
| 26 | + data, |
| 27 | +}); |
| 28 | + |
| 29 | +const MOCK_REGION_COUNTS: RegionCount[] = [ |
| 30 | + { region: "Africa", count: 92 }, |
| 31 | + { region: "Asia", count: 263 }, |
| 32 | + { region: "Europe", count: 542 }, |
| 33 | + { region: "North America", count: 87 }, |
| 34 | + { region: "South America", count: 103 }, |
| 35 | + { region: "Oceania", count: 35 }, |
| 36 | +]; |
| 37 | + |
| 38 | +describe("createRegionCountApi", () => { |
| 39 | + let api: ReturnType<typeof createRegionCountApi>; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + fetchSpy = jest.fn() as jest.MockedFunction<typeof fetch>; |
| 43 | + api = createRegionCountApi({ apiBase: API_BASE, fetchImpl: fetchSpy }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("fetchRegionCount", () => { |
| 47 | + it("calls the correct endpoint", async () => { |
| 48 | + fetchSpy.mockResolvedValue( |
| 49 | + makeOkResponse(makeRegionCountResponse(MOCK_REGION_COUNTS)) as Response, |
| 50 | + ); |
| 51 | + |
| 52 | + await api.fetchRegionCount(); |
| 53 | + |
| 54 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 55 | + ENDPOINT, |
| 56 | + expect.objectContaining({ |
| 57 | + headers: expect.objectContaining({ Accept: "application/json" }), |
| 58 | + }), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns RegionCount[] when status is success", async () => { |
| 63 | + fetchSpy.mockResolvedValue( |
| 64 | + makeOkResponse(makeRegionCountResponse(MOCK_REGION_COUNTS)) as Response, |
| 65 | + ); |
| 66 | + |
| 67 | + const result = await api.fetchRegionCount(); |
| 68 | + |
| 69 | + expect(result).toEqual(MOCK_REGION_COUNTS); |
| 70 | + }); |
| 71 | + |
| 72 | + it("passes signal when provided", async () => { |
| 73 | + fetchSpy.mockResolvedValue( |
| 74 | + makeOkResponse(makeRegionCountResponse(MOCK_REGION_COUNTS)) as Response, |
| 75 | + ); |
| 76 | + |
| 77 | + const abortController = new AbortController(); |
| 78 | + await api.fetchRegionCount({ signal: abortController.signal }); |
| 79 | + |
| 80 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 81 | + ENDPOINT, |
| 82 | + expect.objectContaining({ signal: abortController.signal }), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("throws on HTTP error", async () => { |
| 87 | + fetchSpy.mockResolvedValue(makeNgResponse(500) as Response); |
| 88 | + |
| 89 | + await expect(api.fetchRegionCount()).rejects.toThrow("HTTP 500"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("throws when API status is not success", async () => { |
| 93 | + fetchSpy.mockResolvedValue(makeOkResponse({ status: "error", data: [] }) as Response); |
| 94 | + |
| 95 | + await expect(api.fetchRegionCount()).rejects.toThrow("API status is not success: error"); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments