Skip to content

Commit

Permalink
fix(useInterval): 修正测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 22, 2020
1 parent 59b4e38 commit 8d45de0
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/react/useInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { wait } from '../utils'
describe('useInterval', () => {
test('通过 stop/start 控制', async () => {
let i = 0
const { result } = renderHook(() => useInterval(() => i++, 20))
const { result } = renderHook(() =>
useInterval(() => {
const prevI = i
i = i + 1
return prevI
}, 20),
)
expect(result.current[0]).toBe(0)
await wait(20)
expect(result.current[0]).toBe(1)
Expand All @@ -17,13 +23,21 @@ describe('useInterval', () => {
expect(result.current[0]).toBe(2)
await wait(25)
expect(result.current[0]).toBe(3)
result.current[1].stop()
})

test('通过 delay 控制', async () => {
let i = 0
const { result } = renderHook(() => {
const [delay, setDelay] = useState<any>(20)
return { interval: useInterval(() => i++, delay), setDelay }
return {
interval: useInterval(() => {
const prevI = i
i = i + 1
return prevI
}, delay),
setDelay: setDelay,
}
})
expect(result.current.interval[0]).toBe(0)
await wait(25)
Expand All @@ -36,21 +50,30 @@ describe('useInterval', () => {
expect(result.current.interval[0]).toBe(2)
await wait(25)
expect(result.current.interval[0]).toBe(3)
result.current.interval[1].stop()
})

test('支持 duration', async () => {
let i = 0
renderHook(() => {
return useInterval(() => i++, 10, 21)
})
renderHook(() =>
useInterval(
() => {
i = i + 1
},
10,
28,
),
)
await wait(50)
expect(i).toBe(2)
expect(i).toBe(3)
})

test('支持 start 设置 delay, duration', async () => {
let i = 0
const { result } = renderHook(() => {
return useInterval(() => i++, null)[1]
return useInterval(() => {
i = i + 1
}, null)[1]
})
expect(i).toBe(0)
act(() => result.current.start(10, 3))
Expand Down

0 comments on commit 8d45de0

Please sign in to comment.