|
1 | | -// import { mount } from '@src/helpers/test' |
2 | | -// import { useIntervalFn } from '@src/vue-use-kit' |
| 1 | +import { mount } from '@src/helpers/test' |
| 2 | +import { ref } from '@src/api' |
| 3 | +import { useIntervalFn } from '@src/vue-use-kit' |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + jest.useFakeTimers() |
| 7 | +}) |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + jest.clearAllTimers() |
| 11 | +}) |
| 12 | + |
| 13 | +const testComponent = (onMount = true) => ({ |
| 14 | + template: ` |
| 15 | + <div> |
| 16 | + <div id="callbackCounter" v-text="callbackCounter" /> |
| 17 | + <div id="isRunning" v-if="isRunning"></div> |
| 18 | + <button id="stop" @click="stop"></button> |
| 19 | + <button id="start" @click="start"></button> |
| 20 | + </div> |
| 21 | + `, |
| 22 | + setup() { |
| 23 | + const callbackCounter = ref(0) |
| 24 | + const { isRunning, start, stop } = useIntervalFn( |
| 25 | + () => { |
| 26 | + callbackCounter.value = callbackCounter.value + 1 |
| 27 | + }, |
| 28 | + 1000, |
| 29 | + onMount |
| 30 | + ) |
| 31 | + return { isRunning, start, stop, callbackCounter } |
| 32 | + } |
| 33 | +}) |
3 | 34 |
|
4 | 35 | describe('useIntervalFn', () => { |
5 | | - it('should do something', () => { |
6 | | - // Add test here |
| 36 | + it('should call setInterval when initialized', () => { |
| 37 | + expect(setInterval).toHaveBeenCalledTimes(0) |
| 38 | + mount(testComponent()) |
| 39 | + jest.advanceTimersByTime(1500) |
| 40 | + expect(setInterval).toHaveBeenCalled() |
| 41 | + }) |
| 42 | + |
| 43 | + it('should call callback several times', async () => { |
| 44 | + const wrapper = mount(testComponent()) |
| 45 | + jest.advanceTimersByTime(25500) |
| 46 | + await wrapper.vm.$nextTick() |
| 47 | + expect(wrapper.find('#callbackCounter').text()).toBe('25') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should not show #isRunning when onMount is false', async () => { |
| 51 | + const wrapper = mount(testComponent(false)) |
| 52 | + jest.advanceTimersByTime(1500) |
| 53 | + |
| 54 | + // Wait for Vue rerender |
| 55 | + await wrapper.vm.$nextTick() |
| 56 | + expect(wrapper.find('#isRunning').exists()).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should show #isRunning when the intervals are called', async () => { |
| 60 | + const wrapper = mount(testComponent()) |
| 61 | + jest.advanceTimersByTime(1500) |
| 62 | + |
| 63 | + // Wait for Vue to append #isReady in the DOM |
| 64 | + await wrapper.vm.$nextTick() |
| 65 | + expect(wrapper.find('#isRunning').exists()).toBe(true) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should show #isRunning when start is called', async () => { |
| 69 | + const wrapper = mount(testComponent(false)) |
| 70 | + jest.advanceTimersByTime(1500) |
| 71 | + |
| 72 | + // Wait for Vue to append #isRunning in the DOM |
| 73 | + await wrapper.vm.$nextTick() |
| 74 | + expect(wrapper.find('#isRunning').exists()).toBe(false) |
| 75 | + wrapper.find('#start').trigger('click') |
| 76 | + jest.advanceTimersByTime(2500) |
| 77 | + |
| 78 | + // Wait for Vue to remove #isRunning from the DOM |
| 79 | + await wrapper.vm.$nextTick() |
| 80 | + expect(wrapper.find('#isRunning').exists()).toBe(true) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should hide #isRunning when stop is called', async () => { |
| 84 | + const wrapper = mount(testComponent()) |
| 85 | + jest.advanceTimersByTime(1500) |
| 86 | + |
| 87 | + // Wait for Vue to append #isRunning in the DOM |
| 88 | + await wrapper.vm.$nextTick() |
| 89 | + expect(wrapper.find('#isRunning').exists()).toBe(true) |
| 90 | + wrapper.find('#stop').trigger('click') |
| 91 | + jest.advanceTimersByTime(2500) |
| 92 | + |
| 93 | + // Wait for Vue to remove #isRunning from the DOM |
| 94 | + await wrapper.vm.$nextTick() |
| 95 | + expect(wrapper.find('#isRunning').exists()).toBe(false) |
7 | 96 | }) |
8 | 97 | }) |
0 commit comments