array-type-parser.spec.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {expect} from 'chai';
  2. import {DataParsingError} from '../errors/index.js';
  3. import {ServiceContainer} from '@e22m4u/js-service';
  4. import {arrayTypeParser} from './array-type-parser.js';
  5. import {DATA_TYPE_LIST, DataType} from '../data-type.js';
  6. describe('arrayTypeParser', function () {
  7. it('should return a value as is when a non-array schema is given', function () {
  8. const container = new ServiceContainer();
  9. const dataTypes = DATA_TYPE_LIST.filter(v => v !== DataType.ARRAY);
  10. const values = ['str', '', 10, 0, true, false, [], {}, undefined, null];
  11. const parse = type => {
  12. values.forEach(value => {
  13. const res = arrayTypeParser(value, {type}, undefined, container);
  14. expect(res).to.be.eql(value);
  15. });
  16. };
  17. dataTypes.forEach(parse);
  18. });
  19. it('should return an array value as is when an array schema is given', function () {
  20. const container = new ServiceContainer();
  21. const value = [];
  22. const res = arrayTypeParser(
  23. value,
  24. {type: DataType.ARRAY},
  25. undefined,
  26. container,
  27. );
  28. expect(res).to.be.eq(value);
  29. });
  30. it('should parse a json string when an array schema is given', function () {
  31. const container = new ServiceContainer();
  32. const value = '[1, 2, 3]';
  33. const res = arrayTypeParser(
  34. value,
  35. {type: DataType.ARRAY},
  36. undefined,
  37. container,
  38. );
  39. expect(res).to.be.eql([1, 2, 3]);
  40. });
  41. it('should return a nullish value as is when an array schema is given', function () {
  42. const container = new ServiceContainer();
  43. [undefined, null].forEach(value => {
  44. const res = arrayTypeParser(
  45. value,
  46. {type: DataType.ARRAY},
  47. undefined,
  48. container,
  49. );
  50. expect(res).to.be.eq(value);
  51. });
  52. });
  53. it('should throw an error for a non-array value when an array schema is given', function () {
  54. const container = new ServiceContainer();
  55. const values = ['str', '', 10, 0, true, false, {}];
  56. const dataType = DataType.ARRAY;
  57. const parse = value => {
  58. const throwable = () =>
  59. arrayTypeParser(value, {type: dataType}, undefined, container);
  60. expect(throwable).to.throw(DataParsingError);
  61. };
  62. values.forEach(parse);
  63. });
  64. it('should pass correct arguments to the DataParsingError', function () {
  65. const container = new ServiceContainer();
  66. const value = Symbol();
  67. const dataType = DataType.ARRAY;
  68. const sourcePath = 'aSource';
  69. let error;
  70. try {
  71. arrayTypeParser(value, {type: dataType}, {sourcePath}, container);
  72. } catch (e) {
  73. error = e;
  74. }
  75. expect(error.value).to.be.eq(value);
  76. expect(error.targetType).to.be.eq(dataType);
  77. expect(error.sourcePath).to.be.eq(sourcePath);
  78. });
  79. it('should ignore parsing errors when the "noParsingErrors" option is true', function () {
  80. const container = new ServiceContainer();
  81. const value = Symbol();
  82. const res = arrayTypeParser(
  83. value,
  84. {type: DataType.ARRAY},
  85. {noParsingErrors: true},
  86. container,
  87. );
  88. expect(res).to.be.eq(value);
  89. });
  90. });