From 21c111b18c2517f36807a213a99ad57372aa6772 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 10 Jan 2025 15:41:44 -0500 Subject: [PATCH] Moved test file initialization code to its own function --- test/utils/gameWrapper.ts | 50 +-------------- test/utils/testFileInitialization.ts | 96 ++++++++++++++++++++++++++++ test/vitest.setup.ts | 36 +---------- 3 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 test/utils/testFileInitialization.ts diff --git a/test/utils/gameWrapper.ts b/test/utils/gameWrapper.ts index 0311a5911a0..4d8268d2fa8 100644 --- a/test/utils/gameWrapper.ts +++ b/test/utils/gameWrapper.ts @@ -14,7 +14,6 @@ import MockImage from "#test/utils/mocks/mocksContainer/mockImage"; import MockTextureManager from "#test/utils/mocks/mockTextureManager"; import fs from "fs"; import Phaser from "phaser"; -import InputText from "phaser3-rex-plugins/plugins/inputtext"; import { vi } from "vitest"; import { MockGameObjectCreator } from "./mocks/mockGameObjectCreator"; import InputManager = Phaser.Input.InputManager; @@ -26,54 +25,7 @@ import UpdateList = Phaser.GameObjects.UpdateList; import { version } from "../../package.json"; import { MockTimedEventManager } from "./mocks/mockTimedEventManager"; -Object.defineProperty(window, "localStorage", { - value: mockLocalStorage(), -}); -Object.defineProperty(window, "console", { - value: mockConsoleLog(false), -}); - - -InputText.prototype.setElement = () => null; -InputText.prototype.resize = () => null; -Phaser.GameObjects.Image = MockImage; -window.URL.createObjectURL = (blob: Blob) => { - blobToString(blob).then((data: string) => { - localStorage.setItem("toExport", data); - }); - return null; -}; -navigator.getGamepads = () => []; -global.fetch = vi.fn(MockFetch); -Utils.setCookie(Utils.sessionIdKey, 'fake_token'); - - -window.matchMedia = () => ({ - matches: false, -}); - - -/** - * Sets this object's position relative to another object with a given offset - * @param guideObject {@linkcode Phaser.GameObjects.GameObject} to base the position off of - * @param x The relative x position - * @param y The relative y position - */ -const setPositionRelative = function (guideObject: any, x: number, y: number) { - const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); - const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); - this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); -}; - -Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Image.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative; - - -export default class GameWrapper { +export class GameWrapper { public game: Phaser.Game; public scene: BattleScene; diff --git a/test/utils/testFileInitialization.ts b/test/utils/testFileInitialization.ts new file mode 100644 index 00000000000..19e9255ebb2 --- /dev/null +++ b/test/utils/testFileInitialization.ts @@ -0,0 +1,96 @@ +import { initLoggedInUser } from "#app/account"; +import { SESSION_ID_COOKIE } from "#app/constants"; +import { initAbilities } from "#app/data/all-abilities"; +import { allMoves, initMoves } from "#app/data/all-moves"; +import { initBiomes } from "#app/data/balance/biomes"; +import { initEggMoves } from "#app/data/balance/egg-moves"; +import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions"; +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 { setCookie } from "#app/utils"; +import { blobToString } from "#test/testUtils/gameManagerUtils"; +import { mockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog"; +import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage"; +import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; +import Phaser from "phaser"; +import InputText from "phaser3-rex-plugins/plugins/inputtext"; +import { vi } from "vitest"; +import { MockFetch } from "./mocks/mockFetch"; + +/** + * An initialization function that is run at the beginning of every test file (via `beforeAll()`). + */ +export function initTestFile() { + // Set the timezone to UTC for tests. + process.env.TZ = "UTC"; + + Object.defineProperty(window, "localStorage", { + value: mockLocalStorage(), + }); + Object.defineProperty(window, "console", { + value: mockConsoleLog(false), + }); + Object.defineProperty(document, "fonts", { + writable: true, + value: { + add: () => {}, + }, + }); + + InputText.prototype.setElement = () => null as any; + InputText.prototype.resize = () => null as any; + Phaser.GameObjects.Image = MockImage as any; + window.URL.createObjectURL = (blob: Blob) => { + blobToString(blob).then((data: string) => { + localStorage.setItem("toExport", data); + }); + return null as any; + }; + navigator.getGamepads = () => []; + global.fetch = vi.fn(MockFetch) as any; + setCookie(SESSION_ID_COOKIE, "fake_token"); + + window.matchMedia = () => + ({ + matches: false, + }) as any; + + /** + * Sets this object's position relative to another object with a given offset + * @param guideObject {@linkcode Phaser.GameObjects.GameObject} to base the position off of + * @param x The relative x position + * @param y The relative y position + */ + const setPositionRelative = function (guideObject: any, x: number, y: number) { + const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); + const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); + this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); + }; + + Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Image.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative; + + // Initialize all of these things if and only if they have not been initialized yet + if (allMoves.length === 0) { + initMoves(); + initVouchers(); + initAchievements(); + initStatsKeys(); + initPokemonPrevolutions(); + initBiomes(); + initEggMoves(); + initPokemonForms(); + initSpecies(); + initAbilities(); + initLoggedInUser(); + initMysteryEncounters(); + } +} diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index 44a304f52f2..9350f7f0eaf 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -1,21 +1,7 @@ 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"; +import { initTestFile } from "#test/utils/testFileInitialization"; /** Mock the override import to always return default values, ignoring any custom overrides. */ vi.mock("#app/overrides", async (importOriginal) => { @@ -63,28 +49,10 @@ vi.mock("i18next", async (importOriginal) => { 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: () => {}, - }, - }); + initTestFile(); }); afterAll(() => {