如何在Vue Jest测试中“模拟” navigato.geolocation.getCurrentposition

时间:2019-01-04 04:54:10

标签: javascript unit-testing vue.js jestjs vuex

我正在尝试在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().....
      })
    })

我如何“模拟”此导航器方法并使测试通过?

0 个答案:

没有答案
相关问题