Skip to content

Commit

Permalink
test(recs): update tests to take default runOnInit into account
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 28, 2022
1 parent f9baf44 commit 5cacc92
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/recs/tests/System.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ describe("System", () => {
entity = createEntity(world, [withValue(Position, { x: 1, y: 2 })]);
});

it.only("defineSystem should rerun the system if the query result changes (enter, update, exit)", () => {
it("defineSystem should rerun the system if the query result changes (enter, update, exit)", () => {
const mock = jest.fn();
defineSystem(world, [Has(Position)], mock);

setComponent(Position, entity, { x: 2, y: 3 });

expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledTimes(2);
expect(mock).toHaveBeenCalledWith({
entity,
component: Position,
value: [
{ x: 2, y: 3 },
{ x: 1, y: 2 },
],
type: UpdateType.Enter,
type: UpdateType.Update,
});

setComponent(Position, entity, { x: 3, y: 3 });
expect(mock).toHaveBeenCalledTimes(2);
expect(mock).toHaveBeenCalledTimes(3);
expect(mock).toHaveBeenCalledWith({
entity,
component: Position,
Expand All @@ -52,7 +52,7 @@ describe("System", () => {
});

removeComponent(Position, entity);
expect(mock).toHaveBeenCalledTimes(3);
expect(mock).toHaveBeenCalledTimes(4);
expect(mock).toHaveBeenCalledWith({
entity,
component: Position,
Expand All @@ -65,12 +65,13 @@ describe("System", () => {
const mock = jest.fn();
defineUpdateSystem(world, [Has(Position)], mock);

// First time is the "enter" event
// The entity already had a position when the system was created and the system runs on init,
// so this position update is an update
setComponent(Position, entity, { x: 2, y: 3 });
expect(mock).toHaveBeenCalledTimes(0);
expect(mock).toHaveBeenCalledTimes(1);

setComponent(Position, entity, { x: 2, y: 3 });
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledTimes(2);
expect(mock).toHaveBeenCalledWith({
entity,
component: Position,
Expand All @@ -83,10 +84,10 @@ describe("System", () => {

// Setting the same value again should rerun the system
setComponent(Position, entity, { x: 2, y: 3 });
expect(mock).toHaveBeenCalledTimes(2);
expect(mock).toHaveBeenCalledTimes(3);

setComponent(Position, entity, { x: 3, y: 3 });
expect(mock).toHaveBeenCalledTimes(3);
expect(mock).toHaveBeenCalledTimes(4);
});

it("defineEnterSystem should rerun once with entities matching the query for the first time", () => {
Expand Down

0 comments on commit 5cacc92

Please sign in to comment.