validate-projection-schema-definition.spec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {validateProjectionSchemaDefinition} from './validate-projection-schema-definition.js';
  4. describe('validateProjectionSchemaDefinition', function () {
  5. it('should require the first argument to be an object', function () {
  6. const throwable = v => () => validateProjectionSchemaDefinition(v);
  7. const error = s =>
  8. format(
  9. 'Projection schema definition must be an Object, but %s was given.',
  10. s,
  11. );
  12. expect(throwable('str')).to.throw(error('"str"'));
  13. expect(throwable('')).to.throw(error('""'));
  14. expect(throwable(10)).to.throw(error('10'));
  15. expect(throwable(0)).to.throw(error('0'));
  16. expect(throwable(true)).to.throw(error('true'));
  17. expect(throwable(false)).to.throw(error('false'));
  18. expect(throwable([])).to.throw(error('Array'));
  19. expect(throwable(null)).to.throw(error('null'));
  20. expect(throwable(undefined)).to.throw(error('undefined'));
  21. throwable({name: 'mySchema', schema: {}})();
  22. });
  23. it('should require the "name" option to be a string', function () {
  24. const throwable = v => () =>
  25. validateProjectionSchemaDefinition({name: v, schema: {}});
  26. const error = s =>
  27. format(
  28. 'Projection schema name must be a non-empty String, but %s was given.',
  29. s,
  30. );
  31. expect(throwable('')).to.throw(error('""'));
  32. expect(throwable(10)).to.throw(error('10'));
  33. expect(throwable(0)).to.throw(error('0'));
  34. expect(throwable([])).to.throw(error('Array'));
  35. expect(throwable({})).to.throw(error('Object'));
  36. expect(throwable(null)).to.throw(error('null'));
  37. expect(throwable(undefined)).to.throw(error('undefined'));
  38. throwable('mySchema')();
  39. });
  40. it('should require the "schema" option to be an object', function () {
  41. const throwable = v => () =>
  42. validateProjectionSchemaDefinition({name: 'mySchema', schema: v});
  43. const error = s =>
  44. format('Projection schema must be an Object, but %s was given.', s);
  45. expect(throwable('str')).to.throw(error('"str"'));
  46. expect(throwable('')).to.throw(error('""'));
  47. expect(throwable(10)).to.throw(error('10'));
  48. expect(throwable(0)).to.throw(error('0'));
  49. expect(throwable([])).to.throw(error('Array'));
  50. expect(throwable(null)).to.throw(error('null'));
  51. expect(throwable(undefined)).to.throw(error('undefined'));
  52. throwable({})();
  53. });
  54. it('should validate the projection schema', function () {
  55. const throwable = () =>
  56. validateProjectionSchemaDefinition({name: 'mySchema', schema: {foo: 10}});
  57. expect(throwable).to.throw(
  58. 'Field options must be a Boolean or an Object, but 10 was given.',
  59. );
  60. });
  61. });