projection-schema-registry.spec.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {expect} from 'chai';
  2. import {ProjectionSchemaRegistry} from './projection-schema-registry.js';
  3. describe('ProjectionSchemaRegistry', function () {
  4. describe('defineSchema', function () {
  5. it('should validate the given definition', function () {
  6. const S = new ProjectionSchemaRegistry();
  7. const throwable = () => S.defineSchema({});
  8. expect(throwable).to.throw(
  9. 'Definition option "name" must be a non-empty String, ' +
  10. 'but undefined was given.',
  11. );
  12. });
  13. it('should register the given definition', function () {
  14. const def = {name: 'mySchema', schema: {}};
  15. const S = new ProjectionSchemaRegistry();
  16. S.defineSchema(def);
  17. const res = S.getDefinition(def.name);
  18. expect(res).to.be.eql(def);
  19. });
  20. it('should throw an error if the schema name is already registered', function () {
  21. const S = new ProjectionSchemaRegistry();
  22. const def = {name: 'mySchema', schema: {}};
  23. S.defineSchema(def);
  24. const throwable = () => S.defineSchema(def);
  25. expect(throwable).to.throw(
  26. 'Projection schema "mySchema" is already registered.',
  27. );
  28. });
  29. });
  30. describe('hasSchema', function () {
  31. it('should return true when the given name is registered', function () {
  32. const S = new ProjectionSchemaRegistry();
  33. expect(S.hasSchema('mySchema')).to.be.false;
  34. S.defineSchema({name: 'mySchema', schema: {}});
  35. expect(S.hasSchema('mySchema')).to.be.true;
  36. });
  37. });
  38. describe('getSchema', function () {
  39. it('should throw an error if the given name is not registered', function () {
  40. const S = new ProjectionSchemaRegistry();
  41. const throwable = () => S.getSchema('mySchema');
  42. expect(throwable).to.throw('Projection schema "mySchema" is not found.');
  43. });
  44. it('should return a registered schema by the given name', function () {
  45. const def = {name: 'mySchema', schema: {foo: true, bar: false}};
  46. const S = new ProjectionSchemaRegistry();
  47. S.defineSchema(def);
  48. const res = S.getSchema(def.name);
  49. expect(res).to.be.eql(def.schema);
  50. });
  51. });
  52. describe('getDefinition', function () {
  53. it('should throw an error if the given name is not registered', function () {
  54. const S = new ProjectionSchemaRegistry();
  55. const throwable = () => S.getDefinition('mySchema');
  56. expect(throwable).to.throw('Schema definition "mySchema" is not found.');
  57. });
  58. it('should return a registered definition by the given name', function () {
  59. const def = {name: 'mySchema', schema: {}};
  60. const S = new ProjectionSchemaRegistry();
  61. S.defineSchema(def);
  62. const res = S.getDefinition(def.name);
  63. expect(res).to.be.eql(def);
  64. });
  65. });
  66. });