|
| 1 | +import { |
| 2 | + checkElementExistenceOnMount, |
| 3 | + checkOnStartEvents, |
| 4 | + checkOnStopEvents, |
| 5 | + mount |
| 6 | +} from '@src/helpers/test' |
| 7 | +import { ref } from '@src/api' |
| 8 | +import { useScroll } from '@src/vue-use-kit' |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + jest.clearAllMocks() |
| 12 | +}) |
| 13 | + |
| 14 | +const testComponent = (onMount = true) => ({ |
| 15 | + template: ` |
| 16 | + <div> |
| 17 | + <div id="isTracking" v-if="isTracking"></div> |
| 18 | + <div id="isScrolling" v-if="isScrolling"></div> |
| 19 | + <div id="x" v-if="x === 0">{{ x }}px</div> |
| 20 | + <div id="y" v-if="y === 0">{{ y }}px</div> |
| 21 | + <button id="start" @click="start"></button> |
| 22 | + <button id="stop" @click="stop"></button> |
| 23 | + </div> |
| 24 | + `, |
| 25 | + setup() { |
| 26 | + const { x, y, isScrolling, isTracking, start, stop } = useScroll( |
| 27 | + ref(window), |
| 28 | + 150, |
| 29 | + onMount |
| 30 | + ) |
| 31 | + return { x, y, isScrolling, isTracking, start, stop } |
| 32 | + } |
| 33 | +}) |
| 34 | + |
| 35 | +describe('useScroll', () => { |
| 36 | + const events = ['scroll'] |
| 37 | + |
| 38 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 39 | + const addEventListenerSpy = jest.spyOn(window, 'addEventListener') |
| 40 | + const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener') |
| 41 | + const wrapper = mount(testComponent()) |
| 42 | + await wrapper.vm.$nextTick() |
| 43 | + expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 44 | + events.forEach(event => { |
| 45 | + expect(addEventListenerSpy).toBeCalledWith( |
| 46 | + event, |
| 47 | + expect.any(Function), |
| 48 | + expect.any(Object) |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + // Destroy instance to check if the remove event listener is being called |
| 53 | + wrapper.destroy() |
| 54 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 55 | + events.forEach(event => { |
| 56 | + expect(removeEventListenerSpy).toBeCalledWith(event, expect.any(Function)) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should add events again when start is called', async () => { |
| 61 | + await checkOnStartEvents(window, events, testComponent) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should remove events when stop is called', async () => { |
| 65 | + await checkOnStopEvents(window, events, testComponent) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should show #isTracking when runOnMount is true', async () => { |
| 69 | + await checkElementExistenceOnMount(true, testComponent(true)) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 73 | + await checkElementExistenceOnMount(false, testComponent(false)) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should show #x and #y elements and not show #isScrolling', async () => { |
| 77 | + const wrapper = mount(testComponent()) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + expect(wrapper.find('#x').exists()).toBe(true) |
| 80 | + expect(wrapper.find('#y').exists()).toBe(true) |
| 81 | + expect(wrapper.find('#isScrolling').exists()).toBe(false) |
| 82 | + }) |
| 83 | +}) |
0 commit comments