From 60cd11624aa1d3cea0fc12a5b51b054314d89cec Mon Sep 17 00:00:00 2001 From: soupette Date: Wed, 8 Jan 2020 17:57:25 +0100 Subject: [PATCH] Add test for adding a common attribute --- .../reducer_add_attribute_action.test.js | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_add_attribute_action.test.js diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_add_attribute_action.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_add_attribute_action.test.js new file mode 100644 index 0000000000..d4004332c9 --- /dev/null +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_add_attribute_action.test.js @@ -0,0 +1,124 @@ +import { fromJS } from 'immutable'; +import { get } from 'lodash'; +import reducer, { initialState } from '../reducer'; +import testData from './data'; + +describe('CTB | containers | reducer | ADD_ATTRIBUTE', () => { + describe('Adding a common field that is not a relation', () => { + it('Should add a text field to a content type correctly', () => { + const state = initialState.setIn( + ['modifiedData', 'contentType'], + fromJS(get(testData, ['contentTypes', 'application::address.address'])) + ); + const action = { + type: 'ADD_ATTRIBUTE', + + attributeToSet: { + type: 'string', + name: 'name', + default: 'something', + private: true, + required: true, + unique: true, + maxLength: 3, + minLength: 1, + }, + forTarget: 'contentType', + targetUid: 'application::address.address', + initialAttribute: {}, + shouldAddComponentToData: false, + }; + + const expected = state.setIn( + ['modifiedData', 'contentType', 'schema', 'attributes', 'name'], + fromJS({ + type: 'string', + default: 'something', + private: true, + required: true, + unique: true, + maxLength: 3, + minLength: 1, + }) + ); + + expect(reducer(state, action)).toEqual(expected); + }); + + it('Should add a integer field to a component that is an attribute of a content type', () => { + const compoUID = 'default.dish'; + const compoSchema = fromJS(get(testData, ['components', compoUID])); + const state = initialState + .setIn( + ['modifiedData', 'contentType'], + fromJS( + get(testData, ['contentTypes', 'application::address.address']) + ).setIn( + ['schema', 'attributes', 'compo_field'], + fromJS({ + type: 'component', + component: compoUID, + }) + ) + ) + .setIn(['modifiedData', 'components', compoUID], compoSchema) + .setIn(['components', compoUID], compoSchema); + + const action = { + type: 'ADD_ATTRIBUTE', + attributeToSet: { + name: 'test', + type: 'integer', + default: 2, + private: true, + required: true, + min: null, + }, + forTarget: 'components', + targetUid: 'default.dish', + initialAttribute: {}, + shouldAddComponentToData: false, + }; + + const expected = state.setIn( + [ + 'modifiedData', + 'components', + compoUID, + 'schema', + 'attributes', + 'test', + ], + fromJS({ + type: 'integer', + default: 2, + private: true, + required: true, + min: null, + }) + ); + + expect(reducer(state, action)).toEqual(expected); + }); + }); + + describe('Adding a component field attribute', () => { + it('Should create the component attribute and add the component to the modifiedData.components if the component is not in the object', () => { + expect(true).toBe(true); + }); + + it('Should create the component attribute and not add the component to the modifiedData.components if the component is already in the object to keep the modifications', () => { + expect(true).toBe(true); + }); + + it('Should create the component correctly in case the component is created on the fly', () => { + expect(true).toBe(true); + }); + }); + + describe('Adding a dynamic zone', () => { + it('Should create the dynamiczone attribute correctly', () => { + expect(true).toBe(true); + }); + }); +});