|
| 1 | +import { mount } from '@src/helpers/test' |
| 2 | +import { useMouseLeavePage } 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="hasLeftPage" v-if="hasLeftPage"></div> |
| 13 | + <button id="start" @click="start"></button> |
| 14 | + <button id="stop" @click="stop"></button> |
| 15 | + </div> |
| 16 | + `, |
| 17 | + setup() { |
| 18 | + const { hasLeftPage, isTracking, start, stop } = useMouseLeavePage(onMount) |
| 19 | + return { hasLeftPage, isTracking, start, stop } |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +describe('useMouseLeavePage', () => { |
| 24 | + const eventName = 'mouseout' |
| 25 | + it('should call mouseout onMounted', async () => { |
| 26 | + const addEventListenerSpy = jest.spyOn(document, 'addEventListener') |
| 27 | + const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener') |
| 28 | + const wrapper = mount(testComponent()) |
| 29 | + await wrapper.vm.$nextTick() |
| 30 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(1) |
| 31 | + expect(addEventListenerSpy).toBeCalledWith(eventName, expect.any(Function)) |
| 32 | + |
| 33 | + // Destroy instance to check if the remove event listener is being called |
| 34 | + wrapper.destroy() |
| 35 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(1) |
| 36 | + expect(removeEventListenerSpy).toBeCalledWith( |
| 37 | + eventName, |
| 38 | + expect.any(Function) |
| 39 | + ) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should call document.addEventListener again when start is called', async () => { |
| 43 | + const addEventListenerSpy = jest.spyOn(document, 'addEventListener') |
| 44 | + const wrapper = mount(testComponent()) |
| 45 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(1) |
| 46 | + wrapper.find('#stop').trigger('click') |
| 47 | + |
| 48 | + // Wait for Vue to append #start in the DOM |
| 49 | + await wrapper.vm.$nextTick() |
| 50 | + wrapper.find('#start').trigger('click') |
| 51 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(1 * 2) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should call document.removeEventListener when stop is called', async () => { |
| 55 | + const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener') |
| 56 | + const wrapper = mount(testComponent()) |
| 57 | + wrapper.find('#stop').trigger('click') |
| 58 | + |
| 59 | + // Wait for Vue to append #start in the DOM |
| 60 | + await wrapper.vm.$nextTick() |
| 61 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(1) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should show #isTracking when onMount is true', async () => { |
| 65 | + const wrapper = mount(testComponent(true)) |
| 66 | + await wrapper.vm.$nextTick() |
| 67 | + expect(wrapper.find('#isTracking').exists()).toBe(true) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not show #isTracking when onMount is false', async () => { |
| 71 | + const wrapper = mount(testComponent(false)) |
| 72 | + await wrapper.vm.$nextTick() |
| 73 | + expect(wrapper.find('#isTracking').exists()).toBe(false) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should not show #hasLeftPage when onMount is false', async () => { |
| 77 | + const wrapper = mount(testComponent(false)) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + expect(wrapper.find('#hasLeftPage').exists()).toBe(false) |
| 80 | + }) |
| 81 | +}) |
0 commit comments