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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 schema definition to be an object', function () {
  6. const throwable = v => () => validateProjectionSchemaDefinition(v);
  7. const error = s =>
  8. format('Schema definition must be an Object, but %s was given.', s);
  9. expect(throwable('str')).to.throw(error('"str"'));
  10. expect(throwable('')).to.throw(error('""'));
  11. expect(throwable(10)).to.throw(error('10'));
  12. expect(throwable(0)).to.throw(error('0'));
  13. expect(throwable(true)).to.throw(error('true'));
  14. expect(throwable(false)).to.throw(error('false'));
  15. expect(throwable([])).to.throw(error('Array'));
  16. expect(throwable(undefined)).to.throw(error('undefined'));
  17. expect(throwable(null)).to.throw(error('null'));
  18. expect(throwable(() => undefined)).to.throw(error('Function'));
  19. throwable({name: 'mySchema', schema: {}})();
  20. });
  21. it('should require the name option to be a non-empty string', function () {
  22. const throwable = v => () =>
  23. validateProjectionSchemaDefinition({name: v, schema: {}});
  24. const error = s =>
  25. format(
  26. 'Definition option "name" must be a non-empty String, ' +
  27. 'but %s was given.',
  28. s,
  29. );
  30. expect(throwable('')).to.throw(error('""'));
  31. expect(throwable(10)).to.throw(error('10'));
  32. expect(throwable(0)).to.throw(error('0'));
  33. expect(throwable([])).to.throw(error('Array'));
  34. expect(throwable({})).to.throw(error('Object'));
  35. expect(throwable(null)).to.throw(error('null'));
  36. expect(throwable(undefined)).to.throw(error('undefined'));
  37. expect(throwable(() => ({}))).to.throw(error('Function'));
  38. throwable('str')();
  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(
  45. 'Definition option "schema" must be an Object, but %s was given.',
  46. s,
  47. );
  48. expect(throwable('str')).to.throw(error('"str"'));
  49. expect(throwable('')).to.throw(error('""'));
  50. expect(throwable(10)).to.throw(error('10'));
  51. expect(throwable(0)).to.throw(error('0'));
  52. expect(throwable([])).to.throw(error('Array'));
  53. expect(throwable(null)).to.throw(error('null'));
  54. expect(throwable(undefined)).to.throw(error('undefined'));
  55. expect(throwable(() => ({}))).to.throw(error('Function'));
  56. throwable({})();
  57. });
  58. });