Skip to content

Commit

Permalink
chore: add form definition mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Hein Jeong committed May 31, 2022
1 parent 7c46670 commit f585116
Show file tree
Hide file tree
Showing 14 changed files with 1,028 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/codegen-ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './lib/component-renderer-base';
export * from './lib/render-component-response';
export * from './lib/framework-output-manager';
export * from './lib/template-renderer-factory';
export * from './lib/generate-form-definition';

export * from './lib/renderer-helper';
export * from './lib/validation-helper';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 { addDataStoreModelField } from '../../../generate-form-definition/helpers';
import { FormDefinition } from '../../../types';

describe('addDataStoreModelField', () => {
it('should map the component type, data type and props to the form definition', () => {
const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: [],
};

const dataStoreModelField = { name: 'name', type: 'String', isReadOnly: false, isRequired: false, isArray: false };

addDataStoreModelField(formDefinition, dataStoreModelField);

expect(formDefinition.elements.name.componentType).toBe('TextField');
expect(formDefinition.elements.name.dataType).toBe('String');
expect(formDefinition.elements.name.props).toStrictEqual({ label: 'name', isRequired: false, isReadOnly: false });
});

it('should throw if field is an array', () => {
const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: [],
};

const dataStoreModelField = { name: 'name', type: 'String', isReadOnly: false, isRequired: false, isArray: true };

expect(() => addDataStoreModelField(formDefinition, dataStoreModelField)).toThrow();
});

it('should throw if there is no default component', () => {
const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: [],
};

const dataStoreModelField = {
name: 'name',
type: 'ErrantType',
isReadOnly: false,
isRequired: false,
isArray: false,
};

expect(() => addDataStoreModelField(formDefinition, dataStoreModelField)).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 { mapFormFieldConfig } from '../../../generate-form-definition/helpers';
import { FormDefinition, StudioFormFieldConfig } from '../../../types';

describe('mapFormFieldConfig', () => {
it('should map properties onto an existing field', () => {
const element: { type: string; name: string; config: StudioFormFieldConfig } = {
type: 'field',
name: 'price',
config: {
label: 'Price',
position: { fixed: 'first' },
inputType: {
type: 'SliderField',
minValue: 0,
maxValue: 100,
step: 1,
readOnly: true,
required: false,
},
},
};

const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {
price: {
componentType: 'TextField',
dataType: 'Int',
props: { label: 'price', isReadOnly: false, isRequired: true },
},
},
buttons: {},
elementMatrix: [['price']],
};

mapFormFieldConfig(element, formDefinition);

expect(formDefinition.elements.price).toStrictEqual({
componentType: 'SliderField',
dataType: 'Int',
props: { label: 'Price', isReadOnly: true, isRequired: false, minValue: 0, maxValue: 100, step: 1 },
});
});

it('should map fields that do not yet exist', () => {
const element: { type: string; name: string; config: StudioFormFieldConfig } = {
type: 'field',
name: 'price',
config: {
label: 'Price',
position: { fixed: 'first' },
inputType: {
type: 'SliderField',
minValue: 0,
maxValue: 100,
step: 1,
readOnly: true,
required: false,
},
},
};

const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: [['price']],
};

mapFormFieldConfig(element, formDefinition);

expect(formDefinition.elements.price).toStrictEqual({
componentType: 'SliderField',
props: { label: 'Price', isReadOnly: true, isRequired: false, minValue: 0, maxValue: 100, step: 1 },
});
});

it('should throw if input type is not valid for the data type', () => {
const element: { type: string; name: string; config: StudioFormFieldConfig } = {
type: 'field',
name: 'price',
config: {
inputType: {
type: 'RadioGroupField',
valueMappings: [],
},
},
};

const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {
price: {
componentType: 'TextField',
dataType: 'Int',
props: { label: 'price', isReadOnly: false, isRequired: true },
},
},
buttons: {},
elementMatrix: [['price']],
};

expect(() => mapFormFieldConfig(element, formDefinition)).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 { findIndices, removeAndReturnItemOnward, removeFromMatrix } from '../../../generate-form-definition/helpers';

describe('findIndices', () => {
it('should find the indices of a string in a two-dimensional array', () => {
const matrix = [['one', 'two', 'three', 'four'], ['five'], ['six', 'seven', 'eight']];

expect(findIndices('seven', matrix)).toStrictEqual([2, 1]);
});

it('should return undefined if string not found', () => {
const matrix = [['one', 'two', 'three', 'four'], ['five'], ['six', 'seven', 'eight']];
expect(findIndices('nine', matrix)).toBe(undefined);
});
});

describe('removeFromMatrix', () => {
it('should remove given indices from matrix', () => {
const matrix = [['one', 'two', 'three', 'four'], ['five'], ['six', 'seven', 'eight']];

const formDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: matrix,
};

removeFromMatrix([2, 1], formDefinition);

expect(formDefinition.elementMatrix).toStrictEqual([['one', 'two', 'three', 'four'], ['five'], ['six', 'eight']]);
});
});

describe('removeAndReturnItemOnward', () => {
it('should remove and return an item and all items to the right of it', () => {
const matrix = [['one', 'two', 'three', 'four'], ['five'], ['six', 'seven', 'eight']];

const formDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: matrix,
};

expect(removeAndReturnItemOnward([0, 1], formDefinition)).toStrictEqual(['two', 'three', 'four']);

expect(formDefinition.elementMatrix).toStrictEqual([['one'], ['five'], ['six', 'seven', 'eight']]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 { mapSectionalElement } from '../../../generate-form-definition/helpers';
import { FormDefinition, SectionalElement } from '../../../types';

describe('mapSectionalElement', () => {
it('should throw if there is already an element with the same name', () => {
const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: { Heading123: { componentType: 'Heading', props: {} } },
buttons: {},
elementMatrix: [],
};

const element: { type: string; name: string; config: SectionalElement } = {
type: 'sectionalElement',
name: 'Heading123',
config: { type: 'Heading', level: 1, text: 'My Heading', position: { fixed: 'first' }, name: 'Heading123' },
};

expect(() => mapSectionalElement(element, formDefinition)).toThrow();
});

it('should map configurations', () => {
const formDefinition: FormDefinition = {
form: { props: { layoutStyle: {} } },
elements: {},
buttons: {},
elementMatrix: [],
};

const element: { type: string; name: string; config: SectionalElement } = {
type: 'sectionalElement',
name: 'Heading123',
config: { type: 'Heading', level: 1, text: 'My Heading', position: { fixed: 'first' }, name: 'Heading123' },
};

mapSectionalElement(element, formDefinition);

expect(formDefinition.elements.Heading123).toStrictEqual({
componentType: 'Heading',
props: { level: 1, text: 'My Heading' },
});
});
});
Loading

0 comments on commit f585116

Please sign in to comment.