|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { component, relation } from "./entity"; |
| 3 | +import { World } from "./world"; |
| 4 | + |
| 5 | +describe("World.getOptional", () => { |
| 6 | + it("should return { value: T } when component exists", () => { |
| 7 | + const world = new World(); |
| 8 | + const PositionId = component<{ x: number; y: number }>(); |
| 9 | + const entity = world.new(); |
| 10 | + world.set(entity, PositionId, { x: 10, y: 20 }); |
| 11 | + world.sync(); |
| 12 | + |
| 13 | + const result = world.getOptional(entity, PositionId); |
| 14 | + expect(result).toEqual({ value: { x: 10, y: 20 } }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return undefined when component does not exist", () => { |
| 18 | + const world = new World(); |
| 19 | + const PositionId = component<{ x: number; y: number }>(); |
| 20 | + const VelocityId = component<{ x: number; y: number }>(); |
| 21 | + const entity = world.new(); |
| 22 | + world.set(entity, PositionId, { x: 10, y: 20 }); |
| 23 | + world.sync(); |
| 24 | + |
| 25 | + const result = world.getOptional(entity, VelocityId); |
| 26 | + expect(result).toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should distinguish between component value being undefined and component not existing", () => { |
| 30 | + const world = new World(); |
| 31 | + const UndefinedComponent = component<undefined>(); |
| 32 | + const entity = world.new(); |
| 33 | + world.set(entity, UndefinedComponent, undefined); |
| 34 | + world.sync(); |
| 35 | + |
| 36 | + // Exists with undefined value |
| 37 | + expect(world.getOptional(entity, UndefinedComponent)).toEqual({ value: undefined }); |
| 38 | + |
| 39 | + // Not existing |
| 40 | + const Other = component<number>(); |
| 41 | + expect(world.getOptional(entity, Other)).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should throw error when entity does not exist", () => { |
| 45 | + const world = new World(); |
| 46 | + const PositionId = component<{ x: number; y: number }>(); |
| 47 | + const entity = 1234 as any; // non-existent entity |
| 48 | + |
| 49 | + expect(() => world.getOptional(entity, PositionId)).toThrow("Entity 1234 does not exist"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should return undefined for wildcard relations", () => { |
| 53 | + const world = new World(); |
| 54 | + const Rel = component<number>(); |
| 55 | + const target = world.new(); |
| 56 | + const entity = world.new(); |
| 57 | + world.set(entity, relation(Rel, target), 100); |
| 58 | + world.sync(); |
| 59 | + |
| 60 | + const wildcard = relation(Rel, "*"); |
| 61 | + expect(world.getOptional(entity, wildcard as any)).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should work with dontFragment relations", () => { |
| 65 | + const world = new World(); |
| 66 | + const DFRel = component<number>({ dontFragment: true }); |
| 67 | + const target = world.new(); |
| 68 | + const entity = world.new(); |
| 69 | + world.set(entity, relation(DFRel, target), 42); |
| 70 | + world.sync(); |
| 71 | + |
| 72 | + const relId = relation(DFRel, target); |
| 73 | + expect(world.getOptional(entity, relId)).toEqual({ value: 42 }); |
| 74 | + |
| 75 | + const otherTarget = world.new(); |
| 76 | + const otherRelId = relation(DFRel, otherTarget); |
| 77 | + expect(world.getOptional(entity, otherRelId)).toBeUndefined(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments