Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: useTimeout hook #2

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./useCounter/useCounter";
export * from "./useHover/useHover";
export * from "./useFavicon/useFavicon";
export * from "./useQueue/useQueue";
export * from "./useTimeout";
1 change: 1 addition & 0 deletions src/hooks/useTimeout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useTimeout";
97 changes: 97 additions & 0 deletions src/hooks/useTimeout/useTimeout.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, it, beforeEach, vi, afterEach } from "vitest";
import useTimeout from "./useTimeout";
import { act } from "react-dom/test-utils";
import { render } from "@testing-library/react";

// Test Component using the useTimeout hook
function TestComponent({
onTimeout,
delay,
}: {
onTimeout: () => void;
delay: number;
}) {
useTimeout(onTimeout, delay);
return null;
}

describe("useTimeout", () => {
beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it("should call the callback after the specified timeout", () => {
// mock
const callback = vi.fn();
const delay = 1000;

// act
render(<TestComponent onTimeout={callback} delay={delay} />);

act(() => {
vi.advanceTimersByTime(delay);
});

// assert
expect(callback).toHaveBeenCalledTimes(1);
});

it("should clear timeout on unmount", () => {
// mock
const callback = vi.fn();
const delay = 1000;

// act
const { unmount } = render(
<TestComponent onTimeout={callback} delay={delay} />
);

act(() => {
vi.advanceTimersByTime(delay / 2);
});

unmount();

act(() => {
vi.advanceTimersByTime(delay / 2);
});

// assert
expect(callback).not.toHaveBeenCalled();
});

it("should clear the timeout when delay changes", () => {
// mock
const callback = vi.fn();
const initialDelay = 1000;
const newDelay = 2000;

// act
const { rerender } = render(
<TestComponent onTimeout={callback} delay={initialDelay} />
);

act(() => {
vi.advanceTimersByTime(initialDelay / 2);
});

rerender(<TestComponent onTimeout={callback} delay={newDelay} />);

act(() => {
vi.advanceTimersByTime(newDelay / 2);
});

// assert
expect(callback).not.toHaveBeenCalled();

act(() => {
vi.advanceTimersByTime(newDelay / 2);
});

expect(callback).toHaveBeenCalledTimes(1);
});
});
14 changes: 14 additions & 0 deletions src/hooks/useTimeout/useTimeout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect, useRef, useCallback } from "react";

export default function useTimeout(cb: () => void, ms: number) {
const timeId = useRef<NodeJS.Timeout | null>(null);
const clearCurrentTimeOut = useCallback(() => {
if (timeId.current) clearTimeout(timeId.current);
}, []);

useEffect(() => {
timeId.current = setTimeout(cb, ms);
return clearCurrentTimeOut;
}, [ms, cb]);
return clearCurrentTimeOut;
}
Loading