-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hein Jeong
committed
May 31, 2022
1 parent
7c46670
commit f585116
Showing
14 changed files
with
1,028 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/datastore-model.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
121 changes: 121 additions & 0 deletions
121
packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/form-field.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/position.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']]); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/sectional-element.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.