From bf4f79379d724644b50383046635d9902ffccaac Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Wed, 9 Oct 2024 09:29:15 -0700 Subject: [PATCH] add pokerogue-daily-api test coverage --- .../plugins/api/pokerogue-daily-api.test.ts | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/test/plugins/api/pokerogue-daily-api.test.ts diff --git a/src/test/plugins/api/pokerogue-daily-api.test.ts b/src/test/plugins/api/pokerogue-daily-api.test.ts new file mode 100644 index 00000000000..449cf62f2f0 --- /dev/null +++ b/src/test/plugins/api/pokerogue-daily-api.test.ts @@ -0,0 +1,98 @@ +import type { GetDailyRankingsPageCountRequest, GetDailyRankingsRequest } from "#app/@types/PokerogueDailyApi"; +import { PokerogueDailyApi } from "#app/plugins/api/pokerogue-daily-api"; +import { getApiBaseUrl } from "#app/test/utils/testUtils"; +import { ScoreboardCategory, type RankingEntry } from "#app/ui/daily-run-scoreboard"; +import { http, HttpResponse } from "msw"; +import { setupServer } from "msw/node"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; + +const apiBase = getApiBaseUrl(); +const dailyApi = new PokerogueDailyApi(apiBase); +const server = setupServer(); + +beforeAll(() => { + server.listen({ onUnhandledRequest: "error" }); +}); + +afterAll(() => { + server.close(); +}); + +afterEach(() => { + server.resetHandlers(); +}); + +describe("Pokerogue Admin API", () => { + beforeEach(() => { + vi.spyOn(console, "warn"); + }); + + describe("Get Seed", () => { + it("should return seed string on SUCCESS", async () => { + server.use(http.get(`${apiBase}/daily/seed`, () => HttpResponse.text("this-is-a-test-seed"))); + + const seed = await dailyApi.getSeed(); + + expect(seed).toBe("this-is-a-test-seed"); + }); + + it("should return null and report a warning on ERROR", async () => { + server.use(http.get(`${apiBase}/daily/seed`, () => HttpResponse.error())); + + const seed = await dailyApi.getSeed(); + + expect(seed).toBeNull(); + expect(console.warn).toHaveBeenCalledWith("Could not get daily-run seed!", expect.any(Error)); + }); + }); + + describe("Get Rankings", () => { + const params: GetDailyRankingsRequest = { + category: ScoreboardCategory.DAILY, + }; + + it("should return ranking entries on SUCCESS", async () => { + const expectedRankings: RankingEntry[] = [ + { rank: 1, score: 999, username: "Player 1", wave: 200 }, + { rank: 2, score: 10, username: "Player 2", wave: 1 }, + ]; + server.use(http.get(`${apiBase}/daily/rankings`, () => HttpResponse.json(expectedRankings))); + + const rankings = await dailyApi.getRankings(params); + + expect(rankings).toStrictEqual(expectedRankings); + }); + + it("should return null and report a warning on ERROR", async () => { + server.use(http.get(`${apiBase}/daily/rankings`, () => HttpResponse.error())); + + const rankings = await dailyApi.getRankings(params); + + expect(rankings).toBeNull(); + expect(console.warn).toHaveBeenCalledWith("Could not get daily rankings!", expect.any(Error)); + }); + }); + + describe("Get Rankings Page Count", () => { + const params: GetDailyRankingsPageCountRequest = { + category: ScoreboardCategory.DAILY, + }; + + it("should return a number on SUCCESS", async () => { + server.use(http.get(`${apiBase}/daily/rankingpagecount`, () => HttpResponse.json(5))); + + const pageCount = await dailyApi.getRankingsPageCount(params); + + expect(pageCount).toBe(5); + }); + + it("should return 1 and report a warning on ERROR", async () => { + server.use(http.get(`${apiBase}/daily/rankingpagecount`, () => HttpResponse.error())); + + const pageCount = await dailyApi.getRankingsPageCount(params); + + expect(pageCount).toBe(1); + expect(console.warn).toHaveBeenCalledWith("Could not get daily rankings page count!", expect.any(Error)); + }); + }); +});