Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2812 parseInitialState function #2818

Merged
merged 6 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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