From 94a925944d664d8e32f6da0dcedc3f1f414f7fb0 Mon Sep 17 00:00:00 2001 From: Marco Alka Date: Thu, 4 Apr 2024 22:16:27 +0200 Subject: [PATCH] add test for entity added internal event --- src/events/internal-events.test.ts | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/events/internal-events.test.ts diff --git a/src/events/internal-events.test.ts b/src/events/internal-events.test.ts new file mode 100644 index 0000000..906645f --- /dev/null +++ b/src/events/internal-events.test.ts @@ -0,0 +1,61 @@ +import {buildWorld} from "../ecs/ecs-world"; +import {Actions, createSystem, ReadEvents} from "../system/system"; +import {SimECSAddEntityEvent} from "./internal-events"; +import {Entity} from "../entity/entity"; +import {assert} from "chai"; + +describe('Test internal events', () => { + it('Entity added', async () => { + // Count the number of times the add entity event was fired and read + let firedCount = 0; + // Count the amount of steps we already did + let loopCounter = 0; + + // Will listen for added entity events + // and add to the firedCount + const EventListenerSystem = createSystem({ + myEvents: ReadEvents(SimECSAddEntityEvent), + }).withRunFunction(({myEvents}) => { + myEvents.execute(() => { + firedCount++ + }); + }).build(); + + // Will add entities + const EventTriggerSystem = createSystem({ + actions: Actions, + }).withRunFunction(async ({actions}) => { + actions.commands.addEntity(new Entity()); + }).build(); + + // Will count the number of steps we took and end the simulation + const LoopCounterSystem = createSystem({ + actions: Actions, + }).withRunFunction(({ actions }) => { + loopCounter++; + if (loopCounter >= 3) { + actions.commands.stopRun(); + } + }).build(); + + const prepWorld = buildWorld() + .withDefaultScheduling(root => root + .addNewStage(stage => stage + .addSystem(EventTriggerSystem) + .addSystem(EventListenerSystem) + .addSystem(LoopCounterSystem) + ) + ) + .build(); + + const runWorld = await prepWorld.prepareRun(); + + // Start and await the end of the simulation + await runWorld.start(); + + assert.equal(loopCounter, 3, 'Did not get to three steps exactly!'); + // Since entities are added after the step, the first step will not fire an event, + // so the number of events will always be one smaller than the number of steps. + assert.equal(firedCount, loopCounter - 1, 'Not all added entities fired an event!'); + }); +});