From 22c58415a4dbb7a2c47dd324ba5dcbbad65d2964 Mon Sep 17 00:00:00 2001 From: soupette Date: Fri, 23 Aug 2019 14:48:44 +0200 Subject: [PATCH] Test group utils --- .../admin/src/containers/EditView/index.js | 2 + .../containers/EditView/tests/groups.test.js | 288 +++++++++++++++++- 2 files changed, 274 insertions(+), 16 deletions(-) diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/EditView/index.js b/packages/strapi-plugin-content-manager/admin/src/containers/EditView/index.js index 1ad5578d88..82fc18f8bb 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/EditView/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/EditView/index.js @@ -95,9 +95,11 @@ function EditView({ return acc; }, {}); + console.log({ groupLayouts }); // Retrieve all the default values for the repeatables and init the form const defaultGroupValues = getDefaultGroupValues(groups, groupLayouts); + console.log({ defaultGroupValues }); dispatch({ type: 'GET_GROUP_LAYOUTS_SUCCEEDED', diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/EditView/tests/groups.test.js b/packages/strapi-plugin-content-manager/admin/src/containers/EditView/tests/groups.test.js index 32fcb6a02b..1eddb6567c 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/EditView/tests/groups.test.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/EditView/tests/groups.test.js @@ -1,7 +1,7 @@ import { getDefaultGroupValues, - // retrieveDisplayedGroups, - // retrieveGroupLayoutsToFetch, + retrieveDisplayedGroups, + retrieveGroupLayoutsToFetch, } from '../utils/groups'; describe('Content Manager | EditView | utils | groups', () => { @@ -10,21 +10,277 @@ describe('Content Manager | EditView | utils | groups', () => { expect(getDefaultGroupValues([], {})).toEqual({}); }); - it('should return an object with empty keys', () => { - console.log( - getDefaultGroupValues( - [ - // { - // key: 'closing_period', - // group: 'closingperiod', - // repeatable: undefined, - // isOpen: true, - // min: undefined, - // }, + it('should return the correct data', () => { + const group1 = { + schema: { + attributes: { + title: { + type: 'string', + default: 'test', + }, + description: { + type: 'text', + }, + }, + }, + }; + const group2 = { + schema: { + attributes: { + otherTitle: { + type: 'string', + default: 'test', + }, + otherDescription: { + type: 'text', + }, + }, + }, + }; + const group3 = { + schema: { + attributes: { + otherTitle: { + type: 'string', + }, + otherDescription: { + type: 'text', + }, + }, + }, + }; + const group4 = { + schema: { + attributes: { + otherTitle: { + type: 'string', + }, + otherDescription: { + type: 'text', + }, + }, + }, + }; + const group5 = { + schema: { + attributes: { + otherTitle: { + type: 'string', + }, + otherDescription: { + type: 'text', + }, + }, + }, + }; + const groups = [ + { + key: 'group1', + group: 'group1', + }, + { + key: 'group2', + group: 'group2', + repeatable: true, + min: 1, + }, + { + key: 'group3', + group: 'group3', + repeatable: true, + }, + { + key: 'group4', + group: 'group4', + }, + { + key: 'group5', + group: 'group5', + required: true, + repeatable: true, + }, + { + key: 'group6', + group: 'group5', + min: 1, + repeatable: true, + }, + ]; + const groupLayouts = { + group1, + group2, + group3, + group4, + group5, + }; + const expected = { + group1: { + toSet: { + title: 'test', + }, + defaultRepeatable: { + title: 'test', + }, + }, + group2: { + toSet: [{ _temp__id: 0, otherTitle: 'test' }], + defaultRepeatable: { + otherTitle: 'test', + }, + }, + group3: { + toSet: [], + defaultRepeatable: {}, + }, + group4: { + toSet: {}, + defaultRepeatable: {}, + }, + group5: { + toSet: [], + defaultRepeatable: {}, + }, + group6: { + toSet: [{ _temp__id: 0 }], + defaultRepeatable: {}, + }, + }; + + expect(getDefaultGroupValues(groups, groupLayouts)).toEqual(expected); + }); + }); + + describe('retrieveDisplayedGroups', () => { + it('should return an array with all the groups', () => { + const attributes = { + name: { + maxLength: 50, + required: true, + minLength: 5, + type: 'string', + }, + cover: { + model: 'file', + via: 'related', + plugin: 'upload', + }, + menu: { + model: 'menu', + via: 'restaurant', + }, + categories: { + collection: 'category', + }, + price_range: { + enum: [ + 'very_cheap', + 'cheap', + 'average', + 'expensive', + 'very_expensive', ], - {} - ) - ); + type: 'enumeration', + }, + description: { + type: 'richtext', + required: true, + }, + opening_times: { + group: 'openingtimes', + type: 'group', + required: true, + repeatable: true, + min: 1, + max: 10, + }, + opening_times2: { + group: 'openingtimes', + type: 'group', + }, + closing_period: { + group: 'closingperiod', + type: 'group', + }, + services: { + group: 'restaurantservice', + required: true, + repeatable: true, + type: 'group', + }, + address: { + model: 'address', + }, + }; + const expected = [ + { + key: 'opening_times', + group: 'openingtimes', + repeatable: true, + min: 1, + isOpen: false, + }, + { + key: 'opening_times2', + group: 'openingtimes', + isOpen: true, + min: undefined, + repeatable: undefined, + }, + { + key: 'closing_period', + group: 'closingperiod', + isOpen: true, + min: undefined, + repeatable: undefined, + }, + { + key: 'services', + group: 'restaurantservice', + repeatable: true, + isOpen: false, + min: undefined, + }, + ]; + + expect(retrieveDisplayedGroups(attributes)).toEqual(expected); + }); + }); + + describe('retrieveGroupLayoutsToFetch', () => { + it('should return a filterd array of the groups to fetch', () => { + const groups = [ + { + key: 'opening_times', + group: 'openingtimes', + repeatable: true, + min: 1, + isOpen: false, + }, + { + key: 'opening_times2', + group: 'openingtimes', + isOpen: true, + min: undefined, + repeatable: undefined, + }, + { + key: 'closing_period', + group: 'closingperiod', + isOpen: true, + min: undefined, + repeatable: undefined, + }, + { + key: 'services', + group: 'restaurantservice', + repeatable: true, + isOpen: false, + min: undefined, + }, + ]; + const expected = ['openingtimes', 'closingperiod', 'restaurantservice']; + + expect(retrieveGroupLayoutsToFetch(groups)).toEqual(expected); }); }); });