Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jespertheend committed Sep 24, 2024
1 parent b5f057a commit 70cf790
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
25 changes: 21 additions & 4 deletions test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { assertEquals, assertRejects, assertStrictEquals } from "std/testing/asserts.ts";
import { CustomMaterialData, Entity, Material, MaterialMap, Mesh, ShaderSource, VertexState, WebGlMaterialConfig, WebGlMaterialMapType, WebGlRenderer, WebGlRendererError } from "../../../../../../src/mod.js";
import { assertHasSingleContext, runWithWebGlMocksAsync, setWebGlContextSupported } from "./shared/webGlMocks.js";
import { assertHasSingleWebGlContext, runWithWebGlMocksAsync, setWebGlContextSupported } from "./shared/webGlMocks.js";
import { assertIsType, testTypes } from "../../../../shared/typeAssertions.js";
import { createCam, createCubeEntity, createVertexState } from "../shared/sceneUtil.js";
import { assertLogEntryEquals, assertLogEquals } from "./shared/WebGlCommandLog.js";
import { assertHasSingle2dRenderingContext } from "./shared/RenderingContext2d.js";
import { assertSpyCall, assertSpyCalls, spy } from "std/testing/mock.ts";

async function basicRendererSetup() {
const renderer = new WebGlRenderer();
Expand All @@ -15,7 +17,7 @@ async function basicRendererSetup() {
const scene = new Entity();
scene.add(cam);

const context = assertHasSingleContext();
const context = assertHasSingleWebGlContext();

return { renderer, domTarget, camComponent, scene, ...context };
}
Expand Down Expand Up @@ -88,10 +90,25 @@ Deno.test({

const domTarget = renderer.createDomTarget();

const context2d = assertHasSingle2dRenderingContext();
const { commandLog, canvas: webGlCanvas } = assertHasSingleWebGlContext();

const clearRectSpy = spy(context2d, "clearRect");
const drawImageSpy = spy(context2d, "drawImage");

const { camComponent } = createCam();
domTarget.render(camComponent);

const { commandLog } = assertHasSingleContext();
assertSpyCalls(clearRectSpy, 1);
assertSpyCall(clearRectSpy, 0, {
args: [0, 0, 300, 150],
});
assertSpyCalls(drawImageSpy, 1);
assertEquals(drawImageSpy.calls[0].args.length, 3);
assertStrictEquals(drawImageSpy.calls[0].args[0], webGlCanvas);
assertEquals(drawImageSpy.calls[0].args[1], 0);
assertEquals(drawImageSpy.calls[0].args[2], 0);

commandLog.assertCount(5);

commandLog.assertLogEquals([
Expand Down Expand Up @@ -127,7 +144,7 @@ Deno.test({
const renderer = new WebGlRenderer();
await renderer.init();

const { commandLog, canvas } = assertHasSingleContext();
const { commandLog, canvas } = assertHasSingleWebGlContext();

const domTargetA = renderer.createDomTarget();
domTargetA.resize(100, 200);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { assertEquals } from "std/testing/asserts.ts";

export class RenderingContext2d {
/**
* @param {CanvasImageSource} image
* @param {number} dx
* @param {number} dy
*/
drawImage(image, dx, dy) {}

clearRect() {}
}

/** @type {RenderingContext2d[]} */
let created2dRenderingContexts = [];

/**
* @param {{width: number, height: number}} canvas
*/
export function create2dRenderingContext(canvas) {
const context = new RenderingContext2d();
created2dRenderingContexts.push(context);
return context;
}

export function assertHasSingle2dRenderingContext() {
assertEquals(created2dRenderingContexts.length, 1, 'Expected to have exactly one canvas with getContext("2d")');
return created2dRenderingContexts[0];
}

export function clearCreated2dRenderingContexts() {
created2dRenderingContexts = [];
}
5 changes: 3 additions & 2 deletions test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { create2dRenderingContext } from "./RenderingContext2d.js";
import { clearCreated2dRenderingContexts, create2dRenderingContext } from "./RenderingContext2d.js";
import { createWebGlRenderingContext } from "./WebGlRenderingContext.js";

const oldDocument = globalThis.document;
Expand Down Expand Up @@ -54,6 +54,7 @@ export function installWebGlMocks() {
export function uninstallWebGlMocks() {
globalThis.document = oldDocument;
installed = false;
clearCreated2dRenderingContexts();
}

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ export function set2dContextSupported(supported) {
context2dSupported = supported;
}

export function assertHasSingleContext() {
export function assertHasSingleWebGlContext() {
if (createdContexts.length != 1) {
throw new Error("Expected exactly one webgl context to be created");
}
Expand Down

0 comments on commit 70cf790

Please sign in to comment.