import {expect} from 'chai'; import {DataType} from '../data-type.js'; import {ServiceContainer} from '@e22m4u/js-service'; import {defaultValueSetter} from './default-value-setter.js'; describe('defaultValueSetter', function () { it('should return a nullish value as is when default values is disabled', function () { const container = new ServiceContainer(); [undefined, null].forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY, default: 10}, {noDefaultValues: true}, container, ); expect(res).to.be.eq(value); }); }); it('should return a nullish value as is when no default value is specified', function () { const container = new ServiceContainer(); [undefined, null].forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY}, undefined, container, ); expect(res).to.be.eq(value); }); }); it('should return a non-nullish value as is', function () { const container = new ServiceContainer(); const values = ['str', '', 10, 0, true, false, [1], [], {p: 1}, {}]; values.forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY}, undefined, container, ); expect(res).to.be.eq(value); }); }); it('should return a non-nullish value as is even if the "default" option is specified', function () { const container = new ServiceContainer(); const values = ['str', '', 10, 0, true, false, [1], [], {p: 1}, {}]; values.forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY, default: 10}, undefined, container, ); expect(res).to.be.eq(value); }); }); it('should resolve a factory value from the "default" option', function () { const container = new ServiceContainer(); [undefined, null].forEach(value => { let invoked = 0; const factory = (...args) => { invoked++; expect(args).to.be.eql([container]); return 10; }; const res = defaultValueSetter( value, {type: DataType.ANY, default: factory}, undefined, container, ); expect(res).to.be.eq(10); expect(invoked).to.be.eq(1); }); }); it('should return a default value when a nullish value is given', function () { const container = new ServiceContainer(); [undefined, null].forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY, default: 10}, undefined, container, ); expect(res).to.be.eq(10); }); }); it('should return a clone of a default value instead of its reference', function () { const container = new ServiceContainer(); const defaultValue = {foo: 'bar'}; [undefined, null].forEach(value => { const res = defaultValueSetter( value, {type: DataType.ANY, default: defaultValue}, undefined, container, ); expect(res).to.be.eql(defaultValue); expect(res).to.be.not.eq(defaultValue); }); }); });