diff --git a/examples/getstarted/api/address/models/Address.settings.json b/examples/getstarted/api/address/models/Address.settings.json index 270b48cb94..3d9b41b654 100755 --- a/examples/getstarted/api/address/models/Address.settings.json +++ b/examples/getstarted/api/address/models/Address.settings.json @@ -43,6 +43,12 @@ "full_name": { "type": "string", "required": true + }, + "dz": { + "type": "dynamiczone", + "components": [ + "default.openingtimes" + ] } } } diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js index 4867924037..22ee5d61aa 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/index.js @@ -97,8 +97,6 @@ const DataManagerProvider = ({ allIcons, children }) => { components: contentTypes, }); - console.log(orderedContenTypes.get('components').toJS()); - dispatch({ type: 'GET_DATA_SUCCEEDED', components: orderedComponents.get('components'), @@ -376,7 +374,7 @@ const DataManagerProvider = ({ allIcons, children }) => { }); }; - console.log({ modifiedData }); + console.log({ modifiedData: reducerState.toJS() }); const sortedContentTypesList = sortBy( Object.keys(contentTypes) diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_basic_actions.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_basic_actions.test.js index 3a7babf0c1..8a99e08d81 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_basic_actions.test.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/tests/reducer_basic_actions.test.js @@ -6,6 +6,199 @@ describe('CTB | containers | reducer | basics actions ', () => { expect(reducer(initialState, { type: 'TEST' })).toEqual(initialState); }); + describe('ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE', () => { + it('should add the created component to the dynamic zone', () => { + const createdComponent = fromJS({ + uid: 'default.test', + category: 'default', + isTemporary: true, + schema: { + icon: 'book', + name: 'test', + collectionName: '', + attributes: {}, + }, + }); + const components = fromJS({ + 'default.test': createdComponent, + 'default.other': { + uid: 'default.other', + category: 'default', + + schema: { + icon: 'book', + name: 'test', + collectionName: '', + attributes: {}, + }, + }, + }); + const contentType = fromJS({ + uid: 'application::test', + schema: { + name: 'test', + attributes: { + dz: { + type: 'dynamiczone', + components: ['default.other'], + }, + }, + }, + }); + const state = initialState + .setIn(['components'], components) + .setIn(['modifiedData', 'components'], components) + + .setIn(['modifiedData', 'contentType'], contentType); + + const expected = state.setIn( + [ + 'modifiedData', + 'contentType', + 'schema', + 'attributes', + 'dz', + 'components', + ], + fromJS(['default.other', 'default.test']) + ); + + expect( + reducer(state, { + type: 'ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE', + dynamicZoneTarget: 'dz', + componentsToAdd: ['default.test'], + }) + ).toEqual(expected); + }); + }); + + describe('CANCEL_CHANGES', () => { + it('Should set the modifiedData and the components object with the initial ones', () => { + const state = fromJS({ + components: { + test: { + something: true, + }, + other: { + something: false, + }, + }, + initialComponents: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + modifiedData: { + components: { + test: { + something: true, + }, + other: { + something: false, + }, + }, + contentType: { + uid: 'something', + name: 'test', + }, + }, + initialData: { + components: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + contentType: { + uid: 'something', + name: 'something', + }, + }, + }); + + const expected = fromJS({ + components: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + initialComponents: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + modifiedData: { + components: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + contentType: { + uid: 'something', + name: 'something', + }, + }, + initialData: { + components: { + test: { + something: false, + }, + other: { + something: false, + }, + }, + contentType: { + uid: 'something', + name: 'something', + }, + }, + }); + + expect(reducer(state, { type: 'CANCEL_CHANGES' })).toEqual(expected); + }); + }); + + describe('CREATE_SCHEMA', () => { + it('Should create a content type schema correctly', () => { + const uid = 'application::test'; + const data = { + collectionName: 'test', + name: 'test', + }; + const expected = initialState.setIn( + ['contentTypes', uid], + fromJS({ + uid, + isTemporary: true, + schema: { + collectionName: data.collectionName, + name: data.name, + attributes: {}, + }, + }) + ); + + expect( + reducer(initialState, { type: 'CREATE_SCHEMA', uid, data }) + ).toEqual(expected); + }); + }); + describe('GET_DATA_SUCCEEDED', () => { const components = { 'default.test': { @@ -50,6 +243,355 @@ describe('CTB | containers | reducer | basics actions ', () => { }); }); + describe('REMOVE_COMPONENT_FROM_DYNAMIC_ZONE', () => { + it('Should remove a component from a dynamic zone', () => { + const state = fromJS({ + components: { + 'default.openingtimes': { + uid: 'default.openingtimes', + category: 'default', + schema: { + icon: 'calendar', + name: 'openingtimes', + description: '', + connection: 'default', + collectionName: 'components_openingtimes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + 'default.dish': { + uid: 'default.dish', + category: 'default', + schema: { + icon: 'calendar', + name: 'dish', + description: '', + connection: 'default', + collectionName: 'components_dishes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + }, + modifiedData: { + components: { + 'default.dish': { + uid: 'default.dish', + category: 'default', + schema: { + icon: 'calendar', + name: 'dish', + description: '', + connection: 'default', + collectionName: 'components_dishes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + 'default.openingtimes': { + uid: 'default.openingtimes', + category: 'default', + schema: { + icon: 'calendar', + name: 'openingtimes', + description: '', + connection: 'default', + collectionName: 'components_openingtimes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + }, + contentType: { + uid: 'application::address.address', + schema: { + name: 'address', + description: '', + connection: 'default', + collectionName: 'addresses', + attributes: { + geolocation: { + type: 'json', + required: true, + }, + city: { + type: 'string', + required: true, + }, + postal_coder: { + type: 'string', + }, + category: { + model: 'category', + }, + cover: { + model: 'file', + via: 'related', + plugin: 'upload', + required: false, + }, + images: { + collection: 'file', + via: 'related', + plugin: 'upload', + required: false, + }, + full_name: { + type: 'string', + required: true, + }, + dz: { + type: 'dynamiczone', + components: ['default.openingtimes', 'default.dish'], + }, + otherDz: { + type: 'dynamiczone', + components: ['default.openingtimes', 'default.dish'], + }, + }, + }, + }, + }, + }); + + const expected = fromJS({ + components: { + 'default.openingtimes': { + uid: 'default.openingtimes', + category: 'default', + schema: { + icon: 'calendar', + name: 'openingtimes', + description: '', + connection: 'default', + collectionName: 'components_openingtimes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + 'default.dish': { + uid: 'default.dish', + category: 'default', + schema: { + icon: 'calendar', + name: 'dish', + description: '', + connection: 'default', + collectionName: 'components_dishes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + }, + modifiedData: { + components: { + 'default.dish': { + uid: 'default.dish', + category: 'default', + schema: { + icon: 'calendar', + name: 'dish', + description: '', + connection: 'default', + collectionName: 'components_dishes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + 'default.openingtimes': { + uid: 'default.openingtimes', + category: 'default', + schema: { + icon: 'calendar', + name: 'openingtimes', + description: '', + connection: 'default', + collectionName: 'components_openingtimes', + attributes: { + label: { + type: 'string', + required: true, + default: 'something', + }, + time: { + type: 'string', + }, + }, + }, + }, + }, + contentType: { + uid: 'application::address.address', + schema: { + name: 'address', + description: '', + connection: 'default', + collectionName: 'addresses', + attributes: { + geolocation: { + type: 'json', + required: true, + }, + city: { + type: 'string', + required: true, + }, + postal_coder: { + type: 'string', + }, + category: { + model: 'category', + }, + cover: { + model: 'file', + via: 'related', + plugin: 'upload', + required: false, + }, + images: { + collection: 'file', + via: 'related', + plugin: 'upload', + required: false, + }, + full_name: { + type: 'string', + required: true, + }, + dz: { + type: 'dynamiczone', + components: ['default.openingtimes'], + }, + otherDz: { + type: 'dynamiczone', + components: ['default.openingtimes', 'default.dish'], + }, + }, + }, + }, + }, + }); + + expect( + reducer(state, { + type: 'REMOVE_COMPONENT_FROM_DYNAMIC_ZONE', + dzName: 'dz', + componentToRemoveIndex: 1, + }) + ).toEqual(expected); + }); + }); + + describe('REMOVE_FIELD_FROM_DISPLAYED_COMPONENT', () => { + it('Should remove the selected field', () => { + const state = fromJS({ + modifiedData: { + components: { + 'default.test': { + schema: { + attributes: { + text: { + type: 'text', + }, + other: { + type: 'string', + }, + last: { + type: 'integer', + }, + }, + }, + }, + }, + }, + }); + + const expected = fromJS({ + modifiedData: { + components: { + 'default.test': { + schema: { + attributes: { + text: { + type: 'text', + }, + last: { + type: 'integer', + }, + }, + }, + }, + }, + }, + }); + + expect( + reducer(state, { + type: 'REMOVE_FIELD_FROM_DISPLAYED_COMPONENT', + componentUid: 'default.test', + attributeToRemoveName: 'other', + }) + ).toEqual(expected); + }); + }); + describe('SET_MODIFIED_DATA', () => { it('Should set the modifiedData object correctly if the user did create a new type', () => { const schemaToSet = fromJS({ @@ -143,5 +685,93 @@ describe('CTB | containers | reducer | basics actions ', () => { }) ).toEqual(expected); }); + + it('Should update the modified data correctly if the schemaType is a component', () => { + const data = { + name: 'newTest', + collectionName: 'newTest', + category: 'test', + icon: 'test', + }; + const state = fromJS({ + components: { + test: { + uid: 'test', + category: 'default', + schema: { + name: 'test', + icon: 'book', + collectionName: 'components_tests', + attributes: { + something: { + type: 'string', + }, + }, + }, + }, + }, + modifiedData: { + components: {}, + component: { + uid: 'test', + category: 'default', + schema: { + name: 'test', + icon: 'book', + collectionName: 'components_tests', + attributes: { + something: { + type: 'string', + }, + }, + }, + }, + }, + }); + const expected = fromJS({ + components: { + test: { + uid: 'test', + category: 'test', + schema: { + name: 'newTest', + icon: 'test', + collectionName: 'newTest', + attributes: { + something: { + type: 'string', + }, + }, + }, + }, + }, + modifiedData: { + components: {}, + component: { + uid: 'test', + category: 'test', + schema: { + name: 'newTest', + icon: 'test', + collectionName: 'newTest', + attributes: { + something: { + type: 'string', + }, + }, + }, + }, + }, + }); + + expect( + reducer(state, { + type: 'UPDATE_SCHEMA', + data, + schemaType: 'component', + uid: 'test', + }) + ).toEqual(expected); + }); }); });