import {expect} from 'chai'; import {ProjectionSchemaRegistry} from './projection-schema-registry.js'; describe('ProjectionSchemaRegistry', function () { describe('defineSchema', function () { it('should validate the given definition', function () { const S = new ProjectionSchemaRegistry(); const throwable = () => S.defineSchema({}); expect(throwable).to.throw( 'Definition option "name" must be a non-empty String, ' + 'but undefined was given.', ); }); it('should register the given definition', function () { const def = {name: 'mySchema', schema: {}}; const S = new ProjectionSchemaRegistry(); S.defineSchema(def); const res = S.getDefinition(def.name); expect(res).to.be.eql(def); }); it('should throw an error if the schema name is already registered', function () { const S = new ProjectionSchemaRegistry(); const def = {name: 'mySchema', schema: {}}; S.defineSchema(def); const throwable = () => S.defineSchema(def); expect(throwable).to.throw( 'Projection schema "mySchema" is already registered.', ); }); }); describe('hasSchema', function () { it('should return true when the given name is registered', function () { const S = new ProjectionSchemaRegistry(); expect(S.hasSchema('mySchema')).to.be.false; S.defineSchema({name: 'mySchema', schema: {}}); expect(S.hasSchema('mySchema')).to.be.true; }); }); describe('getSchema', function () { it('should throw an error if the given name is not registered', function () { const S = new ProjectionSchemaRegistry(); const throwable = () => S.getSchema('mySchema'); expect(throwable).to.throw('Projection schema "mySchema" is not found.'); }); it('should return a registered schema by the given name', function () { const def = {name: 'mySchema', schema: {foo: true, bar: false}}; const S = new ProjectionSchemaRegistry(); S.defineSchema(def); const res = S.getSchema(def.name); expect(res).to.be.eql(def.schema); }); }); describe('getDefinition', function () { it('should throw an error if the given name is not registered', function () { const S = new ProjectionSchemaRegistry(); const throwable = () => S.getDefinition('mySchema'); expect(throwable).to.throw('Schema definition "mySchema" is not found.'); }); it('should return a registered definition by the given name', function () { const def = {name: 'mySchema', schema: {}}; const S = new ProjectionSchemaRegistry(); S.defineSchema(def); const res = S.getDefinition(def.name); expect(res).to.be.eql(def); }); }); });