Skip to content

Commit 44681a1

Browse files
committed
improvement: Add tests and documentation to useTimeoutFn
1 parent 222704e commit 44681a1

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

src/components/useTimeoutFn/stories/UseTimeoutFnDemo.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ export default Vue.extend({
4646
name: 'UseTimeoutFnDemo',
4747
setup() {
4848
const timerFnMsg = ref('Timer not completed')
49-
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(() => {
49+
const timerDuration = 3000
50+
const timerHandler = () => {
5051
timerFnMsg.value = 'Timer completed!'
51-
}, 3000)
52+
}
53+
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(
54+
timerHandler,
55+
timerDuration
56+
)
5257
5358
const isReadyStatus = computed(() => {
5459
if (isReady.value === false) return 'Pending...'

src/components/useTimeoutFn/stories/useTimeoutFn.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# useTimeoutFn
22

3-
Vue function that calls given callback function after specified amount of milliseconds.
3+
Vue function that calls given callback function after a specified amount of milliseconds.
44

55
## Reference
66

@@ -17,11 +17,14 @@ useTimeoutFn(
1717

1818
### Parameters
1919

20-
- `value: string` lorem ipsa
20+
- `callback: Function` the function to call when the timer finishes
21+
- `ms: number` how many milliseconds to wait before running the callback function
2122

2223
### Returns
2324

24-
- `value: string` lorem ipsa
25+
- `isReady: boolean | null` the timer status
26+
- `cancelTimer: Function` the function used for canceling the timer
27+
- `resetTimer: Function` the function used for resetting the timer
2528

2629
## Usage
2730

@@ -45,9 +48,15 @@ useTimeoutFn(
4548
name: 'UseTimeoutFnDemo',
4649
setup() {
4750
const timerFnMsg = ref('Timer not completed')
48-
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(() => {
51+
const timerDuration = 3000
52+
const timerHandler = () => {
4953
timerFnMsg.value = 'Timer completed!'
50-
}, 3000)
54+
}
55+
56+
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(
57+
timerHandler,
58+
timerDuration
59+
)
5160
5261
return { timerFnMsg, isReady, cancelTimer, resetTimer }
5362
}
Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,74 @@
1+
import { mount } from '../../helpers/test'
2+
import { ref } from '../../api'
3+
import { useTimeoutFn } from '../../vue-use-kit'
4+
5+
beforeEach(() => {
6+
jest.useFakeTimers()
7+
})
8+
9+
afterEach(() => {
10+
jest.clearAllTimers()
11+
})
12+
13+
const testComponent = () => ({
14+
template: `
15+
<div>
16+
<div id="isReady" v-if="isReady">
17+
<button id="cancelTimer" @click="cancelTimer"></button>
18+
<button id="resetTimer" @click="resetTimer"></button>
19+
<div id="isCallbackCalled" v-if="isCallbackCalled"></div>
20+
</div>
21+
</div>
22+
`,
23+
setup() {
24+
const isCallbackCalled = ref(false)
25+
const { isReady, cancelTimer, resetTimer } = useTimeoutFn(() => {
26+
isCallbackCalled.value = true
27+
}, 1000)
28+
return { isReady, cancelTimer, resetTimer, isCallbackCalled }
29+
}
30+
})
31+
132
describe('useTimeoutFn', () => {
2-
it('should do something', () => {
3-
// Add test here
33+
it('should call setTimeout when initialized', () => {
34+
expect(setTimeout).toHaveBeenCalledTimes(0)
35+
mount(testComponent())
36+
expect(setTimeout).toHaveBeenCalledTimes(1)
37+
})
38+
39+
it('should call setTimeout again when resetTimer is called', async () => {
40+
expect(setTimeout).toHaveBeenCalledTimes(0)
41+
const wrapper = mount(testComponent())
42+
expect(setTimeout).toHaveBeenCalledTimes(1)
43+
jest.runAllTimers()
44+
45+
// Vue is inserting #resetTimer in the DOM
46+
await wrapper.vm.$nextTick()
47+
wrapper.find('#resetTimer').trigger('click')
48+
expect(setTimeout).toHaveBeenCalledTimes(2)
49+
})
50+
51+
it('should hide all elements when cancelTimer is called', async () => {
52+
const wrapper = mount(testComponent())
53+
jest.runAllTimers()
54+
55+
// Vue is inserting #isReady in the DOM
56+
await wrapper.vm.$nextTick()
57+
expect(wrapper.find('#isReady').exists()).toBe(true)
58+
wrapper.find('#cancelTimer').trigger('click')
59+
jest.runAllTimers()
60+
61+
// Vue is removing #isReady from the DOM
62+
await wrapper.vm.$nextTick()
63+
expect(wrapper.find('#isReady').exists()).toBe(false)
64+
})
65+
66+
it('should display #isReady when the timers are called', async () => {
67+
const wrapper = mount(testComponent())
68+
jest.runAllTimers()
69+
70+
// Vue is inserting #isReady in the DOM
71+
await wrapper.vm.$nextTick()
72+
expect(wrapper.find('#isReady').exists()).toBe(true)
473
})
574
})

0 commit comments

Comments
 (0)