projection-schema-registry.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {ProjectionSchemaRegistry} from './projection-schema-registry.js';
  4. describe('ProjectionSchemaRegistry', function () {
  5. describe('defineSchema', function () {
  6. it('should require the name parameter to be a non-empty string', function () {
  7. const S = new ProjectionSchemaRegistry();
  8. const throwable = v => () => S.defineSchema(v, {});
  9. const error = s =>
  10. format('Schema name must be a non-empty String, but %s was given.', s);
  11. expect(throwable('')).to.throw(error('""'));
  12. expect(throwable(10)).to.throw(error('10'));
  13. expect(throwable(0)).to.throw(error('0'));
  14. expect(throwable(true)).to.throw(error('true'));
  15. expect(throwable(false)).to.throw(error('false'));
  16. expect(throwable([])).to.throw(error('Array'));
  17. expect(throwable({})).to.throw(error('Object'));
  18. expect(throwable(null)).to.throw(error('null'));
  19. expect(throwable(undefined)).to.throw(error('undefined'));
  20. throwable('mySchema')();
  21. });
  22. it('should require the schema parameter to be an object', function () {
  23. const S = new ProjectionSchemaRegistry();
  24. const throwable = v => () => S.defineSchema('mySchema', v);
  25. const error = s =>
  26. format('Projection schema must be an Object, but %s was given.', s);
  27. expect(throwable('str')).to.throw(error('"str"'));
  28. expect(throwable('')).to.throw(error('""'));
  29. expect(throwable(10)).to.throw(error('10'));
  30. expect(throwable(0)).to.throw(error('0'));
  31. expect(throwable(true)).to.throw(error('true'));
  32. expect(throwable(false)).to.throw(error('false'));
  33. expect(throwable([])).to.throw(error('Array'));
  34. expect(throwable(null)).to.throw(error('null'));
  35. expect(throwable(undefined)).to.throw(error('undefined'));
  36. throwable({})();
  37. });
  38. it('should throw an error if the name is already registered', function () {
  39. const S = new ProjectionSchemaRegistry();
  40. S.defineSchema('mySchema', {});
  41. const throwable = () => S.defineSchema('mySchema', {});
  42. expect(throwable).to.throw(
  43. 'Projection schema "mySchema" is already registered.',
  44. );
  45. });
  46. it('should register the given schema', function () {
  47. const S = new ProjectionSchemaRegistry();
  48. const schema = {foo: true, bar: false};
  49. S.defineSchema('mySchema', schema);
  50. expect(S['_schemas'].get('mySchema')).to.be.eql(schema);
  51. });
  52. it('should return this', function () {
  53. const S = new ProjectionSchemaRegistry();
  54. const res = S.defineSchema('mySchema', {});
  55. expect(res).to.be.eq(S);
  56. });
  57. });
  58. describe('getSchema', function () {
  59. it('should require the name parameter to be a non-empty string', function () {
  60. const S = new ProjectionSchemaRegistry();
  61. S.defineSchema('mySchema', {});
  62. const throwable = v => () => S.getSchema(v);
  63. const error = s =>
  64. format('Schema name must be a non-empty String, but %s was given.', s);
  65. expect(throwable(10)).to.throw(error('10'));
  66. expect(throwable(0)).to.throw(error('0'));
  67. expect(throwable(true)).to.throw(error('true'));
  68. expect(throwable(false)).to.throw(error('false'));
  69. expect(throwable([])).to.throw(error('Array'));
  70. expect(throwable(null)).to.throw(error('null'));
  71. expect(throwable(undefined)).to.throw(error('undefined'));
  72. throwable('mySchema')();
  73. });
  74. it('should throw an error if the name is not registered', function () {
  75. const S = new ProjectionSchemaRegistry();
  76. S.defineSchema('mySchema', {});
  77. const throwable = () => S.getSchema('unknown');
  78. expect(throwable).to.throw('Projection schema "unknown" is not found.');
  79. });
  80. it('should return the registered schema', function () {
  81. const S = new ProjectionSchemaRegistry();
  82. const schema = {foo: true, bar: false};
  83. S.defineSchema('mySchema', schema);
  84. const res = S.getSchema('mySchema');
  85. expect(res).to.be.eql(schema);
  86. });
  87. });
  88. });