aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-timeout/use-timeout.test.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-02 15:33:29 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:15:27 +0100
commite2daf7f81789c54b23ade72bd164492e7304d375 (patch)
tree4d515b6b113dcc5bba6ff08ca5c6e7ddc8094e02 /src/utils/hooks/use-timeout/use-timeout.test.ts
parent1e4b48aa075e6131a7244cd4726ddb5ba75fcecf (diff)
feat(hooks): add an useTimeout hook
Diffstat (limited to 'src/utils/hooks/use-timeout/use-timeout.test.ts')
-rw-r--r--src/utils/hooks/use-timeout/use-timeout.test.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/utils/hooks/use-timeout/use-timeout.test.ts b/src/utils/hooks/use-timeout/use-timeout.test.ts
new file mode 100644
index 0000000..296b320
--- /dev/null
+++ b/src/utils/hooks/use-timeout/use-timeout.test.ts
@@ -0,0 +1,50 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ jest,
+} from '@jest/globals';
+import { renderHook } from '@testing-library/react';
+import { useTimeout } from './use-timeout';
+
+describe('useTimeout', () => {
+ beforeEach(() => {
+ jest.useFakeTimers();
+ });
+
+ afterEach(() => {
+ jest.runOnlyPendingTimers();
+ jest.useRealTimers();
+ });
+
+ it('executes the given callback with default delay', () => {
+ // When less than 1ms, setTimeout use 1. Default delay is 0ms.
+ const defaultTimeoutDelay = 1;
+ const callback = jest.fn();
+ renderHook(() => useTimeout(callback));
+
+ expect(callback).not.toHaveBeenCalled();
+
+ jest.advanceTimersByTime(defaultTimeoutDelay);
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+
+ it('executes the given callback with custom delay', () => {
+ const customDelay = 1500;
+ const callback = jest.fn();
+ renderHook(() => useTimeout(callback, customDelay));
+
+ expect(callback).not.toHaveBeenCalled();
+
+ jest.advanceTimersByTime(1);
+
+ expect(callback).not.toHaveBeenCalled();
+
+ jest.advanceTimersByTime(customDelay);
+
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+});