Skip to content

Commit

Permalink
test: add EventManager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xShadowBlade committed Sep 28, 2024
1 parent d016635 commit 36d3d2c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/types/game/managers/EventManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ declare class EventManager<Events extends string = string> {
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;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/game/managers/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class EventManager<Events extends string = string> {

/**
* 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.
Expand Down
99 changes: 99 additions & 0 deletions test/tests/game/managers/eventManager.test.ts
Original file line number Diff line number Diff line change
@@ -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 <Game> 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);
});
});
});
2 changes: 1 addition & 1 deletion test/tests/game/setupGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const gameConfig: GameConfigOptions = {
framerate: 10,
},

// Seperate localStorage for testing in node
// Separate localStorage for testing in node
localStorage: localStorageTest,
};

Expand Down
4 changes: 4 additions & 0 deletions test/tests/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -51,6 +52,9 @@ class LocalStorageTest implements Storage {
}
}

/**
* An instance of {@link LocalStorageTest} for testing.
*/
const localStorageTest = new LocalStorageTest();

export { assertDecimal, LocalStorageTest, localStorageTest };

0 comments on commit 36d3d2c

Please sign in to comment.