Skip to content

Commit

Permalink
Fix geosolutions-it#2812 parseInitialState function (geosolutions-it#…
Browse files Browse the repository at this point in the history
…2818)

* fix geosolutions-it#2812 update parsing of initialState in localConfig.json

* update parsing method

* add documentation and test

* update documentation

* add default in toChangesMap and add a test for empty arrays
  • Loading branch information
MV88 authored and kappu committed Apr 26, 2018
1 parent ca27fbb commit 045aaac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
13 changes: 11 additions & 2 deletions web/client/components/app/StandardApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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])}};
}, {});
};
Expand Down
37 changes: 37 additions & 0 deletions web/client/components/app/__tests__/StandardApp-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('StandardApp', () => {
const app = ReactDOM.render(<StandardApp appStore={store} storeOpts={storeOpts}/>, 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');
Expand All @@ -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(<StandardApp appStore={store} storeOpts={storeOpts}/>, 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 = () => ({
Expand Down
6 changes: 3 additions & 3 deletions web/client/utils/FeatureGridUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 045aaac

Please sign in to comment.