Skip to content

Commit

Permalink
fix[test]: add setintervalByTimeout unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-hearts committed Oct 13, 2024
1 parent ac53268 commit a65dd65
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions __test__/lib/workers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,31 @@ describe('setintervalByTimeout', () => {

jest.useRealTimers()
})

test('setintervalByTimeout should cancel next invoke when clearMyInterval is called', async () => {

jest.useFakeTimers()

const mockFn = jest.fn()
const fn = () => {
return new Promise<void>(resolve => {
setTimeout(() => {
mockFn()
resolve()
}, 1000)
})
}


const clearMyInterval = setintervalByTimeout(fn, 500)

setTimeout(() => {
clearMyInterval()
}, 600)
await jest.advanceTimersByTimeAsync(2000)
expect(mockFn).toHaveBeenCalledTimes(1)
await jest.advanceTimersByTimeAsync(2000)
expect(mockFn).toHaveBeenCalledTimes(1)
jest.useRealTimers()
})
})

0 comments on commit a65dd65

Please sign in to comment.