pokerogue/src/test/plugins/api/pokerogue-api.test.ts
flx-sta 7a0c88e661
[Refactor] Pokerogue API client (#4583)
* start migrating Utils.apiFetch to api class

* move dailyranking to api

* use api in title-ui-handler

* remove: Utils.apiFetch

* migrate `updateSystemSavedata` to api

* migrate clear session savedata to api

* migrate updateAllSavedata to api

* migrate `updateSessionSavedata` to api

* rename `api` to `pokerogue-api`

* migrate unlink discord to pokerogue-api

* migrate unlink google to pokerogue-api

* update pokerogue-api login

* migrate register account to pokerogue-api

* remove Utils.apiPost

* reset overrides.ts

* chore: cleanup

* fix env.development

* fix circular dependencies with api

* fix gamedata verify missing await

* fix daily api calls in daily-run-scorebard

* fix discord-link request body being empty

there was a double `toUrlSearchParams()` call involved

* add pokerogue-api test coverge

* add test-utils `getApiBaseUrl()` method

* add pokerogue-admin-api test coverage

* add pokerogue-account-api test coverage

* add pokerogue-daily-api test coverage

* add pokerogue-savedata-api test coverage

* fix some test describes

* add pokerogue-session-savedata-api test coverage

* add pokerogue-system-savedata-api test coverage

* fix tests

* fix tryExportData

thanks @MokaStitcher

* chore: fix menu-ui-handlers.ts

* fix admin-ui-handler (types)

* extend test-coverage for admin-api

* remove outdated code

* skip some clowning-around-encounter tests if events are active

this is not a permanent solution

* Update src/system/game-data.ts

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

* Revert "skip some clowning-around-encounter tests if events are active"

This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67.

* mark `localServerUrl` and `apiUrl` as deprecated

in `utils.ts`

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 15:57:21 -05:00

98 lines
3.4 KiB
TypeScript

import type { TitleStatsResponse } from "#app/@types/PokerogueApi";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import { getApiBaseUrl } from "#app/test/utils/testUtils";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const { server } = global;
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));
});
});
});