Skip to content

Commit

Permalink
【UT】Add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
songyumeng committed Sep 18, 2024
1 parent 226d5e2 commit 7ec4ba5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
10 changes: 10 additions & 0 deletions test/unit/__tests__/leaflet/components.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { mount, createLocalVue } from '@vue/test-utils';
import components from '@leaflet/components.ts';

describe('leaflet_components', () => {
it('init', () => {
const localVue = createLocalVue();
localVue.use(components);
expect(localVue.component('SmWebMap')).not.toBeNull();
});
});
20 changes: 20 additions & 0 deletions test/unit/__tests__/leaflet/leaflet-wrapper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import L from 'leaflet';
import '@leaflet/leaflet-wrapper';

describe('leaflet-wrapper', () => {
it('addLayer_removeLayer', () => {
expect(L.Map).not.toBeNull();
const map = new L.Map('map');
map.addLayer(L.tileLayer([50.5, 30.5], {radius: 200}));
expect(map.layersOnMap.length).toBe(1);
expect(map.layersOnMap[0].name.indexOf('sm-custom')).toBe(0);
const layer = L.tileLayer([50.5, 30.5], {radius: 200});
map.addLayer(layer,'test');
expect(map.layersOnMap.length).toBe(2);
expect(map.layersOnMap[1].name).toBe('test');
expect(layer._mapToAdd).not.toBeNull();
map.removeLayer(layer,'test');
expect(map.layersOnMap.length).toBe(1);
expect(layer._mapToAdd).toBeNull();
});
});
4 changes: 3 additions & 1 deletion test/unit/mocks/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ function geti18n() {
function getLanguage() {
return 'zh';
}
function initi18n(){}
var lang = {zh:{}}

export { geti18n, getLanguage };
export { geti18n, getLanguage, lang, initi18n };
33 changes: 24 additions & 9 deletions test/unit/mocks/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,47 @@ const leafletMap = class {
addLayer() {}
invalidateSize() {}
remove() {}
_layers = {}
whenReady() {}
};

class GeoJSON {
static coordsToLatLngs() {}

on = jest.fn();
off = jest.fn();
_layerAdd = jest.fn();
}

class TileLayer {
static extend(options) {
return function() {
options._layerAdd = jest.fn()
return options;
};
}
static BingLayer() {}
_layerAdd = jest.fn()
}

function tileLayer() {
return new TileLayer();
}
tileLayer.wms = jest.fn((url, options) => {
return options.layers;
});
// tileLayer.wms = {jest.fn((url, options) => {
// return options.layers;
// })};
class WMS {
_layerAdd = jest.fn()
}
function wms() {
return new WMS();
}
tileLayer.wms = wms;

module.exports = {
Map: leafletMap,
point: () => {},
latLng: () => {},
point: () => { _layerAdd : jest.fn()},
latLng: () => {_layerAdd : jest.fn()},
map: () => {
return new leafletMap();
},
Expand All @@ -59,6 +71,7 @@ module.exports = {
}
}
},

crs: class {
unproject() {
return [];
Expand All @@ -76,16 +89,18 @@ module.exports = {
TileLayer,
tileLayer,
layerGroup: () => {
return {};
return {_layerAdd : jest.fn()};
},
geoJSON: ()=>{
return new GeoJSON();
},
GeoJSON: GeoJSON,
polyline: () => {},
marker: () => {},
polyline: () => {_layerAdd : jest.fn()},
marker: () => {_layerAdd : jest.fn()},
circleMarker: jest.fn(),
icon: () => {},
circle: jest.fn(),
icon: () => {_layerAdd : jest.fn()},
Icon: {Default:class {static mergeOptions=jest.fn()}},
Popup: () => {},
Evented: class {},
svg: jest.fn()
Expand Down
21 changes: 11 additions & 10 deletions test/unit/mocks/leaflet_iclient.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import { Events } from 'vue-iclient/src/common/_types/event/Events';
var supermap = {
cloudTileLayer: () => {
return {};
return { _layerAdd : jest.fn() };
},
uniqueThemeLayer: () => {
return { addFeatures: () => {} };
return { addFeatures: () => {}, _layerAdd : jest.fn() };
},
rangeThemeLayer: () => {
return { addFeatures: () => {} };
return { addFeatures: () => {}, _layerAdd : jest.fn() };
},
labelThemeLayer: () => {
return { addFeatures: () => {} };
return { addFeatures: () => {}, _layerAdd : jest.fn() };
},
heatMapLayer: () => {
return { addFeatures: () => {} };
return { addFeatures: () => {}, _layerAdd : jest.fn() };
},
baiduTileLayer: () => {
return {};
return { _layerAdd : jest.fn() };
},
tiledMapLayer: () => {
return {};
return { _layerAdd : jest.fn() };
},
dataFlowLayer: () => {
return {
on: () => {},
off: () => {},
addLayer: () => {},
getLayerId: () => {}
getLayerId: () => {},
_layerAdd : jest.fn()
};
},
tiandituTileLayer: () => {
return {};
return { _layerAdd : jest.fn() };
},
themeFeature: class {},
wmtsLayer: () => {},
wmtsLayer: () => { _layerAdd : jest.fn() },
createWebMapV2BaseExtending(SuperClass = Events, fireField = 'triggerEvent') {
return class WebMapBase extends SuperClass {
constructor(id, options, mapOptions) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jest.mock('@libs/mapbox-gl-draw/mapbox-gl-draw.js', () => require('./mocks/mapbo
jest.mock('@libs/mapboxgl/mapbox-gl-enhance.js', () => require('./mocks/mapboxgl').mapboxgl);
jest.mock('@libs/deckgl/deck.gl.min', () => require());
jest.mock('@libs/iclient-mapboxgl/iclient-mapboxgl.min.js', () => require('./mocks/mapboxgl_iclient'));
jest.mock('@leaflet/leaflet-wrapper', () => require('./mocks/leaflet'));
jest.mock('leaflet', () => require('./mocks/leaflet'));
jest.mock('@libs/iclient-leaflet/iclient-leaflet.min', () => require('./mocks/leaflet_iclient'));

0 comments on commit 7ec4ba5

Please sign in to comment.