-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
372 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { vi as jest } from 'vitest' | ||
import { useDisposeWithUtils, useEventListener, useTimeout } from './dispose-utils' | ||
|
||
describe('useTimeout', () => { | ||
it('should call the provided function after the specified timeout', () => { | ||
jest.useFakeTimers() | ||
|
||
const mockFn = jest.fn() | ||
const timeout = 1000 | ||
|
||
useTimeout(mockFn, timeout) | ||
|
||
expect(mockFn).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(timeout) | ||
|
||
expect(mockFn).toBeCalled() | ||
}) | ||
|
||
it('should cancel the timeout when the disposer function is called', () => { | ||
jest.useFakeTimers() | ||
|
||
const mockFn = jest.fn() | ||
const timeout = 1000 | ||
|
||
const disposer = useTimeout(mockFn, timeout) | ||
|
||
expect(mockFn).not.toBeCalled() | ||
|
||
disposer() | ||
|
||
jest.advanceTimersByTime(timeout) | ||
|
||
expect(mockFn).not.toBeCalled() | ||
}) | ||
|
||
it('should add event listener using "on" method if available', () => { | ||
const emitter = { | ||
on: jest.fn(), | ||
off: jest.fn(), | ||
} | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const disposer = useEventListener(emitter, eventName, fn, ...args) | ||
|
||
expect(emitter.on).toBeCalledWith(eventName, fn, ...args) | ||
expect(emitter.off).not.toBeCalled() | ||
|
||
disposer() | ||
|
||
expect(emitter.off).toBeCalledWith(eventName, fn, ...args) | ||
}) | ||
|
||
it('should add event listener using "addEventListener" method if "on" method is not available', () => { | ||
const emitter = { | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
} | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const disposer = useEventListener(emitter, eventName, fn, ...args) | ||
|
||
expect(emitter.addEventListener).toBeCalledWith(eventName, fn, ...args) | ||
expect(emitter.removeEventListener).not.toBeCalled() | ||
|
||
disposer() | ||
|
||
expect(emitter.removeEventListener).toBeCalledWith(eventName, fn, ...args) | ||
}) | ||
|
||
it('should return an empty disposer function if emitter is null', () => { | ||
const emitter = null | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const disposer = useEventListener(emitter, eventName, fn, ...args) | ||
|
||
expect(disposer).toEqual(expect.any(Function)) | ||
|
||
disposer() // Should not throw any error | ||
}) | ||
|
||
it('should add timeout disposer to the dispose object', () => { | ||
jest.useFakeTimers() | ||
|
||
const mockFn = jest.fn() | ||
const timeout = 1000 | ||
|
||
const dispose = useDisposeWithUtils() | ||
dispose.timeout(mockFn, timeout) | ||
|
||
expect(mockFn).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(timeout) | ||
|
||
expect(mockFn).toBeCalled() | ||
}) | ||
|
||
it('should add interval disposer to the dispose object', () => { | ||
jest.useFakeTimers() | ||
|
||
const mockFn = jest.fn() | ||
const interval = 1000 | ||
|
||
const dispose = useDisposeWithUtils() | ||
dispose.interval(mockFn, interval) | ||
|
||
expect(mockFn).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(interval) | ||
|
||
expect(mockFn).toBeCalled() | ||
}) | ||
|
||
// it('should add interval pause disposer to the dispose object', () => { | ||
// jest.useFakeTimers() | ||
|
||
// const mockFn = jest.fn() | ||
// const interval = 1000 | ||
|
||
// const dispose = useDisposeWithUtils() | ||
// dispose.intervalPause(mockFn, interval) | ||
|
||
// expect(mockFn).not.toBeCalled() | ||
|
||
// jest.advanceTimersByTime(interval) | ||
|
||
// expect(mockFn).not.toBeCalled() | ||
// }) | ||
|
||
it('should add event listener disposer to the dispose object using "on" method', async () => { | ||
const emitter = { | ||
on: jest.fn(), | ||
off: jest.fn(), | ||
} | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const dispose = useDisposeWithUtils() | ||
dispose.on(emitter, eventName, fn, ...args) | ||
|
||
expect(emitter.on).toBeCalledWith(eventName, fn, ...args) | ||
expect(emitter.off).not.toBeCalled() | ||
|
||
await dispose.dispose() | ||
|
||
expect(emitter.off).toBeCalledWith(eventName, fn, ...args) | ||
}) | ||
|
||
it('should add event listener disposer to the dispose object using "addEventListener" method', async () => { | ||
const emitter = { | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
} | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const dispose = useDisposeWithUtils() | ||
dispose.on(emitter, eventName, fn, ...args) | ||
|
||
expect(emitter.addEventListener).toBeCalledWith(eventName, fn, ...args) | ||
expect(emitter.removeEventListener).not.toBeCalled() | ||
|
||
await dispose.dispose() | ||
|
||
expect(emitter.removeEventListener).toBeCalledWith(eventName, fn, ...args) | ||
}) | ||
|
||
it('should return an empty disposer function if emitter is null 2', () => { | ||
const emitter = null | ||
const eventName = 'click' | ||
const fn = jest.fn() | ||
const args = [1, 2, 3] | ||
|
||
const dispose = useDisposeWithUtils() | ||
const disposer = dispose.on(emitter, eventName, fn, ...args) | ||
|
||
expect(disposer).toEqual(expect.any(Function)) | ||
|
||
disposer!() // Should not throw any error | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.