/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { TransferValue } from './host-store'; import { inflateValidateAndFillDefaults } from './inflate-value'; import type { ExpressionMeta } from './models'; import type { ParameterDefinitions } from './parameter'; describe('inflateValidateAndFillDefaults', () => { it('should set the default value', () => { const parametersValue = {}; const parameters: ParameterDefinitions = { foo: { type: 'string', default: 'Hello', }, }; const transferState = {}; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({ foo: 'Hello', }); }); it('should keep the given value', () => { const parametersValue = { foo: 'Mlem' }; const parameters: ParameterDefinitions = { foo: { type: 'string', default: 'Hello', }, }; const transferState = {}; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({ foo: 'Mlem', }); }); it('should transfer a value', () => { const parametersValue = {}; const parameters: ParameterDefinitions = { foo: { type: 'string', default: 'Hello', control: { transferGroup: 'some-group', }, }, }; const transferState = { 'some-group': ['string', 'Guten Tag'] as TransferValue }; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({ foo: 'Guten Tag', }); }); it('should override a given value with a transfer value', () => { const parametersValue = { foo: 'Bonjour' }; const parameters: ParameterDefinitions = { foo: { type: 'string', default: 'Hello', control: { transferGroup: 'some-group', }, }, }; const transferState = { 'some-group': ['string', 'Guten Tag'] as TransferValue }; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({ foo: 'Guten Tag', }); }); it('should delete a value if undefined returns true', () => { const parametersValue = { foo: 'Bonjour' }; const parameters: ParameterDefinitions = { foo: { type: 'string', default: 'Hello', defined: () => false, control: { transferGroup: 'some-group', }, }, }; const transferState = { 'some-group': ['string', 'Guten Tag'] as TransferValue }; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({}); }); // Let's also make sure that the check on `parameter.undefined` runs on the fully inflated // record of values it('should pass a fully inflated record of values to `undefined` functions', () => { const parametersValue = { bar: 'this should be deleted' }; const parameters: ParameterDefinitions = { bar: { type: 'string', defined: ({ parametersValues }) => parametersValues.foo !== 'Hello', }, foo: { type: 'string', default: 'Hello', // this will default to Hello }, }; const transferState = { 'some-group': ['string', 'Guten Tag'] as TransferValue }; const columns: ExpressionMeta[] = []; expect( inflateValidateAndFillDefaults(parametersValue, parameters, transferState, columns), ).toStrictEqual({ foo: 'Hello' }); }); });