Skip to content

Commit 9114402

Browse files
committed
improvement: Add tests to useRafFn
1 parent 8e81d34 commit 9114402

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed
Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1+
import { mount } from '../../helpers/test'
2+
import { ref } from '../../api'
3+
import { useRafFn } from '../../vue-use-kit'
4+
5+
let rafSpy: any = null
6+
// Logic to avoid infinite loop
7+
const totCalls = 4
8+
let count = 0
9+
beforeEach(() => {
10+
window.requestAnimationFrame = (cb: any) => {
11+
if (count >= totCalls) return
12+
count++
13+
return cb()
14+
}
15+
rafSpy = jest.spyOn(window, 'requestAnimationFrame')
16+
})
17+
18+
afterEach(() => {
19+
count = 0
20+
rafSpy.mockClear()
21+
})
22+
23+
const testComponent = (onMount = false) => ({
24+
template: `
25+
<div>
26+
<div id="isRunning" v-if="isRunning" />
27+
<div id="counter" v-text="counter"></div>
28+
<button id="start" @click="start"></button>
29+
<button id="stop" @click="stop"></button>
30+
</div>
31+
`,
32+
setup() {
33+
const counter = ref(0)
34+
35+
const { isRunning, start, stop } = useRafFn(
36+
() => {
37+
counter.value = counter.value + 1
38+
},
39+
60,
40+
onMount
41+
)
42+
43+
return { isRunning, counter, start, stop }
44+
}
45+
})
46+
147
describe('useRafFn', () => {
2-
it('should do something', () => {
3-
// Add test here
48+
it('should not display #isRunning when onMount is false', async () => {
49+
const wrapper = mount(testComponent(false))
50+
await wrapper.vm.$nextTick()
51+
expect(rafSpy).not.toHaveBeenCalled()
52+
expect(wrapper.find('#isRunning').exists()).toBe(false)
53+
expect(wrapper.find('#counter').text()).toBe('0')
54+
})
55+
56+
it('should display #isRunning when onMount is true', async () => {
57+
const wrapper = mount(testComponent(true))
58+
await wrapper.vm.$nextTick()
59+
expect(rafSpy).toHaveBeenCalled()
60+
expect(wrapper.find('#isRunning').exists()).toBe(true)
61+
expect(wrapper.find('#counter').text()).toBe(`${totCalls}`)
462
})
563
})

0 commit comments

Comments
 (0)