Skip to content

Commit f1bfcd9

Browse files
committed
improvement(useGeolocation): Add tests for useGeolocation function
1 parent c237580 commit f1bfcd9

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

src/components/useFullscreen/useFullscreen.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const testComponent = () => ({
2121
})
2222

2323
describe('useFullscreen', () => {
24-
it('should not be fullscreen when initialized', () => {
24+
it('should not be fullscreen onMounted', () => {
2525
const wrapper = mount(testComponent())
2626
expect(wrapper.find('#isFullscreen').exists()).toBe(false)
2727
})
Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,83 @@
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+
})
345

446
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)
782
})
883
})

src/components/useIntersection/useIntersection.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const testComponent = (onMount = true) => ({
3131
})
3232

3333
describe('useIntersection', () => {
34-
it('should call IntersectionObserver when initialized', () => {
34+
it('should call IntersectionObserver onMounted', () => {
3535
expect(observe).toHaveBeenCalledTimes(0)
3636
mount(testComponent())
3737
expect(observe).toHaveBeenCalledTimes(1)

src/components/useIntervalFn/useIntervalFn.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const testComponent = (onMount = true) => ({
3333
})
3434

3535
describe('useIntervalFn', () => {
36-
it('should call setInterval when initialized', () => {
36+
it('should call setInterval onMounted', () => {
3737
expect(setInterval).toHaveBeenCalledTimes(0)
3838
mount(testComponent())
3939
jest.advanceTimersByTime(1500)

src/components/useTimeoutFn/useTimeoutFn.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const testComponent = (onMount = true) => ({
3535
})
3636

3737
describe('useTimeoutFn', () => {
38-
it('should call setTimeout when initialized', () => {
38+
it('should call setTimeout onMounted', () => {
3939
expect(setTimeout).toHaveBeenCalledTimes(0)
4040
mount(testComponent())
4141
expect(setTimeout).toHaveBeenCalledTimes(1)

0 commit comments

Comments
 (0)