|
| 1 | +import { useWindowSize } from '@src/vue-use-kit' |
| 2 | +import { |
| 3 | + checkElementExistenceOnMount, |
| 4 | + checkOnMountAndUnmountEvents, |
| 5 | + checkOnStartEvents, |
| 6 | + checkOnStopEvents, |
| 7 | + mount |
| 8 | +} from '@src/helpers/test' |
| 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="width" v-if="width">{{width}}</div> |
| 19 | + <div id="height" v-if="height">{{height}}</div> |
| 20 | + <button id="start" @click="start"></button> |
| 21 | + <button id="stop" @click="stop"></button> |
| 22 | + </div> |
| 23 | + `, |
| 24 | + setup() { |
| 25 | + const { width, height, isTracking, start, stop } = useWindowSize(onMount) |
| 26 | + return { width, height, isTracking, start, stop } |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +describe('useWindowSize', () => { |
| 31 | + const events = ['resize'] |
| 32 | + |
| 33 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 34 | + await checkOnMountAndUnmountEvents(window, events, testComponent) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should add events again when start is called', async () => { |
| 38 | + await checkOnStartEvents(window, events, testComponent) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should remove events when stop is called', async () => { |
| 42 | + await checkOnStopEvents(window, events, testComponent) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should show #isTracking when runOnMount is true', async () => { |
| 46 | + await checkElementExistenceOnMount(true, testComponent(true)) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 50 | + await checkElementExistenceOnMount(false, testComponent(false)) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should display the width and height values', async () => { |
| 54 | + const wrapper = mount(testComponent(true)) |
| 55 | + await wrapper.vm.$nextTick() |
| 56 | + expect(wrapper.find('#width').text()).toEqual(`${window.innerWidth}`) |
| 57 | + expect(wrapper.find('#height').text()).toEqual(`${window.innerHeight}`) |
| 58 | + }) |
| 59 | +}) |
0 commit comments