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): Test useIdle hook #701

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
2 changes: 1 addition & 1 deletion react-app/src/components/TimeoutModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DialogTitle,
} from "@/components";
import { Auth } from "aws-amplify";
import { useIdle } from "@/hooks/useIdle";
import { useIdle } from "@/hooks";
import { useGetUser } from "@/api";
import { useCountdown } from "@/hooks/useCountdown";
import { intervalToDuration } from "date-fns";
Expand Down
2 changes: 2 additions & 0 deletions react-app/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from "./useDebounce";
export * from "./useLabelMappings";
export * from "./useReadOnlyUser";
export * from "./useScrollToTop";
export * from "./useCountdown";
export * from "./useIdle";
87 changes: 87 additions & 0 deletions react-app/src/hooks/useIdle/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { useIdle } from ".";

const IDLE_TIME = 100;

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

test("Returns correct initial value", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

expect(result.current).toBe(true);
});

test("Returns correct value on mouse event", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

act(() => {
document.dispatchEvent(new MouseEvent("mousemove"));
});

expect(result.current).toBe(false);
});

test("Returns correct value on click event", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

act(() => {
document.dispatchEvent(new MouseEvent("click"));
});

expect(result.current).toBe(false);
});

test("Returns correct value on scroll event", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

act(() => {
document.dispatchEvent(new MouseEvent("scroll"));
});

expect(result.current).toBe(false);
});

test("Returns correct value on touch event", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

act(() => {
document.dispatchEvent(new MouseEvent("touchmove"));
});

expect(result.current).toBe(false);
});

test("Returns correct value on keypress event", () => {
const { result } = renderHook(() => useIdle(IDLE_TIME));

act(() => {
document.dispatchEvent(new MouseEvent("keypress"));
});

expect(result.current).toBe(false);
});

test("Returns correct initial value from initialState argument", () => {
const { result } = renderHook(() =>
useIdle(IDLE_TIME, { initialState: false }),
);

expect(result.current).toBe(false);
});

test("Returns correct value when timeout has elapsed", () => {
const { result } = renderHook(() =>
useIdle(IDLE_TIME, { initialState: false }),
);

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

expect(result.current).toBe(true);
});
});
File renamed without changes.
Loading