|
| 1 | +import { mount } from '@src/helpers/test' |
| 2 | +import { useLocation } from '@src/vue-use-kit' |
| 3 | + |
| 4 | +afterEach(() => { |
| 5 | + jest.clearAllMocks() |
| 6 | +}) |
| 7 | + |
| 8 | +const testComponent = (onMount = true) => ({ |
| 9 | + template: ` |
| 10 | + <div> |
| 11 | + <div id="isTracking" v-if="isTracking"></div> |
| 12 | + <div id="locationState">{{JSON.stringify(locationState)}}</div> |
| 13 | + <button id="start" @click="start"></button> |
| 14 | + <button id="stop" @click="stop"></button> |
| 15 | + </div> |
| 16 | + `, |
| 17 | + setup() { |
| 18 | + const { locationState, isTracking, start, stop } = useLocation(onMount) |
| 19 | + return { locationState, isTracking, start, stop } |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +describe('useLocation', () => { |
| 24 | + const locationStateKeys = [ |
| 25 | + 'trigger', |
| 26 | + 'state', |
| 27 | + 'length', |
| 28 | + 'hash', |
| 29 | + 'host', |
| 30 | + 'hostname', |
| 31 | + 'href', |
| 32 | + 'origin', |
| 33 | + 'pathname', |
| 34 | + 'port', |
| 35 | + 'protocol', |
| 36 | + 'search' |
| 37 | + ] |
| 38 | + const events = ['popstate', 'pushstate', 'replacestate'] |
| 39 | + |
| 40 | + it('should call popstate, pushstate and replacestate onMounted', async () => { |
| 41 | + const addEventListenerSpy = jest.spyOn(window, 'addEventListener') |
| 42 | + expect(addEventListenerSpy).not.toHaveBeenCalled() |
| 43 | + const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener') |
| 44 | + const wrapper = mount(testComponent()) |
| 45 | + await wrapper.vm.$nextTick() |
| 46 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 47 | + events.forEach(event => { |
| 48 | + expect(addEventListenerSpy).toBeCalledWith(event, expect.any(Function)) |
| 49 | + }) |
| 50 | + |
| 51 | + // Destroy instance to check if the remove event listener is being called |
| 52 | + wrapper.destroy() |
| 53 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 54 | + events.forEach(event => { |
| 55 | + expect(removeEventListenerSpy).toBeCalledWith(event, expect.any(Function)) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should call document.addEventListener again when start is called', async () => { |
| 60 | + const addEventListenerSpy = jest.spyOn(window, 'addEventListener') |
| 61 | + const wrapper = mount(testComponent()) |
| 62 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 63 | + wrapper.find('#stop').trigger('click') |
| 64 | + |
| 65 | + // Wait for Vue to append #start in the DOM |
| 66 | + await wrapper.vm.$nextTick() |
| 67 | + wrapper.find('#start').trigger('click') |
| 68 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length * 2) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should call document.removeEventListener when stop is called', async () => { |
| 72 | + const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener') |
| 73 | + const wrapper = mount(testComponent()) |
| 74 | + wrapper.find('#stop').trigger('click') |
| 75 | + |
| 76 | + // Wait for Vue to append #start in the DOM |
| 77 | + await wrapper.vm.$nextTick() |
| 78 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should show #isTracking when onMount is true', async () => { |
| 82 | + const wrapper = mount(testComponent(true)) |
| 83 | + await wrapper.vm.$nextTick() |
| 84 | + expect(wrapper.find('#isTracking').exists()).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should not show #isTracking when onMount is false', async () => { |
| 88 | + const wrapper = mount(testComponent(false)) |
| 89 | + await wrapper.vm.$nextTick() |
| 90 | + expect(wrapper.find('#isTracking').exists()).toBe(false) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should display the locationState object', async () => { |
| 94 | + const wrapper = mount(testComponent(true)) |
| 95 | + await wrapper.vm.$nextTick() |
| 96 | + locationStateKeys.forEach(locationKey => { |
| 97 | + expect(wrapper.text().includes(locationKey)).toBe(true) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments