array-type-parser.spec.js 3.1 KB

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