From 17d44dd125118509a6808633708ec422b2af7c78 Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Mon, 7 Oct 2024 11:47:20 -0700 Subject: [PATCH] add pokerogue-api test coverge --- src/plugins/api/pokerogue-api.ts | 2 +- src/test/plugins/api/pokerogue-api.test.ts | 106 +++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/test/plugins/api/pokerogue-api.test.ts diff --git a/src/plugins/api/pokerogue-api.ts b/src/plugins/api/pokerogue-api.ts index ed65dab775a..92d0ff1bbbb 100644 --- a/src/plugins/api/pokerogue-api.ts +++ b/src/plugins/api/pokerogue-api.ts @@ -68,7 +68,7 @@ export class PokerogueApi extends ApiBase { if (response.ok) { return true; } else { - console.warn(`Google Unlink failed (${response.status}: ${response.statusText})`); + console.warn(`Google unlink failed (${response.status}: ${response.statusText})`); } } catch (err) { console.warn("Could not unlink Google!", err); diff --git a/src/test/plugins/api/pokerogue-api.test.ts b/src/test/plugins/api/pokerogue-api.test.ts new file mode 100644 index 00000000000..0c85e039b9a --- /dev/null +++ b/src/test/plugins/api/pokerogue-api.test.ts @@ -0,0 +1,106 @@ +import type { TitleStatsResponse } from "#app/@types/PokerogueApi"; +import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; +import { http, HttpResponse } from "msw"; +import { setupServer } from "msw/node"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; + +const apiBase = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8001"; +const server = setupServer(); + +beforeAll(() => { + server.listen({ onUnhandledRequest: "error" }); +}); + +afterAll(() => { + server.close(); +}); + +afterEach(() => { + server.resetHandlers(); +}); + +describe("Pokerogue API", () => { + beforeEach(() => { + vi.spyOn(console, "warn"); + }); + + describe("Game Title Stats", () => { + const expectedTitleStats: TitleStatsResponse = { + playerCount: 9999999, + battleCount: 9999999, + }; + + it("should return the stats on SUCCESS", async () => { + server.use(http.get(`${apiBase}/game/titlestats`, () => HttpResponse.json(expectedTitleStats))); + + const titleStats = await pokerogueApi.getGameTitleStats(); + + expect(titleStats).toEqual(expectedTitleStats); + }); + + it("should return null and report a warning on ERROR", async () => { + server.use(http.get(`${apiBase}/game/titlestats`, () => HttpResponse.error())); + const titleStats = await pokerogueApi.getGameTitleStats(); + + expect(titleStats).toBeNull(); + expect(console.warn).toHaveBeenCalledWith("Could not get game title stats!", expect.any(Error)); + }); + }); + + describe("Unlink Discord", () => { + it("should return true on SUCCESS", async () => { + server.use(http.post(`${apiBase}/auth/discord/logout`, () => new HttpResponse("", { status: 200 }))); + + const success = await pokerogueApi.unlinkDiscord(); + + expect(success).toBe(true); + }); + + it("should return false and report a warning on FAILURE", async () => { + server.use(http.post(`${apiBase}/auth/discord/logout`, () => new HttpResponse("", { status: 401 }))); + + const success = await pokerogueApi.unlinkDiscord(); + + expect(success).toBe(false); + expect(console.warn).toHaveBeenCalledWith("Discord unlink failed (401: Unauthorized)"); + }); + + it("should return false and report a warning on ERROR", async () => { + server.use(http.post(`${apiBase}/auth/discord/logout`, () => HttpResponse.error())); + + const success = await pokerogueApi.unlinkDiscord(); + + expect(success).toBe(false); + expect(console.warn).toHaveBeenCalledWith("Could not unlink Discord!", expect.any(Error)); + }); + }); + + describe("Unlink Google", () => { + it("should return true on SUCCESS", async () => { + server.use(http.post(`${apiBase}/auth/google/logout`, () => new HttpResponse("", { status: 200 }))); + + const success = await pokerogueApi.unlinkGoogle(); + + expect(success).toBe(true); + }); + + + it("should return false and report a warning on FAILURE", async () => { + server.use(http.post(`${apiBase}/auth/google/logout`, () => new HttpResponse("", { status: 401 }))); + + const success = await pokerogueApi.unlinkGoogle(); + + expect(success).toBe(false); + expect(console.warn).toHaveBeenCalledWith("Google unlink failed (401: Unauthorized)"); + }); + + it("should return false and report a warning on ERROR", async () => { + server.use(http.post(`${apiBase}/auth/google/logout`, () => HttpResponse.error())); + + const success = await pokerogueApi.unlinkGoogle(); + + expect(success).toBe(false); + expect(console.warn).toHaveBeenCalledWith("Could not unlink Google!", expect.any(Error)); + }); + }); +});