| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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';
- import {EmptyValuesService} from '@e22m4u/js-empty-values';
- 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 the 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 the 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 an empty value as is when the array schema is given', function () {
- const container = new ServiceContainer();
- const emptyValues = container.get(EmptyValuesService);
- emptyValues.setEmptyValuesOf(DataType.ARRAY, [undefined]);
- const res = arrayTypeParser(
- undefined,
- {type: DataType.ARRAY},
- undefined,
- container,
- );
- expect(res).to.be.undefined;
- });
- it('should throw an error for a non-array value when the 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);
- });
- });
|