-import { describe, it, beforeEach } from 'node:test';
+import { describe, it, beforeEach, mock } from 'node:test';
import assert from 'node:assert';
import Serpentity, { Component, Entity, Node, System } from '../lib/serpentity.js';
this.testNodes = engine.getNodes(internals.node);
this.addedCalled = true;
this.addedEngine = engine;
+ this.emittedEvents = [];
+ this.beforeCall = null;
+ this.afterCall = null;
+
+ this.changeObserver = (event) => {
+ this.emittedEvents.push(event)
+ };
+
+ this.nodeAddedObserver = ({ node }) => {
+ node.test.addEventListener('change', this.changeObserver);
+ };
+
+ this.nodeRemovedObserver = ({ node }) => {
+ node.test.removeEventListener('change', this.changeObserver);
+ };
+
+ for (const node of this.testNodes) {
+ node.test.addEventListener('change', this.changeObserver);
+ }
+
+ this.testNodes.addEventListener('nodeAdded', this.nodeAddedObserver);
+ this.testNodes.addEventListener('nodeRemoved', this.nodeRemovedObserver);
+
super.added(); // not needed, but takes care of coverage :P
}
removed(engine) {
+ this.testNodes.removeEventListener('nodeAdded', this.nodeAddedObserver);
+ this.nodeAddedObserver = null;
+
+ this.testNodes.removeEventListener('nodeRemoved', this.nodeRemovedObserver);
+ this.nodeRemovedObserver = null;
+
+ for (const node of this.testNodes) {
+ node.test.removeEventListener('change', this.changeObserver);
+ }
+ this.changeObserver = null;
+
this.testNodes = null;
this.removedCalled = true;
this.removedEngine = engine;
+ this.emittedEvents = null;
+ this.beforeCall = null;
+ this.afterCall = null;
super.removed(); // not needed, but takes care of coverage :P
}
this.updateDt = dt;
for (const node of this.testNodes) {
+ this.beforeCall = this.beforeCall === null ? node.test.called : this.beforeCall;
node.test.called = true;
+ this.afterCall = this.afterCall === null ? node.test.called : this.afterCall;
node.entity.called = true;
}
assert(!!internals.context.lowPrioritySystem.updateCalled);
});
+ it('should keep proxied object behavior as expected', () => {
+
+ internals.context.engine.update(internals.delta);
+
+ assert(internals.context.highPrioritySystem.beforeCall === false);
+ assert(internals.context.highPrioritySystem.afterCall === true);
+ });
+
+ it('should emit an event for every changed property', () => {
+
+ internals.context.engine.update(internals.delta);
+
+ assert(internals.context.regularSystem.emittedEvents[0].property === 'called');
+ assert(internals.context.regularSystem.emittedEvents[0].from === false);
+ assert(internals.context.regularSystem.emittedEvents[0].to === true);
+ // 3 systems x 2 entities.
+ assert(internals.context.regularSystem.emittedEvents.length === 6);
+ });
+
it('should call update callback in the order of priorities', () => {
internals.context.engine.update(internals.delta);
assert(!internals.context.firstEntity.called);
assert(!!internals.context.secondEntity.called);
});
+
+ it('should not add duplicate entities', () => {
+
+ const originalEntitiesLength = internals.context.engine.entities.length;
+ const added = internals.context.engine.addEntity(internals.context.firstEntity);
+ const finalEntitiesLength = internals.context.engine.entities.length;
+
+ assert(!added);
+
+ assert.deepEqual(finalEntitiesLength, originalEntitiesLength);
+ });
});