Added mockContext and mockCanvas objects

This commit is contained in:
Michael Li 2025-01-10 16:25:23 -05:00 committed by Sirz Benjie
parent 6049f6a776
commit bc2ebc7967
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
4 changed files with 29 additions and 12 deletions

View File

@ -0,0 +1,11 @@
import { mockContext } from "#test/utils/mocks/mockContext";
/**
* A minimal stub object to mock HTMLCanvasElement
*/
export const mockCanvas: any = {
width: 0,
getContext() {
return mockContext;
},
};

View File

@ -0,0 +1,15 @@
import { mockCanvas } from "#test/utils/mocks/mockCanvas";
/**
* A minimal stub object to mock CanvasRenderingContext2D
*/
export const mockContext: any = {
font: "",
//@ts-ignore
measureText: () => new TextMetrics(""),
save: () => {},
scale: () => {},
clearRect: () => {},
canvas: mockCanvas,
restore: () => {},
};

View File

@ -14,12 +14,13 @@ 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 { mockContext } from "#test/testUtils/mocks/mockContext";
import { MockFetch } from "#test/testUtils/mocks/mockFetch";
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()`).
@ -77,6 +78,7 @@ export function initTestFile() {
Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative;
HTMLCanvasElement.prototype.getContext = () => mockContext;
// Initialize all of these things if and only if they have not been initialized yet
if (allMoves.length === 0) {

View File

@ -6,17 +6,6 @@ export default defineProject(({ mode }) => ({
test: {
testTimeout: 20000,
setupFiles: ["./test/fontFace.setup.ts", "./test/vitest.setup.ts"],
server: {
deps: {
inline: ["vitest-canvas-mock"],
//@ts-ignore
optimizer: {
web: {
include: ["vitest-canvas-mock"],
},
},
},
},
environment: "jsdom" as const,
environmentOptions: {
jsdom: {