oa-ref.spec.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {
  4. oaLinkRef,
  5. oaSchemaRef,
  6. oaExampleRef,
  7. oaResponseRef,
  8. oaPathItemRef,
  9. oaCallbackRef,
  10. oaParameterRef,
  11. oaRequestBodyRef,
  12. oaSecuritySchemeRef,
  13. } from './oa-ref.js';
  14. const ALIASES_MAP = [
  15. [oaSchemaRef, 'schemas'],
  16. [oaResponseRef, 'responses'],
  17. [oaParameterRef, 'parameters'],
  18. [oaExampleRef, 'examples'],
  19. [oaRequestBodyRef, 'requestBodies'],
  20. [oaSecuritySchemeRef, 'securitySchemes'],
  21. [oaLinkRef, 'links'],
  22. [oaCallbackRef, 'callbacks'],
  23. [oaPathItemRef, 'pathItems'],
  24. ];
  25. describe('oaRef aliases', function () {
  26. // eslint-disable-next-line mocha/no-setup-in-describe
  27. ALIASES_MAP.forEach(([fn, segment]) => {
  28. describe(fn.name, function () {
  29. it('should require the "name" parameter to be a none-empty String', function () {
  30. const throwable = v => () => fn(v);
  31. const error = s =>
  32. format(
  33. 'Parameter "name" must be a non-empty String, but %s was given.',
  34. s,
  35. );
  36. expect(throwable('')).to.throw(error('""'));
  37. expect(throwable(10)).to.throw(error('10'));
  38. expect(throwable(0)).to.throw(error('0'));
  39. expect(throwable(true)).to.throw(error('true'));
  40. expect(throwable(false)).to.throw(error('false'));
  41. expect(throwable([])).to.throw(error('Array'));
  42. expect(throwable({})).to.throw(error('Object'));
  43. expect(throwable(undefined)).to.throw(error('undefined'));
  44. expect(throwable(null)).to.throw(error('null'));
  45. throwable('name')();
  46. });
  47. it('should return the correct reference', function () {
  48. expect(fn('name')).to.be.eql({$ref: `#/components/${segment}/name`});
  49. });
  50. });
  51. });
  52. });