Skip to content

Commit 7ddb130

Browse files
[fix]UT _getFiltersByCatalog缺陷
1 parent 11e865b commit 7ddb130

File tree

2 files changed

+273
-8
lines changed

2 files changed

+273
-8
lines changed

src/common/mapping/WebMapV3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
388388
const { catalogType, children } = catalog;
389389
if(catalogType === 'group' && children) {
390390
children.forEach(child => {
391-
this._getFiltersByCatalog(child, res);
391+
this._getFilterByCatalog(child, res);
392392
})
393393
}
394394
if (catalogType === 'layer') {
@@ -402,7 +402,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
402402
}
403403
_getFiltersByCatalog(_mapResourceInfo = this._mapResourceInfo) {
404404
const { catalogs = [] } = _mapResourceInfo;
405-
const res = [];
405+
const res = {};
406406
catalogs.forEach((item) => {
407407
this._getFilterByCatalog(item, res);
408408
})

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 271 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import * as mockL7 from '../../tool/mock_l7';
1313
import mbglmap, { CRS, proj4, revertCRS } from '../../tool/mock_mapboxgl_map';
1414
import '../../resources/WebMapV3.js';
1515
import '../../resources/WebMapV5.js';
16+
var server = 'http://localhost:8190/iportal/';
1617

1718
describe('mapboxgl-webmap3.0', () => {
1819
var originalTimeout, testDiv;
19-
var server = 'http://localhost:8190/iportal/';
2020
var id = 617580084;
2121
var mapstudioWebmap;
2222
const l7LayerUtil = L7LayerUtil({ featureFilter, expression, spec, L7Layer, L7 });
@@ -217,6 +217,7 @@ describe('mapboxgl-webmap3.0', () => {
217217

218218
mapstudioWebmap.on('mapcreatesucceeded', ({ map }) => {
219219
expect(map).not.toBeUndefined();
220+
window.mapstudioWebmap = mapstudioWebmap;
220221
expect(mapstudioWebmap.map).toEqual(map);
221222
expect(mapstudioWebmap._mapInfo.layers[1].filter).toEqual([
222223
'all',
@@ -228,6 +229,35 @@ describe('mapboxgl-webmap3.0', () => {
228229
['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
229230
['all', ['all', ['all', ['any', ['==', ['get', 'smpid'], 121]]]]]
230231
]);
232+
const catelogGroup = [
233+
{
234+
filter: ['all', ['==', 'Ctype', ''], ['!=', 'smpid', '']],
235+
visible: true,
236+
catalogType: 'layer',
237+
layersContent: ['ms_郑州POI_GBK_1774245016129_27']
238+
},
239+
{
240+
catalogType: 'group',
241+
children: [
242+
{
243+
filter: ['all', ['==', 'Ctype1', ''], ['!=', 'smpid1', ''], ['!=', '$type', 'point']],
244+
visible: true,
245+
catalogType: 'layer',
246+
layersContent: ['ms_郑州POI_GBK_1774245104313_105']
247+
}
248+
]
249+
}
250+
];
251+
const result = mapstudioWebmap._getFiltersByCatalog({ catalogs: catelogGroup });
252+
expect(result).toEqual({
253+
ms_郑州POI_GBK_1774245016129_27: ['all', ['==', 'Ctype', ''], ['!=', 'smpid', '']],
254+
ms_郑州POI_GBK_1774245104313_105: ['all', ['==', 'Ctype1', ''], ['!=', 'smpid1', ''], ['!=', '$type', 'point']]
255+
});
256+
const result1 = mapstudioWebmap._getFiltersByCatalog({});
257+
expect(result1).toEqual({});
258+
const result2 = mapstudioWebmap._getPopupInfos({});
259+
expect(result2).toEqual([]);
260+
mapstudioWebmap._changeMapInfoFilter();
231261
done();
232262
});
233263
});
@@ -1727,10 +1757,6 @@ describe('mapboxgl-webmap3.0', () => {
17271757
}
17281758
return Promise.resolve();
17291759
});
1730-
mapstudioWebmap = new WebMap(id, {
1731-
server: server
1732-
});
1733-
17341760
mapstudioWebmap = new WebMap(id, {
17351761
server: server
17361762
});
@@ -8477,8 +8503,247 @@ describe('mapboxgl-webmap3.0', () => {
84778503
done();
84788504
});
84798505
mapstudioWebmap.on('mapcreatefailed', (e) => {
8480-
console.log('mapcreatedfailed', e);
84818506
done();
84828507
});
84838508
});
84848509
});
8510+
8511+
describe('WebMapV3 Filter Methods Additional Tests', () => {
8512+
let webMapV3;
8513+
let testDiv;
8514+
let originalTimeout;
8515+
8516+
beforeEach(() => {
8517+
testDiv = window.document.createElement('div');
8518+
testDiv.setAttribute('id', 'map');
8519+
testDiv.style.styleFloat = 'left';
8520+
testDiv.style.marginLeft = '8px';
8521+
testDiv.style.marginTop = '50px';
8522+
testDiv.style.width = '500px';
8523+
testDiv.style.height = '500px';
8524+
window.document.body.appendChild(testDiv);
8525+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
8526+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
8527+
mapboxgl.Map.prototype.overlayLayersManager = {};
8528+
mbglmap.prototype.getL7Scene = mapboxgl.Map.prototype.getL7Scene;
8529+
mapboxgl.CRS = CRS;
8530+
mapboxgl.proj4 = proj4;
8531+
spyOn(FetchRequest, 'get').and.callFake((url) => {
8532+
if (url.indexOf('web/config/portal.json') > -1) {
8533+
return Promise.resolve(new Response(JSON.stringify(iportal_serviceProxy)));
8534+
}
8535+
if (url.indexOf('map.json') > -1) {
8536+
return Promise.resolve(new Response(mapstudioWebMap_filters));
8537+
}
8538+
if (url.indexOf('932266699.json') > -1) {
8539+
return Promise.resolve(new Response(msProjectINfo_filters));
8540+
}
8541+
if (url.indexOf('/sprites') > -1) {
8542+
return Promise.resolve(new Response(spriteJson));
8543+
}
8544+
return Promise.resolve();
8545+
});
8546+
});
8547+
afterEach(() => {
8548+
if (webMapV3 && webMapV3.map) {
8549+
const webMapV31 = webMapV3._getWebMapInstance ? webMapV3._getWebMapInstance() : webMapV3;
8550+
webMapV31.clean && webMapV31.clean();
8551+
webMapV3 = null;
8552+
}
8553+
window.document.body.removeChild(testDiv);
8554+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
8555+
mbglmap.prototype.getL7Scene = undefined;
8556+
mapboxgl.CRS = undefined;
8557+
mapboxgl.proj4 = undefined;
8558+
revertCRS();
8559+
});
8560+
8561+
it('should return empty array for empty catalogs', () => {
8562+
webMapV3 = new WebMap('932266699', {
8563+
server: server
8564+
});
8565+
webMapV3.on('mapcreatesucceeded', (e) => {
8566+
const result = webMapV3._getFiltersByCatalog({});
8567+
expect(result).toEqual([]);
8568+
const result1 = webMapV3._getFiltersByCatalog(null);
8569+
expect(result1).toEqual([]);
8570+
const catalogs = [
8571+
{
8572+
catalogType: 'layer',
8573+
filter: ['all', ['==', 'Ctype', '']],
8574+
layersContent: ['layer1', 'layer2']
8575+
}
8576+
];
8577+
const result2 = webMapV3._getFiltersByCatalog({ catalogs });
8578+
expect(result2).toEqual({
8579+
layer1: ['all', ['==', 'Ctype', '']],
8580+
layer2: ['all', ['==', 'Ctype', '']]
8581+
});
8582+
8583+
const catalogs1 = [
8584+
{
8585+
catalogType: 'group',
8586+
children: [
8587+
{
8588+
catalogType: 'layer',
8589+
filter: ['all', ['==', 'Ctype', '']],
8590+
layersContent: ['layer1']
8591+
}
8592+
]
8593+
}
8594+
];
8595+
const result3 = webMapV3._getFiltersByCatalog({ catalogs: catalogs1 });
8596+
expect(result3).toEqual({
8597+
layer1: ['all', ['==', 'Ctype', '']]
8598+
});
8599+
const catalogFilter = [
8600+
'all',
8601+
['==', '$type', 'Point'],
8602+
['==', '$id', '123'],
8603+
['==', '$layer', 'layer1'],
8604+
['==', 'smpid', '']
8605+
];
8606+
const keys = webMapV3._collectMatchKeys(catalogFilter);
8607+
expect(keys).not.toContain('$type');
8608+
expect(keys).not.toContain('$id');
8609+
expect(keys).not.toContain('$layer');
8610+
expect(keys).toContain('smpid');
8611+
});
8612+
});
8613+
describe('_changeMapInfoFilter', () => {
8614+
it('should handle empty catalogFilters', () => {
8615+
webMapV3.on('mapcreatesucceeded', (e) => {
8616+
webMapV3._mapInfo = {
8617+
layers: [{ id: 'layer1', filter: ['all', ['==', 'Ctype', '']] }]
8618+
};
8619+
webMapV3._changeMapInfoFilter({});
8620+
expect(webMapV3._mapInfo.layers[0].filter).toEqual(['all', ['==', 'Ctype', '']]);
8621+
});
8622+
});
8623+
8624+
it('should handle null catalogFilters', () => {
8625+
webMapV3.on('mapcreatesucceeded', (e) => {
8626+
webMapV3._mapInfo = {
8627+
layers: [{ id: 'layer1', filter: ['all', ['==', 'Ctype', '']] }]
8628+
};
8629+
webMapV3._changeMapInfoFilter(null);
8630+
expect(webMapV3._mapInfo.layers[0].filter).toEqual(['all', ['==', 'Ctype', '']]);
8631+
});
8632+
});
8633+
it('should transform layer filter when catalog filter exists', () => {
8634+
const catalogFilters = {
8635+
layer1: ['all', ['==', 'Ctype', ''], ['!=', 'smpid', '']]
8636+
};
8637+
webMapV3.on('mapcreatesucceeded', (e) => {
8638+
webMapV3._mapInfo = {
8639+
layers: [
8640+
{ id: 'layer1', filter: ['all', ['==', 'Ctype', ''], ['!=', 'smpid', ''], ['==', 'other', 'value']] }
8641+
]
8642+
};
8643+
webMapV3._changeMapInfoFilter(catalogFilters);
8644+
expect(webMapV3._mapInfo.layers[0].filter[1]).toEqual(['==', ['get', 'Ctype'], '']);
8645+
expect(webMapV3._mapInfo.layers[0].filter[2]).toEqual(['!=', ['get', 'smpid'], '']);
8646+
expect(webMapV3._mapInfo.layers[0].filter[3]).toEqual(['==', 'other', 'value']);
8647+
});
8648+
});
8649+
8650+
it('should not transform layer without filter', () => {
8651+
const catalogFilters = {
8652+
layer1: ['all', ['==', 'Ctype', '']]
8653+
};
8654+
webMapV3.on('mapcreatesucceeded', (e) => {
8655+
webMapV3._mapInfo = {
8656+
layers: [{ id: 'layer1' }]
8657+
};
8658+
webMapV3._changeMapInfoFilter(catalogFilters);
8659+
expect(webMapV3._mapInfo.layers[0].filter).toBeUndefined();
8660+
});
8661+
});
8662+
8663+
it('should not transform when no matching catalog filter', () => {
8664+
const catalogFilters = {
8665+
layer2: ['all', ['==', 'Ctype', '']]
8666+
};
8667+
webMapV3.on('mapcreatesucceeded', (e) => {
8668+
webMapV3._mapInfo = {
8669+
layers: [{ id: 'layer1', filter: ['all', ['==', 'Ctype', '']] }]
8670+
};
8671+
8672+
webMapV3._changeMapInfoFilter(catalogFilters);
8673+
expect(webMapV3._mapInfo.layers[0].filter).toEqual(['all', ['==', 'Ctype', '']]);
8674+
});
8675+
});
8676+
});
8677+
8678+
describe('_transformFilterByMatchKeys', () => {
8679+
it('should handle empty matchKeys', () => {
8680+
const filter = ['all', ['==', 'Ctype', '']];
8681+
webMapV3.on('mapcreatesucceeded', (e) => {
8682+
const result = webMapV3._transformFilterByMatchKeys(filter, []);
8683+
expect(result).toEqual(['all', ['==', 'Ctype', '']]);
8684+
});
8685+
});
8686+
8687+
it('should handle all comparison operators', () => {
8688+
webMapV3.on('mapcreatesucceeded', (e) => {
8689+
const matchKeys = ['prop'];
8690+
expect(webMapV3._transformFilterByMatchKeys(['==', 'prop', 'value'], matchKeys)).toEqual([
8691+
'==',
8692+
['get', 'prop'],
8693+
'value'
8694+
]);
8695+
expect(webMapV3._transformFilterByMatchKeys(['!=', 'prop', 'value'], matchKeys)).toEqual([
8696+
'!=',
8697+
['get', 'prop'],
8698+
'value'
8699+
]);
8700+
expect(webMapV3._transformFilterByMatchKeys(['>', 'prop', 10], matchKeys)).toEqual(['>', ['get', 'prop'], 10]);
8701+
expect(webMapV3._transformFilterByMatchKeys(['<', 'prop', 10], matchKeys)).toEqual(['<', ['get', 'prop'], 10]);
8702+
expect(webMapV3._transformFilterByMatchKeys(['>=', 'prop', 10], matchKeys)).toEqual([
8703+
'>=',
8704+
['get', 'prop'],
8705+
10
8706+
]);
8707+
expect(webMapV3._transformFilterByMatchKeys(['<=', 'prop', 10], matchKeys)).toEqual([
8708+
'<=',
8709+
['get', 'prop'],
8710+
10
8711+
]);
8712+
});
8713+
});
8714+
8715+
it('should handle in and !in operators', () => {
8716+
webMapV3.on('mapcreatesucceeded', (e) => {
8717+
const matchKeys = ['prop'];
8718+
expect(webMapV3._transformFilterByMatchKeys(['in', 'prop', 'A', 'B'], matchKeys)).toEqual([
8719+
'in',
8720+
['get', 'prop'],
8721+
'A',
8722+
'B'
8723+
]);
8724+
expect(webMapV3._transformFilterByMatchKeys(['!in', 'prop', 'A', 'B'], matchKeys)).toEqual([
8725+
'!in',
8726+
['get', 'prop'],
8727+
'A',
8728+
'B'
8729+
]);
8730+
});
8731+
});
8732+
8733+
it('should handle complex nested filters', () => {
8734+
webMapV3.on('mapcreatesucceeded', (e) => {
8735+
const layerFilter = [
8736+
'any',
8737+
['all', ['==', 'Ctype', 'A'], ['!=', 'smpid', '']],
8738+
['none', ['==', '$type', 'Point']]
8739+
];
8740+
const catalogFilter = ['all', ['==', 'Ctype', ''], ['!=', 'smpid', '']];
8741+
const matchKeys = webMapV3._collectMatchKeys(catalogFilter);
8742+
const result = webMapV3._transformFilterByMatchKeys(layerFilter, matchKeys);
8743+
expect(result[1][1]).toEqual(['==', ['get', 'Ctype'], 'A']);
8744+
expect(result[1][2]).toEqual(['!=', ['get', 'smpid'], '']);
8745+
expect(result[2][1]).toEqual(['==', '$type', 'Point']);
8746+
});
8747+
});
8748+
});
8749+
});

0 commit comments

Comments
 (0)