diff --git a/pages/component/map/map.test.js b/pages/component/map/map.test.js index 1a5c8e8c3481cf772e2657b2f5bef94aa895f263..545eff51557f2f430961d8ef8259af5f7a0f37bc 100644 --- a/pages/component/map/map.test.js +++ b/pages/component/map/map.test.js @@ -1,6 +1,5 @@ let page; describe('web-map', () => { - console.log("uniTestPlatformInfo",process.env.uniTestPlatformInfo) if (!process.env.uniTestPlatformInfo.startsWith('web')) { it('app', () => { expect(1).toBe(1) @@ -12,52 +11,63 @@ describe('web-map', () => { await page.waitFor('view'); // 等待地图加载完成 await page.waitFor(4000); - await page.setData({autoTest:true}) + await page.callMethod('updateAutoTest',true) }); it('Check MapMethods', async () => { - const mapMethods = ['changeScale', 'addMarkers', 'addPolyline', 'addPolygons', 'addCircles', - 'includePoint', 'handleTranslateMarker' - ] + const mapMethods = ['addControls', 'addMarkers', 'addMarkersLabel','addPolyline', 'addPolygons', 'addCircles','includePoint'] for (var i = 0; i < mapMethods.length; i++) { await page.callMethod(mapMethods[i]) - if (mapMethods[i] == 'handleTranslateMarker') { - await page.waitFor(2000); - } else { - await page.waitFor(500); - } - expect(await program.screenshot()).toSaveImageSnapshot(); + await page.waitFor(500); + expect(await program.screenshot()).toSaveImageSnapshot({customSnapshotIdentifier() { + return 'map-' + mapMethods[i] + }}); await page.waitFor(500); } }); + it('handleGetCenterLocation', async () => { await page.callMethod('handleGetCenterLocation') await page.waitFor(500); - const centerLocationRes = await page.data('getCenterLocationTest') - expect(centerLocationRes.latitude).not.toBeNull(); - expect(centerLocationRes.longitude).not.toBeNull(); + const centerLocationRes = await page.data('jestResult') + expect(centerLocationRes.centerPoints.latitude).not.toBeNull(); + expect(centerLocationRes.centerPoints.longitude).not.toBeNull(); }); + it('handleGetRegion', async () => { await page.callMethod('handleGetRegion') await page.waitFor(500); - const regionRes = await page.data('getRegionTest') - console.log('regionRes', regionRes); + const regionRes = await page.data('jestResult') const {southwest,northeast} = regionRes; - const southwestExp ={ latitude: 39.88334279187766, longitude: 116.31050146728515 } - const northeastExp ={ latitude: 40.0149408585477, longitude: 116.56799353271484 } - // 在Safari中返回的经纬度 - // southwest: { latitude: 39.64483423532459, longitude: 115.88249286914063 }, - // northeast: { latitude: 40.17153735946949, longitude: 116.91246113085938 }, - if(!process.env.uniTestPlatformInfo.includes('Safari')){ - expect(southwest.latitude).toBeCloseTo(southwestExp.latitude, 3); - expect(southwest.longitude).toBeCloseTo(southwestExp.longitude, 3); - expect(northeast.latitude).toBeCloseTo(northeastExp.latitude, 3); - expect(northeast.longitude).toBeCloseTo(northeastExp.longitude, 3); - }else{ - expect(southwest.longitude).not.toBeFalsy(); - expect(southwest.latitude).not.toBeFalsy(); - expect(northeastExp.longitude).not.toBeFalsy(); - expect(northeastExp.latitude).not.toBeFalsy(); - } + expect(southwest.latitude).not.toBeFalsy(); + expect(southwest.longitude).not.toBeFalsy(); + expect(northeast.latitude).not.toBeFalsy(); + expect(northeast.longitude).not.toBeFalsy(); }); + + it('handleTranslateMarker', async () => { + await page.callMethod('handleTranslateMarker') + await page.waitFor(2000); + expect(await program.screenshot()).toSaveImageSnapshot(); + const translateMarkerRes = await page.data('jestResult') + expect(translateMarkerRes.animationEnd).toBeTruthy(); + expect(translateMarkerRes.translateMarkerMsg).toBe('translateMarker:ok'); + }); + + it('handleMoveToLocation', async () => { + await page.callMethod('handleMoveToLocation') + await page.waitFor(500); + const moveToLocationRes = await page.data('jestResult') + expect(moveToLocationRes.moveToLocationMsg).toBe("moveToLocation:ok"); + }); + + it('handleGetScale', async () => { + await page.callMethod('handleGetScale') + await page.waitFor(500); + const scaleRes = await page.data('jestResult') + expect(scaleRes.scale).toBeGreaterThanOrEqual(5); + expect(scaleRes.scale).toBeLessThanOrEqual(18); + console.log("jestResult",await page.data()) + }); + }); diff --git a/pages/component/map/map.uvue b/pages/component/map/map.uvue index 60025f01ed3fcfcbfd8ec7f65012973902cebc04..38dcbc448d15f635ce9e79332408b0fa901bff6c 100644 --- a/pages/component/map/map.uvue +++ b/pages/component/map/map.uvue @@ -126,6 +126,16 @@ clickable?: boolean; } + type TypeJestResult = { + translateMarkerMsg:string, + animationEnd:boolean, + centerPoints: Points, + southwest: Points, + northeast: Points, + moveToLocationMsg:string, + scale:number + } + const testMarkers = [{ id: 0, latitude: 39.989631, @@ -314,7 +324,32 @@ const location = ref({ longitude: 116.39742, latitude: 39.909 }); const rotate = ref(0); const skew = ref(0); + // 自动化测试 const autoTest = ref(false); + const updateAutoTest = (value: boolean) => { + autoTest.value = value + } + + + const jestResult = reactive({ + translateMarkerMsg:"", + animationEnd:false, + centerPoints: { + latitude : 0, + longitude : 0 + }, + southwest: { + latitude : 0, + longitude : 0 + }, + northeast: { + latitude : 0, + longitude : 0 + }, + moveToLocationMsg:"", + scale:0, + } as TypeJestResult); + onReady(() => { map.value = uni.createMapContext("map1", getCurrentInstance()!.proxy!) @@ -434,13 +469,12 @@ }; - const getCenterLocationTest = ref({}); const handleGetCenterLocation = () => { if (map.value) { map.value.getCenterLocation({ success: ret => { // console.log('getCenterLocation',ret); - getCenterLocationTest.value = ret; + jestResult.centerPoints = ret; if(!autoTest.value){ uni.showModal({ content: JSON.stringify(ret) @@ -451,13 +485,13 @@ } }; - const getRegionTest = ref({}); const handleGetRegion = () => { if (map.value) { map.value.getRegion({ success: ret => { // console.log('getRegion',JSON.stringify(ret)); - getRegionTest.value = ret; + jestResult.southwest = ret.southwest; + jestResult.northeast = ret.northeast if(!autoTest.value){ uni.showModal({ content: JSON.stringify(ret) @@ -481,10 +515,12 @@ rotate:10, duration: 2000, animationEnd: () => { - console.log('动画结束'); + // console.log('动画结束'); + jestResult.animationEnd = true; }, success: ret => { - console.log('handleTranslateMarker',JSON.stringify(ret)); + // console.log('handleTranslateMarker',JSON.stringify(ret)); + jestResult.translateMarkerMsg = ret.errMsg; }, fail: error => { console.log(error) @@ -500,6 +536,7 @@ success: res => { // console.log('getScale',res); scale.value = res.scale + jestResult.scale = res.scale if(!autoTest.value){ uni.showModal({ content: '当前地图的缩放级别为:'+ res.scale @@ -520,6 +557,7 @@ longitude: 116.39742, success: res => { // console.log('moveToLocation',res); + jestResult.moveToLocationMsg = res.errMsg; if(!autoTest.value){ uni.showModal({ content: JSON.stringify(res) @@ -576,6 +614,24 @@ }); }; + defineExpose({ + jestResult, + autoTest, + updateAutoTest, + addControls, + addMarkers, + addMarkersLabel, + addPolyline, + addPolygons, + addCircles, + includePoint, + handleGetCenterLocation, + handleGetRegion, + handleTranslateMarker, + handleMoveToLocation, + handleGetScale + }) +