pokerogue/src/test/vitest.setup.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

92 lines
2.8 KiB
TypeScript

import "vitest-canvas-mock";
import { initLoggedInUser } from "#app/account";
import { initAbilities } from "#app/data/ability";
import { initBiomes } from "#app/data/balance/biomes";
import { initEggMoves } from "#app/data/balance/egg-moves";
import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions";
import { initMoves } from "#app/data/move";
import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters";
import { initPokemonForms } from "#app/data/pokemon-forms";
import { initSpecies } from "#app/data/pokemon-species";
import { initAchievements } from "#app/system/achv";
import { initVouchers } from "#app/system/voucher";
import { initStatsKeys } from "#app/ui/game-stats-ui-handler";
import { afterAll, beforeAll, vi } from "vitest";
/** Set the timezone to UTC for tests. */
process.env.TZ = "UTC";
/** Mock the override import to always return default values, ignoring any custom overrides. */
vi.mock("#app/overrides", async (importOriginal) => {
const { defaultOverrides } = await importOriginal<typeof import("#app/overrides")>();
return {
default: defaultOverrides,
defaultOverrides,
} satisfies typeof import("#app/overrides");
});
/**
* This is a hacky way to mock the i18n backend requests (with the help of {@link https://mswjs.io/ | msw}).
* The reason to put it inside of a mock is to elevate it.
* This is necessary because how our code is structured.
* Do NOT try to put any of this code into external functions, it won't work as it's elevated during runtime.
*/
vi.mock("i18next", async (importOriginal) => {
console.log("Mocking i18next");
const { setupServer } = await import("msw/node");
const { http, HttpResponse } = await import("msw");
global.server = setupServer(
http.get("/locales/en/*", async (req) => {
const filename = req.params[0];
try {
const json = await import(`../../public/locales/en/${req.params[0]}`);
console.log("Loaded locale", filename);
return HttpResponse.json(json);
} catch (err) {
console.log(`Failed to load locale ${filename}!`, err);
return HttpResponse.json({});
}
}),
http.get("https://fonts.googleapis.com/*", () => {
return HttpResponse.text("");
}),
);
global.server.listen({ onUnhandledRequest: "error" });
console.log("i18n MSW server listening!");
return await importOriginal();
});
initVouchers();
initAchievements();
initStatsKeys();
initPokemonPrevolutions();
initBiomes();
initEggMoves();
initPokemonForms();
initSpecies();
initMoves();
initAbilities();
initLoggedInUser();
initMysteryEncounters();
global.testFailed = false;
beforeAll(() => {
Object.defineProperty(document, "fonts", {
writable: true,
value: {
add: () => {},
},
});
});
afterAll(() => {
global.server.close();
console.log("Closing i18n MSW server!");
});