@cc-heart/utils
Version:
🔧 javascript common tools collection
25 lines (21 loc) • 751 B
text/typescript
import { useOnce } from '../../../hooks'
describe('useOnce module', () => {
it('should return a function', () => {
const fn = useOnce(() => null)
expect(fn instanceof Function).toBeTruthy()
})
it('throw error when params is not a function', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const fn = () => useOnce('')
expect(fn).toThrowError('first params must be a function')
})
it('It should only be executed once when it is called multiple times', () => {
const mockFn = jest.fn((num1: number, num2: number) => num1 + num2)
const fn = useOnce(() => mockFn(1, 2))
for (let i = 0; i < 3; i++) {
fn()
}
expect(mockFn.mock.calls.length).toBe(1)
})
})