Skip to content

Commit 0723ade

Browse files
committed
test(world): add tests for component removal and addition errors
1 parent f32027d commit 0723ade

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/world.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@ describe("World", () => {
8282
expect(world.getComponent(entity, positionComponent)).toBeUndefined();
8383
});
8484

85+
it("should throw error when removing invalid component type", () => {
86+
const world = new World();
87+
const entity = world.createEntity();
88+
const invalidComponentType = 0 as EntityId<any>; // Invalid component ID
89+
90+
expect(() => world.removeComponent(entity, invalidComponentType)).toThrow("Invalid component type: 0");
91+
});
92+
93+
it("should allow removing wildcard relation components", () => {
94+
const world = new World();
95+
const entity = world.createEntity();
96+
const position: Position = { x: 10, y: 20 };
97+
const targetEntity1 = world.createEntity();
98+
const targetEntity2 = world.createEntity();
99+
const relationId1 = createRelationId(positionComponent, targetEntity1);
100+
const relationId2 = createRelationId(positionComponent, targetEntity2);
101+
102+
// Add multiple relation components with the same base component
103+
world.addComponent(entity, relationId1, position);
104+
world.addComponent(entity, relationId2, { x: 20, y: 30 });
105+
world.flushCommands();
106+
expect(world.hasComponent(entity, relationId1)).toBe(true);
107+
expect(world.hasComponent(entity, relationId2)).toBe(true);
108+
109+
// Remove using wildcard relation should remove all matching components
110+
const wildcardRelation = createRelationId(positionComponent, "*");
111+
world.removeComponent(entity, wildcardRelation);
112+
world.flushCommands();
113+
expect(world.hasComponent(entity, relationId1)).toBe(false);
114+
expect(world.hasComponent(entity, relationId2)).toBe(false);
115+
});
116+
85117
it("should handle multiple components", () => {
86118
const world = new World();
87119
const entity = world.createEntity();
@@ -106,6 +138,24 @@ describe("World", () => {
106138
expect(() => world.addComponent(fakeEntity, positionComponent, position)).toThrow("Entity 9999 does not exist");
107139
});
108140

141+
it("should throw error when adding invalid component type", () => {
142+
const world = new World();
143+
const entity = world.createEntity();
144+
const position: Position = { x: 10, y: 20 };
145+
const invalidComponentType = 0 as EntityId<any>; // Invalid component ID
146+
147+
expect(() => world.addComponent(entity, invalidComponentType, position)).toThrow("Invalid component type: 0");
148+
});
149+
150+
it("should throw error when adding wildcard relation component", () => {
151+
const world = new World();
152+
const entity = world.createEntity();
153+
const position: Position = { x: 10, y: 20 };
154+
const wildcardRelation = createRelationId(positionComponent, "*");
155+
156+
expect(() => world.addComponent(entity, wildcardRelation, position)).toThrow("Cannot directly add wildcard relation components");
157+
});
158+
109159
it("should throw error when getting component from non-existent entity", () => {
110160
const world = new World();
111161
const fakeEntity = createEntityId(9999);

0 commit comments

Comments
 (0)