diff --git a/web/client/components/app/StandardApp.jsx b/web/client/components/app/StandardApp.jsx index 1f973cefad..3d61f5f741 100644 --- a/web/client/components/app/StandardApp.jsx +++ b/web/client/components/app/StandardApp.jsx @@ -21,7 +21,7 @@ const PluginsUtils = require('../../utils/PluginsUtils'); const assign = require('object-assign'); const url = require('url'); -const {isObject} = require('lodash'); +const {isObject, isArray} = require('lodash'); const urlQuery = url.parse(window.location.href, true).query; @@ -120,9 +120,18 @@ class StandardApp extends React.Component { this.store.dispatch(action()); }); }; + /** + * It returns an object of the same structure of the initialState but replacing strings like "{someExpression}" with the result of the expression between brackets. + * @param {object} state the object to parse + * @param {object} context context for expression + * @return {object} the modified object + */ parseInitialState = (state, context) => { return Object.keys(state || {}).reduce((previous, key) => { - return { ...previous, ...{ [key]: isObject(state[key]) ? this.parseInitialState(state[key], context) : + return { ...previous, ...{ [key]: isObject(state[key]) ? + (isArray(state[key]) ? state[key].map(s => { + return isObject(s) ? this.parseInitialState(s, context) : s; + }) : this.parseInitialState(state[key], context)) : PluginsUtils.handleExpression({}, context, state[key])}}; }, {}); }; diff --git a/web/client/components/app/__tests__/StandardApp-test.jsx b/web/client/components/app/__tests__/StandardApp-test.jsx index c9ae529eb3..aa7e0cfcd2 100644 --- a/web/client/components/app/__tests__/StandardApp-test.jsx +++ b/web/client/components/app/__tests__/StandardApp-test.jsx @@ -106,6 +106,7 @@ describe('StandardApp', () => { const app = ReactDOM.render(, document.getElementById("container")); expect(app).toExist(); }); + it('creates a default app and reads initialState with mode', (done) => { const store = (plugins, storeOpts) => { expect(storeOpts.initialState.defaultState.testMode).toBe('EXPRESSION_MODE_TEST'); @@ -129,6 +130,42 @@ describe('StandardApp', () => { expect(app).toExist(); }); + it('test the parseInitialState func', (done) => { + const store = (plugins, storeOpts) => { + expect(storeOpts.initialState.defaultState.test).toExist(); + done(); + return { + dispatch() { + } + }; + }; + + const valueArr1 = "valueArr1"; + const valueArr2 = "valueArr2"; + const innerObjTestValue = "innerObjTestValue"; + const storeOpts = { + initialState: { + defaultState: { + test: "test", + withArrayEmpty: [], + withArray: [valueArr1], + withArrayObj: [valueArr2, { + innerObjTest: innerObjTestValue + }] + }, + mobile: {} + } + }; + const app = ReactDOM.render(, document.getElementById("container")); + expect(app).toExist(); + const parsedInitialState = app.parseInitialState(storeOpts.initialState, {}); + expect(parsedInitialState.defaultState.withArray.length).toBe(1); + expect(parsedInitialState.defaultState.withArrayEmpty.length).toBe(0); + expect(parsedInitialState.defaultState.withArray[0]).toBe(valueArr1); + expect(parsedInitialState.defaultState.withArrayObj.length).toBe(2); + expect(parsedInitialState.defaultState.withArrayObj[0]).toBe(valueArr2); + expect(parsedInitialState.defaultState.withArrayObj[1].innerObjTest).toBe(innerObjTestValue); + }); it('creates a default app and renders the given component', () => { const store = () => ({ diff --git a/web/client/utils/FeatureGridUtils.js b/web/client/utils/FeatureGridUtils.js index da913595e6..c728de40da 100644 --- a/web/client/utils/FeatureGridUtils.js +++ b/web/client/utils/FeatureGridUtils.js @@ -6,7 +6,7 @@ * LICENSE file in the root directory of this source tree. */ -const { get, findIndex, isNil, fill} = require('lodash'); +const { get, findIndex, isNil, fill, isArray} = require('lodash'); const {getFeatureTypeProperties, isGeometryType, isValid, isValidValueForPropertyName, findGeometryProperty, getPropertyDesciptor} = require('./ogc/WFS/base'); const getGeometryName = (describe) => get(findGeometryProperty(describe), "name"); @@ -35,13 +35,13 @@ const getRow = (i, rows) => rows[i]; /* eslint-disable */ -const toChangesMap = (changesArray) => changesArray.reduce((changes, c) => ({ +const toChangesMap = (changesArray = []) => isArray(changesArray) ? changesArray.reduce((changes, c) => ({ ...changes, [c.id]: { ...changes[c.id], ...c.updated } -}), {}); +}), {}) : {}; const applyChanges = (feature, changes) => { const propChanges = Object.keys(changes).filter(k => k !== "geometry").reduce((acc, cur) => ({ ...acc,