import { MetaContext, $CONSTRUCTOR, $NAME } from '../../../../../lib/experimental/meta/MetaContext'; import { withId, $ID } from '../../../../../lib/experimental/meta/extensions/$id'; import { hashString } from '../../../../../lib/functional/hash/murmurhash'; const IdMetaContext = withId(class extends MetaContext { hashString(str: string, seed?: number) { return hashString(str, seed); } }); class Point { } const POINT_ID = hashString('Point'); describe('experimental.meta.extensions.$id', () => { describe('register', () => { it('should automatically generate ids', () => { const context = new IdMetaContext(); context.register({ [$CONSTRUCTOR]: Point, [$NAME]: 'Point' }); expect(context.resolveName('Point')[$ID]).toBe(POINT_ID); }); it('should set ids with props', () => { const context = new IdMetaContext(); context.register({ [$CONSTRUCTOR]: Point, [$NAME]: 'Point', [$ID]: 115 }); expect(context.resolveName('Point')[$ID]).toBe(115); }); it('should throw error on ids collisions', () => { const context = new IdMetaContext(); class Foo { } context.register({ [$CONSTRUCTOR]: Foo, [$NAME]: 'Foo', [$ID]: POINT_ID }); expect(() => { context.register({ [$CONSTRUCTOR]: Point, [$NAME]: 'Point' }); }).toThrow(); }); }); describe('getId', () => { it('should return meta object for registered ids', () => { const context = new IdMetaContext(); const $Point = context.register({ [$CONSTRUCTOR]: Point, [$NAME]: 'Point' }); expect(context.getId(POINT_ID)).toBe($Point); expect(context.resolveId(POINT_ID)).toBe($Point); }); it('should return undefined for non registered ids', () => { const context = new IdMetaContext(); context.register({ [$CONSTRUCTOR]: Point, [$NAME]: 'Point' }); expect(context.getId(115)).toBeUndefined(); expect(() => { context.resolveId(115) }).toThrow(); }); }); });