import { beforeEach, vi } from "vitest"; import { describe, expect, it } from "vitest"; // oxlint-disable typescript/unbound-method import type Entity from "../entity"; import System from "../system"; function getFakeEntity() { return { id: Math.random(), addSystem: vi.fn(), removeSystem: vi.fn(), } as unknown as Entity; } describe("System", () => { it("should initialize", () => { const system = new System(); expect(system).toBeTruthy(); }); describe("addEntity()", () => { let entity: Entity; let system: System; beforeEach(() => { entity = getFakeEntity(); system = new System(); }); it("should add an entity to the system", () => { system.addEntity(entity); expect(system.entities.size).toEqual(1); }); it("should add the system to entity systems", () => { system.addEntity(entity); expect(entity.addSystem).toHaveBeenCalledWith(system); }); it("should call enter() on added entity", () => { system.enter = vi.fn(); system.addEntity(entity); expect(system.enter).toHaveBeenCalledWith(entity); }); }); describe("removeEntity()", () => { let entity: Entity; let system: System; beforeEach(() => { entity = getFakeEntity(); system = new System(); system.addEntity(entity); }); it("should remove an entity from the system", () => { system.removeEntity(entity); expect(system.entities.size).toEqual(0); }); it("should remove the system from entity systems", () => { system.removeEntity(entity); expect(entity.removeSystem).toHaveBeenCalledWith(system); }); it("should call exit() on removed entity", () => { system.exit = vi.fn(); system.removeEntity(entity); expect(system.exit).toHaveBeenCalledWith(entity); }); }); describe("updateAll()", () => { it("should call update() on each entity", () => { const entity1 = getFakeEntity(); const entity2 = getFakeEntity(); const system = new System(); system.update = vi.fn(); system.addEntity(entity1); system.addEntity(entity2); system.updateAll(0, 0); expect(system.update).toHaveBeenCalledTimes(2); }); it("should call preUpdate()", () => { const system = new System(); system.preUpdate = vi.fn(); system.updateAll(0, 0); expect(system.preUpdate).toHaveBeenCalled(); }); it("should call postUpdate()", () => { const system = new System(); system.postUpdate = vi.fn(); system.updateAll(0, 0); expect(system.postUpdate).toHaveBeenCalled(); }); }); });