Skip to content

Commit 6042cbd

Browse files
MV88Tobia Di Pisa
authored andcommitted
Fix #3051 toc group/layers problems (#3141)
* fix tests * add tests
1 parent a40cf82 commit 6042cbd

5 files changed

Lines changed: 92 additions & 16 deletions

File tree

web/client/components/TOC/fragments/settings/General.jsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const LocaleUtils = require('../../../../utils/LocaleUtils');
1717
const assign = require('object-assign');
1818
require('react-selectize/themes/index.css');
1919
const {Grid} = require('react-bootstrap');
20-
20+
const {createFromSearch} = require('../../../../utils/TOCUtils');
2121
/**
2222
* General Settings form for layer
2323
*/
@@ -121,17 +121,7 @@ class General extends React.Component {
121121
this.updateEntry("group", {target: {value: value || "Default"}});
122122
}}
123123
theme = "bootstrap3"
124-
createFromSearch={function(options, search) {
125-
// only create an option from search if the length of the search string is > 0 and
126-
// it does no match the label property of an existing option
127-
if (search.length === 0 || (options.map(function(option) {
128-
return option.label;
129-
})).indexOf(search) > -1) {
130-
return null;
131-
}
132-
const val = search.replace(/\./g, '${dot}').replace(/\//g, '.');
133-
return {label: search, value: val};
134-
}}
124+
createFromSearch={createFromSearch}
135125

136126
onValueChange={function(item) {
137127
// here, we add the selected item to the options array, the "new-option"

web/client/reducers/__tests__/layers-test.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,39 @@ describe('Test the layers reducer', () => {
9696
nodeType: 'layers'
9797
};
9898
let initialState = {
99-
groups: [{name: 'sample1', id: 'sample1'}, {name: 'sample2', id: 'sample2'}],
100-
flat: [{id: 'layer1', group: 'sample1'}, {id: 'layer2', group: 'sample2'}]
99+
groups: [
100+
{name: 'sample1', nodes: ['layer1'], id: 'sample1'},
101+
{name: 'sample2', nodes: ['layer2'], id: 'sample2'}
102+
],
103+
flat: [
104+
{id: 'layer1', group: 'sample1'},
105+
{id: 'layer2', group: 'sample2'}
106+
]
101107
};
102108
let state = layers(initialState, testAction);
103-
expect(state.groups.length).toBe(2);
109+
expect(state.groups.length).toBe(1);
104110
expect(state.flat.length).toBe(1);
105111
});
112+
it('removeNode norGroupOrLayer', () => {
113+
let testAction = {
114+
type: 'REMOVE_NODE',
115+
node: 'layer1',
116+
nodeType: 'norGroupOrLayer'
117+
};
118+
let initialState = {
119+
groups: [
120+
{name: 'sample1', nodes: ['layer1'], id: 'sample1'},
121+
{name: 'sample2', nodes: ['layer2'], id: 'sample2'}
122+
],
123+
flat: [
124+
{id: 'layer1', group: 'sample1'},
125+
{id: 'layer2', group: 'sample2'}
126+
]
127+
};
128+
let state = layers(initialState, testAction);
129+
expect(state.groups.length).toBe(2);
130+
expect(state.flat.length).toBe(2);
131+
});
106132

107133
it('removeNode nested', () => {
108134
let testAction = {

web/client/reducers/layers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ function layers(state = [], action) {
213213
const newLayers = state.flat.filter((layer) => layer.id !== action.node);
214214
return assign({}, state, {
215215
flat: newLayers,
216-
groups: newGroups
216+
groups: LayersUtils.removeEmptyGroups(newGroups)
217217
});
218218
}
219+
return state;
219220
}
220221
case ADD_LAYER: {
221222
let newLayers = (state.flat || []).concat();

web/client/utils/TOCUtils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2018, GeoSolutions Sas.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
const TOCUtils = {
11+
createFromSearch: function(options, search) {
12+
/* only create an option from search if the length of the search string is > 0 and
13+
it does no match the label property of an existing option
14+
MV: it should also avoid the creation of group with an empty name therefore a new regex has been introduced
15+
*/
16+
const filterWrongGroupRegex = RegExp('^\/|\/$|\/{2,}');
17+
if (search.length === 0 || (options.map(function(option) {
18+
return option.label;
19+
})).indexOf(search) > -1 || filterWrongGroupRegex.test(search)) {
20+
return null;
21+
}
22+
23+
const val = search.replace(/\./g, '${dot}').replace(/\//g, '.');
24+
return {label: search, value: val};
25+
}
26+
};
27+
28+
29+
module.exports = TOCUtils;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2017, GeoSolutions Sas.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
const expect = require('expect');
9+
const {createFromSearch} = require('../TOCUtils');
10+
let options = [{label: "lab1", value: "val1"}];
11+
12+
describe('TOCUtils', () => {
13+
it('test createFromSearch for General Fragment with value not allowed', () => {
14+
let val = createFromSearch(options, "/as");
15+
expect(val).toBe(null);
16+
val = createFromSearch(options, "a//s");
17+
expect(val).toBe(null);
18+
val = createFromSearch(options, "s/d&/");
19+
expect(val).toBe(null);
20+
});
21+
22+
it('test createFromSearch for General Fragment with new valid value', () => {
23+
let val = createFromSearch(options, "lab2");
24+
expect(val.label).toBe("lab2");
25+
expect(val.value).toBe("lab2");
26+
val = createFromSearch(options, "lab2/lab5");
27+
expect(val.label).toBe("lab2/lab5");
28+
expect(val.value).toBe("lab2.lab5");
29+
});
30+
});

0 commit comments

Comments
 (0)