Skip to content

Commit

Permalink
fix(useCountdown): add 2 test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Feb 12, 2024
1 parent 034bd9a commit 57dbd0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions packages/core/useCountdown/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import { useCountdown } from '.'
import { wait } from '../utils'

Expand Down Expand Up @@ -46,6 +46,18 @@ describe('useEllipsis', () => {
expect(countdown.value.total).toEqual(3000)
})

test('cannot start duplicate', async () => {
const spy = vi.spyOn(console, 'warn')
const { start } = useCountdown({
duration: 3000,
interval: 100,
})
start()
await wait(100)
start()
expect(spy).toHaveBeenCalledWith('Countdown has already running.')
})

test('reset with immediate', async () => {
const { countdown, reset } = useCountdown({
duration: 3000,
Expand Down Expand Up @@ -73,5 +85,18 @@ describe('useEllipsis', () => {
expect(isRunning.value).toBeFalsy()
})

// isRunning: Ref<boolean> // 是否在运行
test('isRunning', async () => {
const { countdown, isRunning } = useCountdown({
duration: 200,
interval: 100,
immediate: true,
})
await wait(100)
expect(isRunning.value).toBeTruthy()
expect(countdown.value.total).toEqual(100)

await wait(100)
expect(isRunning.value).toBeFalsy()
expect(countdown.value.total).toEqual(0)
})
})
3 changes: 2 additions & 1 deletion packages/core/useCountdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useCountdown = (

const start = () => {
if (isRunning.value) {
console.warn('Countdown has already running')
console.warn('Countdown has already running.')
return
}
isRunning.value = true
Expand Down Expand Up @@ -84,6 +84,7 @@ export const useCountdown = (

watch(remainingTime, (value) => {
if (value === 0) {
isRunning.value = false
if (timer) clearInterval(timer)
}
})
Expand Down

0 comments on commit 57dbd0c

Please sign in to comment.