From 7ec4ba50dd6e2cf9f59218c3403f372a0f5a9747 Mon Sep 17 00:00:00 2001 From: songyumeng Date: Wed, 18 Sep 2024 15:48:39 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90UT=E3=80=91Add=20UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/__tests__/leaflet/components.spec.js | 10 ++++++ .../__tests__/leaflet/leaflet-wrapper.spec.js | 20 +++++++++++ test/unit/mocks/i18n.js | 4 ++- test/unit/mocks/leaflet.js | 33 ++++++++++++++----- test/unit/mocks/leaflet_iclient.js | 21 ++++++------ test/unit/setup.js | 2 +- 6 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 test/unit/__tests__/leaflet/components.spec.js create mode 100644 test/unit/__tests__/leaflet/leaflet-wrapper.spec.js diff --git a/test/unit/__tests__/leaflet/components.spec.js b/test/unit/__tests__/leaflet/components.spec.js new file mode 100644 index 00000000..99e75425 --- /dev/null +++ b/test/unit/__tests__/leaflet/components.spec.js @@ -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(); + }); +}); diff --git a/test/unit/__tests__/leaflet/leaflet-wrapper.spec.js b/test/unit/__tests__/leaflet/leaflet-wrapper.spec.js new file mode 100644 index 00000000..09a98a8a --- /dev/null +++ b/test/unit/__tests__/leaflet/leaflet-wrapper.spec.js @@ -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(); + }); +}); diff --git a/test/unit/mocks/i18n.js b/test/unit/mocks/i18n.js index 44d983d2..2270a54e 100644 --- a/test/unit/mocks/i18n.js +++ b/test/unit/mocks/i18n.js @@ -15,5 +15,7 @@ function geti18n() { function getLanguage() { return 'zh'; } +function initi18n(){} +var lang = {zh:{}} -export { geti18n, getLanguage }; +export { geti18n, getLanguage, lang, initi18n }; diff --git a/test/unit/mocks/leaflet.js b/test/unit/mocks/leaflet.js index 9abf8d82..8cae3d50 100644 --- a/test/unit/mocks/leaflet.js +++ b/test/unit/mocks/leaflet.js @@ -11,6 +11,8 @@ const leafletMap = class { addLayer() {} invalidateSize() {} remove() {} + _layers = {} + whenReady() {} }; class GeoJSON { @@ -18,28 +20,38 @@ class GeoJSON { 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(); }, @@ -59,6 +71,7 @@ module.exports = { } } }, + crs: class { unproject() { return []; @@ -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() diff --git a/test/unit/mocks/leaflet_iclient.js b/test/unit/mocks/leaflet_iclient.js index bc021474..26e80f50 100644 --- a/test/unit/mocks/leaflet_iclient.js +++ b/test/unit/mocks/leaflet_iclient.js @@ -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) { diff --git a/test/unit/setup.js b/test/unit/setup.js index af5b226e..892901e2 100644 --- a/test/unit/setup.js +++ b/test/unit/setup.js @@ -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')); \ No newline at end of file