brackt/app/services/standings-sync/__tests__/epl.test.ts
2026-05-07 11:48:57 -07:00

100 lines
2.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { EplStandingsAdapter } from "../epl";
const SAMPLE_RESPONSE = {
standings: [
{
type: "TOTAL",
table: [
{
position: 1,
team: { id: 57, name: "Arsenal FC" },
playedGames: 30,
form: "W,W,D,W,L",
won: 21,
draw: 6,
lost: 3,
points: 69,
goalsFor: 72,
goalsAgainst: 25,
goalDifference: 47,
},
{
position: 2,
team: { id: 65, name: "Manchester City FC" },
playedGames: 30,
form: "W,W,W,D,W",
won: 20,
draw: 7,
lost: 3,
points: 67,
goalsFor: 70,
goalsAgainst: 28,
goalDifference: 42,
},
],
},
{ type: "HOME", table: [] },
],
};
describe("EplStandingsAdapter", () => {
beforeEach(() => {
vi.stubGlobal("fetch", vi.fn());
});
afterEach(() => {
vi.restoreAllMocks();
});
it("maps football-data.org TOTAL standings to FetchedStandingsRecord[]", async () => {
vi.mocked(fetch).mockResolvedValueOnce({
ok: true,
json: async () => SAMPLE_RESPONSE,
} as Response);
const records = await new EplStandingsAdapter("test-token").fetchStandings();
expect(records).toHaveLength(2);
const arsenal = records[0];
expect(arsenal.teamName).toBe("Arsenal FC");
expect(arsenal.externalTeamId).toBe("57");
expect(arsenal.wins).toBe(21);
expect(arsenal.ties).toBe(6);
expect(arsenal.losses).toBe(3);
expect(arsenal.gamesPlayed).toBe(30);
expect(arsenal.tablePoints).toBe(69);
expect(arsenal.goalsFor).toBe(72);
expect(arsenal.goalsAgainst).toBe(25);
expect(arsenal.goalDifference).toBe(47);
expect(arsenal.leagueRank).toBe(1);
expect(arsenal.lastTen).toBe("W,W,D,W,L");
});
it("sends the football-data.org auth token", async () => {
vi.mocked(fetch).mockResolvedValueOnce({
ok: true,
json: async () => SAMPLE_RESPONSE,
} as Response);
await new EplStandingsAdapter("abc123").fetchStandings();
expect(fetch).toHaveBeenCalledWith(
"https://api.football-data.org/v4/competitions/PL/standings",
{ headers: { "X-Auth-Token": "abc123" } }
);
});
it("throws when no API key is configured", async () => {
await expect(new EplStandingsAdapter("").fetchStandings()).rejects.toThrow(/FOOTBALL_DATA_API_KEY/);
});
it("throws on non-ok response", async () => {
vi.mocked(fetch).mockResolvedValueOnce({
ok: false,
status: 429,
statusText: "Too Many Requests",
} as Response);
await expect(new EplStandingsAdapter("test-token").fetchStandings()).rejects.toThrow("429");
});
});