| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import {expect} from 'chai';
- import {DataParsingError} from '../errors/index.js';
- import {ServiceContainer} from '@e22m4u/js-service';
- import {arrayTypeParser} from './array-type-parser.js';
- import {DATA_TYPE_LIST, DataType} from '../data-type.js';
- describe('arrayTypeParser', function () {
- it('should return a value as is when a non-array schema is given', function () {
- const container = new ServiceContainer();
- const dataTypes = DATA_TYPE_LIST.filter(v => v !== DataType.ARRAY);
- const values = ['str', '', 10, 0, true, false, [], {}, undefined, null];
- const parse = type => {
- values.forEach(value => {
- const res = arrayTypeParser(value, {type}, undefined, container);
- expect(res).to.be.eql(value);
- });
- };
- dataTypes.forEach(parse);
- });
- it('should return an array value as is when an array schema is given', function () {
- const container = new ServiceContainer();
- const value = [];
- const res = arrayTypeParser(
- value,
- {type: DataType.ARRAY},
- undefined,
- container,
- );
- expect(res).to.be.eq(value);
- });
- it('should parse a json string when an array schema is given', function () {
- const container = new ServiceContainer();
- const value = '[1, 2, 3]';
- const res = arrayTypeParser(
- value,
- {type: DataType.ARRAY},
- undefined,
- container,
- );
- expect(res).to.be.eql([1, 2, 3]);
- });
- it('should return a nullish value as is when an array schema is given', function () {
- const container = new ServiceContainer();
- [undefined, null].forEach(value => {
- const res = arrayTypeParser(
- value,
- {type: DataType.ARRAY},
- undefined,
- container,
- );
- expect(res).to.be.eq(value);
- });
- });
- it('should throw an error for a non-array value when an array schema is given', function () {
- const container = new ServiceContainer();
- const values = ['str', '', 10, 0, true, false, {}];
- const dataType = DataType.ARRAY;
- const parse = value => {
- const throwable = () =>
- arrayTypeParser(value, {type: dataType}, undefined, container);
- expect(throwable).to.throw(DataParsingError);
- };
- values.forEach(parse);
- });
- it('should pass correct arguments to the DataParsingError', function () {
- const container = new ServiceContainer();
- const value = Symbol();
- const dataType = DataType.ARRAY;
- const sourcePath = 'aSource';
- let error;
- try {
- arrayTypeParser(value, {type: dataType}, {sourcePath}, container);
- } catch (e) {
- error = e;
- }
- expect(error.value).to.be.eq(value);
- expect(error.targetType).to.be.eq(dataType);
- expect(error.sourcePath).to.be.eq(sourcePath);
- });
- it('should ignore parsing errors when the "noParsingErrors" option is true', function () {
- const container = new ServiceContainer();
- const value = Symbol();
- const res = arrayTypeParser(
- value,
- {type: DataType.ARRAY},
- {noParsingErrors: true},
- container,
- );
- expect(res).to.be.eq(value);
- });
- });
|