From 36d3d2c82c6721c5a5ea67bdd0697798b347585f Mon Sep 17 00:00:00 2001 From: zBlade Date: Sat, 28 Sep 2024 18:39:44 -0400 Subject: [PATCH] test: add EventManager tests --- dist/game/eMath.game.js | 2 +- dist/game/eMath.game.mjs | 2 +- dist/types/game/managers/EventManager.d.ts | 2 +- src/game/managers/EventManager.ts | 2 +- test/tests/game/managers/eventManager.test.ts | 99 +++++++++++++++++++ test/tests/game/setupGame.ts | 2 +- test/tests/shared/utils.ts | 4 + 7 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 test/tests/game/managers/eventManager.test.ts diff --git a/dist/game/eMath.game.js b/dist/game/eMath.game.js index 931a29f..cd9e28d 100644 --- a/dist/game/eMath.game.js +++ b/dist/game/eMath.game.js @@ -7394,7 +7394,7 @@ var EventManager = class _EventManager { } /** * Warps time by a certain amount. Note: This will affect the stored creation time of timeout events. - * @param dt - The time to warp by. + * @param dt - The time to warp by (in milliseconds). */ timeWarp(dt) { for (const event of Object.values(this.events)) { diff --git a/dist/game/eMath.game.mjs b/dist/game/eMath.game.mjs index 6b7d50a..f19d5e4 100644 --- a/dist/game/eMath.game.mjs +++ b/dist/game/eMath.game.mjs @@ -7360,7 +7360,7 @@ var EventManager = class _EventManager { } /** * Warps time by a certain amount. Note: This will affect the stored creation time of timeout events. - * @param dt - The time to warp by. + * @param dt - The time to warp by (in milliseconds). */ timeWarp(dt) { for (const event of Object.values(this.events)) { diff --git a/dist/types/game/managers/EventManager.d.ts b/dist/types/game/managers/EventManager.d.ts index 40c546f..a044487 100644 --- a/dist/types/game/managers/EventManager.d.ts +++ b/dist/types/game/managers/EventManager.d.ts @@ -178,7 +178,7 @@ declare class EventManager { changeFps(fps: number): void; /** * Warps time by a certain amount. Note: This will affect the stored creation time of timeout events. - * @param dt - The time to warp by. + * @param dt - The time to warp by (in milliseconds). */ timeWarp(dt: number): void; /** diff --git a/src/game/managers/EventManager.ts b/src/game/managers/EventManager.ts index 08a2778..c47255f 100644 --- a/src/game/managers/EventManager.ts +++ b/src/game/managers/EventManager.ts @@ -318,7 +318,7 @@ class EventManager { /** * Warps time by a certain amount. Note: This will affect the stored creation time of timeout events. - * @param dt - The time to warp by. + * @param dt - The time to warp by (in milliseconds). */ public timeWarp(dt: number): void { // Iterate through all events and warp the time. diff --git a/test/tests/game/managers/eventManager.test.ts b/test/tests/game/managers/eventManager.test.ts new file mode 100644 index 0000000..204caa5 --- /dev/null +++ b/test/tests/game/managers/eventManager.test.ts @@ -0,0 +1,99 @@ +/** + * @file Tests for the EventManager class + */ +import { describe, it } from "mocha"; +import { assert } from "chai"; + +import { EventInit, EventManager, EventTypes } from "emath.js/game"; + +// EventManager is a part of class +import { testGame } from "../setupGame"; + +describe("EventManager", () => { + describe("EventManager constructor", () => { + // The event manager should be created when the game is created + it("should create an event manager instance", () => { + assert.instanceOf(testGame.eventManager, EventManager); + }); + }); + + describe("on/dispatch", () => { + let eventDispatched = false; + + // Create a new event and listen to it + testGame.eventManager.on("test-event", () => { + eventDispatched = true; + }); + + // Dispatch the event + testGame.eventManager.dispatch("test-event"); + + // The event should have been dispatched + it("should dispatch an event", () => { + assert.isTrue(eventDispatched); + }); + }); + + describe("setEvent", () => { + let intervalEventDispatchedTimes = 0; + const intervalDtArray: number[] = []; + + // Set an interval event + testGame.eventManager.setEvent("testInterval1", "interval", 100, (dt) => { + intervalEventDispatchedTimes++; + intervalDtArray.push(dt); + }); + + testGame.eventManager.setEvent({ + name: "testInterval2", + type: EventTypes.interval, + delay: 100, + callback: (dt) => { + intervalEventDispatchedTimes++; + intervalDtArray.push(dt); + }, + } as EventInit); + + let timeoutEventDispatchedTimes = 0; + + // Set up a timeout event + testGame.eventManager.setEvent("testTimeout1", "timeout", 100, () => { + // eventDispatchedTimes++; + timeoutEventDispatchedTimes++; + }); + + testGame.eventManager.setEvent({ + name: "testTimeout2", + type: EventTypes.timeout, + delay: 100, + callback: () => { + // eventDispatchedTimes++; + timeoutEventDispatchedTimes++; + }, + } as EventInit); + + it("should dispatch interval events", (done) => { + setTimeout(() => { + // The interval event should have been dispatched + it("should dispatch interval events", () => { + assert.equal(intervalEventDispatchedTimes, 2); + }); + + // The interval event should have been dispatched with the correct dt + it("should dispatch interval events with the correct dt", () => { + assert.equal(intervalDtArray.length, 2); + intervalDtArray.forEach((dt) => { + assert.closeTo(dt, 100, 100); + }); + }); + + // The timeout event should have been dispatched + it("should dispatch timeout events", () => { + assert.equal(timeoutEventDispatchedTimes, 2); + }); + + done(); + }, 120); + }); + }); +}); diff --git a/test/tests/game/setupGame.ts b/test/tests/game/setupGame.ts index 8989520..4bc0f68 100644 --- a/test/tests/game/setupGame.ts +++ b/test/tests/game/setupGame.ts @@ -20,7 +20,7 @@ const gameConfig: GameConfigOptions = { framerate: 10, }, - // Seperate localStorage for testing in node + // Separate localStorage for testing in node localStorage: localStorageTest, }; diff --git a/test/tests/shared/utils.ts b/test/tests/shared/utils.ts index b83ac3b..dcbacce 100644 --- a/test/tests/shared/utils.ts +++ b/test/tests/shared/utils.ts @@ -7,6 +7,7 @@ import { assert } from "chai"; /** * Asserts that two Decimal instances are equal. + * @deprecated * @param e1 - The first Decimal instance. * @param e2 - The second Decimal instance. */ @@ -51,6 +52,9 @@ class LocalStorageTest implements Storage { } } +/** + * An instance of {@link LocalStorageTest} for testing. + */ const localStorageTest = new LocalStorageTest(); export { assertDecimal, LocalStorageTest, localStorageTest };