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(testing): useCountdown hook #702

Merged
merged 3 commits into from
Aug 2, 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
3 changes: 1 addition & 2 deletions react-app/src/components/TimeoutModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
DialogTitle,
} from "@/components";
import { Auth } from "aws-amplify";
import { useIdle } from "@/hooks";
import { useIdle, useCountdown } from "@/hooks";
import { useGetUser } from "@/api";
import { useCountdown } from "@/hooks/useCountdown";
import { intervalToDuration } from "date-fns";
import pluralize from "pluralize";

Expand Down
77 changes: 77 additions & 0 deletions react-app/src/hooks/useCountdown/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { useCountdown } from ".";

const COUNTDOWN_TIME = 10;

describe("useCountdown", () => {
beforeEach(() => {
vi.useFakeTimers();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this. But huge win on this faketimer stuff.

});

test("Returns the correct initial value", () => {
const { result } = renderHook(() => useCountdown(COUNTDOWN_TIME));

expect(result.current[0]).toBe(COUNTDOWN_TIME);
});

test("Returns 0 after counting down", () => {
const { result } = renderHook(() => useCountdown(COUNTDOWN_TIME));

act(result.current[1].startCountdown);

act(() => {
vi.advanceTimersByTime(COUNTDOWN_TIME * 1000);
});

expect(result.current[0]).toBe(0);
});

test("Correctly starts the countdown", () => {
const { result } = renderHook(() => useCountdown(COUNTDOWN_TIME));

act(result.current[1].startCountdown);

act(() => {
vi.advanceTimersByTime(5 * 1000);
});

expect(result.current[0]).toBe(COUNTDOWN_TIME - 5);
});

test("Correctly stops the countdown", () => {
const { result } = renderHook(() => useCountdown(COUNTDOWN_TIME));

act(result.current[1].startCountdown);

act(() => {
vi.advanceTimersByTime(5 * 1000);
});

act(result.current[1].stopCountdown);

act(() => {
vi.advanceTimersByTime(5 * 1000);
});

expect(result.current[0]).toBe(COUNTDOWN_TIME - 5);
});

test("Correctly resets the countdown", () => {
const { result } = renderHook(() => useCountdown(COUNTDOWN_TIME));

act(result.current[1].startCountdown);

act(() => {
vi.advanceTimersByTime(5 * 1000);
});

act(result.current[1].resetCountdown);

act(() => {
vi.advanceTimersByTime(5 * 1000);
});

expect(result.current[0]).toBe(COUNTDOWN_TIME);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type CountdownControllers = {
};

export const useCountdown = (
countStart: number,
minutesToCountDown: number,
): [number, CountdownControllers] => {
const [count, setCount] = useState<number>(countStart);
const [count, setCount] = useState<number>(minutesToCountDown);
const [isCountdownRunning, setIsCountdownRunning] = useState<boolean>(false);

const startCountdown = () => {
Expand All @@ -25,7 +25,7 @@ export const useCountdown = (
// Will set running false and reset the seconds to initial value
const resetCountdown = useCallback(() => {
stopCountdown();
setCount(countStart);
setCount(minutesToCountDown);
}, [stopCountdown]);

const countdownCallback = useCallback(() => {
Expand Down
Loading