boardgame-core/tests/utils/entity.test.ts

136 lines
4.9 KiB
TypeScript
Raw Normal View History

2026-04-01 17:34:21 +08:00
import { describe, it, expect } from 'vitest';
2026-04-02 15:16:30 +08:00
import { createEntityCollection, Entity, entity } from '../../src/utils/entity';
2026-04-01 17:34:21 +08:00
2026-04-02 15:16:30 +08:00
type TestEntity = {
id: string;
2026-04-01 17:34:21 +08:00
name: string;
value: number;
2026-04-02 15:16:30 +08:00
};
2026-04-01 17:34:21 +08:00
describe('createEntityCollection', () => {
it('should create empty collection', () => {
const collection = createEntityCollection<TestEntity>();
expect(collection.collection.value).toEqual({});
});
it('should add single entity', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
2026-04-01 17:34:21 +08:00
expect(collection.collection.value).toHaveProperty('e1');
2026-04-02 15:16:30 +08:00
expect(collection.get('e1').value).toEqual(testEntity);
2026-04-01 17:34:21 +08:00
});
it('should add multiple entities', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 };
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
collection.add(entity1, entity2, entity3);
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
expect(Object.keys(collection.collection.value)).toHaveLength(3);
expect(collection.get('e1').value.name).toBe('Entity 1');
expect(collection.get('e2').value.name).toBe('Entity 2');
expect(collection.get('e3').value.name).toBe('Entity 3');
});
it('should remove single entity', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
collection.add(entity1, entity2);
collection.remove('e1');
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
expect(Object.keys(collection.collection.value)).toHaveLength(1);
expect(collection.collection.value).not.toHaveProperty('e1');
expect(collection.collection.value).toHaveProperty('e2');
});
it('should remove multiple entities', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 };
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
collection.add(entity1, entity2, entity3);
collection.remove('e1', 'e3');
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
expect(Object.keys(collection.collection.value)).toHaveLength(1);
expect(collection.collection.value).toHaveProperty('e2');
});
it('should update entity via accessor', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
2026-04-01 17:34:21 +08:00
const accessor = collection.get('e1');
2026-04-02 15:16:30 +08:00
accessor.value = { ...testEntity, value: 100, name: 'Updated' };
2026-04-01 17:34:21 +08:00
expect(collection.get('e1').value.value).toBe(100);
expect(collection.get('e1').value.name).toBe('Updated');
});
it('should return undefined for non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
expect(collection.get('nonexistent')).toBeUndefined();
2026-04-01 17:34:21 +08:00
});
it('should have correct accessor id', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
2026-04-01 17:34:21 +08:00
const accessor = collection.get('e1');
expect(accessor.id).toBe('e1');
});
it('should handle removing non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
2026-04-01 17:34:21 +08:00
collection.remove('nonexistent');
2026-04-02 15:16:30 +08:00
2026-04-01 17:34:21 +08:00
expect(Object.keys(collection.collection.value)).toHaveLength(1);
});
it('should work with reactive updates', () => {
const collection = createEntityCollection<TestEntity>();
2026-04-02 15:16:30 +08:00
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
2026-04-01 17:34:21 +08:00
const accessor = collection.get('e1');
expect(accessor.value.value).toBe(10);
2026-04-02 15:16:30 +08:00
accessor.value = { ...testEntity, value: 50 };
2026-04-01 17:34:21 +08:00
expect(accessor.value.value).toBe(50);
});
});
2026-04-02 15:16:30 +08:00
describe('Entity', () => {
it('should create entity with id and value', () => {
const e = entity('test', { count: 1 });
expect(e.id).toBe('test');
expect(e.value.count).toBe(1);
});
it('should produce immutable updates', () => {
const e = entity('test', { count: 1, items: [1, 2, 3] });
e.produce(draft => {
draft.count = 2;
draft.items.push(4);
});
expect(e.value.count).toBe(2);
expect(e.value.items).toEqual([1, 2, 3, 4]);
});
});