refactor: entity -> MutableSignal

This commit is contained in:
2026-04-03 14:19:44 +08:00
parent b1b059de8c
commit 86714e7837
8 changed files with 51 additions and 62 deletions
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { createEntityCollection, Entity, entity } from '@/utils/entity';
import { createEntityCollection, MutableSignal, mutableSignal } from '@/utils/mutable-signal';
type TestEntity = {
id: string;
@@ -82,16 +82,6 @@ describe('createEntityCollection', () => {
expect(collection.get('nonexistent')).toBeUndefined();
});
it('should have correct accessor id', () => {
const collection = createEntityCollection<TestEntity>();
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(testEntity);
const accessor = collection.get('e1');
expect(accessor.id).toBe('e1');
});
it('should handle removing non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
@@ -116,20 +106,19 @@ describe('createEntityCollection', () => {
});
});
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);
describe('MutableSignal', () => {
it('should create signal with initial value', () => {
const s = mutableSignal({ count: 1 });
expect(s.value.count).toBe(1);
});
it('should produce immutable updates', () => {
const e = entity('test', { count: 1, items: [1, 2, 3] });
e.produce(draft => {
const s = mutableSignal({ count: 1, items: [1, 2, 3] });
s.produce(draft => {
draft.count = 2;
draft.items.push(4);
});
expect(e.value.count).toBe(2);
expect(e.value.items).toEqual([1, 2, 3, 4]);
expect(s.value.count).toBe(2);
expect(s.value.items).toEqual([1, 2, 3, 4]);
});
});