我正在尝试在vue中测试我的Google Maps组件,并且navigator.geolocation.getCurrentPosition()在方法内
该测试失败,在控制台中将其打印出来:
错误: TypeError:无法读取未定义的属性“ getCurrentPosition”
167 | },
168 | geolocate: function() {
> 169 | navigator.geolocation.getCurrentPosition(position => {
| ^
这是我的代码
export default {
name: "GoogleMap",
data() {
},
created() {
},
mounted() {
this.geolocate();
},
methods: {
..............
geolocate: function() {
navigator.geolocation.getCurrentPosition(position => {
this.center = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
});
...............
},
computed: {
}
};
</script>
我试图嘲笑它,但它没有效果
describe("Maps.vue", () => {
it("renders ... Google maps ", () => {
const wrapper = shallowMount(Maps, {
mocks : {
getCurrentPosition:()=> jest.fn()
},
})
expect().....
})
})
我如何“模拟”此导航器方法并使测试通过?