|
1 | | -// import { mount } from '@src/helpers/test' |
2 | | -// import { useGeolocation } from '@src/vue-use-kit' |
| 1 | +import { mount } from '@src/helpers/test' |
| 2 | +import { useGeolocation } from '@src/vue-use-kit' |
| 3 | + |
| 4 | +let watchPosition: any |
| 5 | +let clearWatch: any |
| 6 | +let getCurrentPosition: any |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + watchPosition = jest.fn() |
| 10 | + clearWatch = jest.fn() |
| 11 | + getCurrentPosition = jest.fn().mockImplementationOnce(success => |
| 12 | + Promise.resolve( |
| 13 | + success({ |
| 14 | + coords: { |
| 15 | + latitude: 51.1, |
| 16 | + longitude: 45.3 |
| 17 | + } |
| 18 | + }) |
| 19 | + ) |
| 20 | + ) |
| 21 | + const inst = ((navigator as any).geolocation = { |
| 22 | + watchPosition, |
| 23 | + clearWatch, |
| 24 | + getCurrentPosition |
| 25 | + }) |
| 26 | +}) |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + jest.clearAllMocks() |
| 30 | +}) |
| 31 | + |
| 32 | +const testComponent = () => ({ |
| 33 | + template: ` |
| 34 | + <div> |
| 35 | + <div id="isTracking" v-if="isTracking"></div> |
| 36 | + <button id="start" @click="start"></button> |
| 37 | + <button id="stop" @click="stop"></button> |
| 38 | + </div> |
| 39 | + `, |
| 40 | + setup() { |
| 41 | + const { isTracking, geo, start, stop } = useGeolocation() |
| 42 | + return { isTracking, geo, start, stop } |
| 43 | + } |
| 44 | +}) |
3 | 45 |
|
4 | 46 | describe('useGeolocation', () => { |
5 | | - it('should do something', () => { |
6 | | - // Add test here |
| 47 | + it('should call getCurrentPosition and watchPosition onMounted', () => { |
| 48 | + expect(getCurrentPosition).toHaveBeenCalledTimes(0) |
| 49 | + expect(watchPosition).toHaveBeenCalledTimes(0) |
| 50 | + mount(testComponent()) |
| 51 | + expect(getCurrentPosition).toHaveBeenCalledTimes(1) |
| 52 | + expect(watchPosition).toHaveBeenCalledTimes(1) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should call clearWatch onUnmounted', () => { |
| 56 | + expect(clearWatch).toHaveBeenCalledTimes(0) |
| 57 | + const wrapper = mount(testComponent()) |
| 58 | + wrapper.vm.$destroy() |
| 59 | + expect(clearWatch).toHaveBeenCalledTimes(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should call getCurrentPosition again when start is called', async () => { |
| 63 | + expect(getCurrentPosition).toHaveBeenCalledTimes(0) |
| 64 | + const wrapper = mount(testComponent()) |
| 65 | + expect(getCurrentPosition).toHaveBeenCalledTimes(1) |
| 66 | + wrapper.find('#stop').trigger('click') |
| 67 | + |
| 68 | + // Wait for Vue to append #start in the DOM |
| 69 | + await wrapper.vm.$nextTick() |
| 70 | + wrapper.find('#start').trigger('click') |
| 71 | + expect(getCurrentPosition).toHaveBeenCalledTimes(2) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should call clearWatch when stop is called', async () => { |
| 75 | + expect(clearWatch).toHaveBeenCalledTimes(0) |
| 76 | + const wrapper = mount(testComponent()) |
| 77 | + wrapper.find('#stop').trigger('click') |
| 78 | + |
| 79 | + // Wait for Vue to append #start in the DOM |
| 80 | + await wrapper.vm.$nextTick() |
| 81 | + expect(clearWatch).toHaveBeenCalledTimes(1) |
7 | 82 | }) |
8 | 83 | }) |
0 commit comments