forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitMapSpec.js
More file actions
218 lines (210 loc) · 7.48 KB
/
InitMapSpec.js
File metadata and controls
218 lines (210 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import mapboxgl from 'mapbox-gl';
import mbglmap from '../../tool/mock_mapboxgl_map';
import { initMap } from '../../../src/mapboxgl/mapping/InitMap';
import { FetchRequest } from '../../../src/common/util/FetchRequest';
describe('InitMap', () => {
let originalTimeout, testDiv;
beforeEach(() => {
spyOn(mapboxgl, 'Map').and.callFake(mbglmap);
testDiv = window.document.createElement('div');
testDiv.setAttribute('id', 'map');
testDiv.style.styleFloat = 'left';
testDiv.style.marginLeft = '8px';
testDiv.style.marginTop = '50px';
testDiv.style.width = '500px';
testDiv.style.height = '500px';
window.document.body.appendChild(testDiv);
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
});
afterEach(() => {
window.document.body.removeChild(testDiv);
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});
it('initMap plane coordinate system', async () => {
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';
const mapServiceInfo = {
dynamicProjection: false,
prjCoordSys: {
epsgCode: 0,
type: 'PCS_NON_EARTH'
}
};
spyOn(FetchRequest, 'get').and.callFake(() => {
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
});
try {
await initMap(url);
} catch (error) {
expect(error).toEqual(new Error('mapbox-gl cannot support plane coordinate system.'));
}
});
it('initMap 4490, dynamicProjection false, non mapbox-gl-enhance', async () => {
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';
const mapServiceInfo = {
dynamicProjection: false,
prjCoordSys: {
epsgCode: 4490
},
bounds: {
top: 20037508.342789087,
left: -20037508.342789248,
bottom: -20037508.34278914,
leftBottom: {
x: -20037508.342789248,
y: -20037508.34278914
},
right: 20037508.342789244,
rightTop: {
x: 20037508.342789244,
y: 20037508.342789087
}
},
center: {
x: -7.450580596923828e-9,
y: -2.60770320892334e-8
}
};
spyOn(FetchRequest, 'get').and.callFake(() => {
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
});
try {
await initMap(url);
} catch (error) {
expect(error).toEqual(
new Error(
'The EPSG code 4490 needs to include mapbox-gl-enhance.js. Refer to the example: https://iclient.supermap.io/examples/mapboxgl/editor.html#mvtVectorTile_2362'
)
);
}
});
it('initMap 3857, non mapbox-gl-enhance', async () => {
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';
const mapServiceInfo = {
dynamicProjection: false,
prjCoordSys: {
epsgCode: 3857
},
bounds: {
top: 20037508.342789087,
left: -20037508.342789248,
bottom: -20037508.34278914,
leftBottom: {
x: -20037508.342789248,
y: -20037508.34278914
},
right: 20037508.342789244,
rightTop: {
x: 20037508.342789244,
y: 20037508.342789087
}
},
center: {
x: -7.450580596923828e-9,
y: -2.60770320892334e-8
}
};
spyOn(FetchRequest, 'get').and.callFake(() => {
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
});
const resData = await initMap(url);
const map = resData.map;
expect(map).not.toBeUndefined();
expect(map.options.crs).toBe('EPSG:3857');
expect(map.options.center).toEqual([mapServiceInfo.center.x, mapServiceInfo.center.y]);
expect(Object.values(map.options.style.sources).length).toBe(1);
expect(map.options.style.layers.length).toBe(1);
expect(Object.values(map.options.style.sources)[0].tiles.length).toBe(1);
expect(Object.values(map.options.style.sources)[0].tiles[0]).toBe(
url + '/zxyTileImage.png?z={z}&x={x}&y={y}&width=256&height=256&transparent=true'
);
});
it('initMap 4326, dynamicProjection true, non mapbox-gl-enhance', async () => {
const url = 'http:/fake:8090/iserver/iserver/services/map-china400/rest/maps/China';
const mapServiceInfo = {
dynamicProjection: true,
prjCoordSys: {
epsgCode: 4326
},
bounds: {
top: 20037508.342789087,
left: -20037508.342789248,
bottom: -20037508.34278914,
leftBottom: {
x: -20037508.342789248,
y: -20037508.34278914
},
right: 20037508.342789244,
rightTop: {
x: 20037508.342789244,
y: 20037508.342789087
}
},
center: {
x: -7.450580596923828e-9,
y: -2.60770320892334e-8
}
};
spyOn(FetchRequest, 'get').and.callFake(() => {
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
});
const resData = await initMap(url);
const map = resData.map;
expect(map).not.toBeUndefined();
expect(map.options.crs).toBe('EPSG:3857');
expect(map.options.center).toEqual([mapServiceInfo.center.x, mapServiceInfo.center.y]);
expect(Object.values(map.options.style.sources).length).toBe(1);
expect(map.options.style.layers.length).toBe(1);
expect(Object.values(map.options.style.sources)[0].tiles.length).toBe(1);
expect(Object.values(map.options.style.sources)[0].tiles[0]).toBe(
url + '/zxyTileImage.png?z={z}&x={x}&y={y}&width=256&height=256&transparent=true'
);
});
it('initMap 4490, dynamicProjection false, mapbox-gl-enhance', async () => {
const url = 'http:/fake:8090/iserver/services/map-mvt-landuse/rest/maps/landuse';
const mapServiceInfo = {
dynamicProjection: false,
prjCoordSys: {
epsgCode: 4490
},
center: {
x: -7.450580596923828e-9,
y: -2.60770320892334e-8
}
};
const vectorstylesInfo = {
metadata: {
indexbounds: [-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
}
};
const vectorStyleUrl =
'http:/fake:8090/iserver/services/map-mvt-landuse/rest/maps/landuse/tileFeature/vectorstyles.json?type=MapBox_GL&styleonly=true&tileURLTemplate=ZXY';
const WKT =
'GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]]';
spyOn(FetchRequest, 'get').and.callFake((url) => {
if (url.includes('/prjCoordSys.wkt')) {
return Promise.resolve(new Response(WKT));
}
if (url === vectorStyleUrl) {
return Promise.resolve(new Response(JSON.stringify(vectorstylesInfo)));
}
return Promise.resolve(new Response(JSON.stringify(mapServiceInfo)));
});
mapboxgl.CRS = function () {
return {
code: mapServiceInfo.prjCoordSys.epsgCode
};
};
mapboxgl.proj4 = function () {
return [0, 0];
};
const resData = await initMap(url, { type: 'vector-tile' });
const map = resData.map;
expect(map).not.toBeUndefined();
expect(map.options.crs.code).toBe(mapServiceInfo.prjCoordSys.epsgCode);
expect(map.options.center).not.toEqual([mapServiceInfo.center.x, mapServiceInfo.center.y]);
expect(map.options.style).toBe(vectorStyleUrl);
delete mapboxgl.CRS;
delete mapboxgl.proj4;
});
});