Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

test: add tests for encapsulated React hooks (#3137)

devin-ai-integration[bot]3b6034f974d5
diff --git a/frontend/src/hooks/__tests__/useBoolean.test.tsx b/frontend/src/hooks/__tests__/useBoolean.test.tsx
new file mode 100644
index 0000000000..9723bf97b4
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useBoolean.test.tsx
@@ -0,0 +1,31 @@
+import { expect, describe, it, vi } from "vitest";
+import { renderHook, act } from "@testing-library/react";
+import { useBoolean } from "../useBoolean";
+
+describe("useBoolean", () => {
+ it("should initialize with given value", () => {
+ const { result } = renderHook(() => useBoolean(true));
+ expect(result.current[0]).toBe(true);
+ });
+
+ it("should toggle value", () => {
+ const { result } = renderHook(() => useBoolean(false));
+ act(() => result.current[1].toggle());
+ expect(result.current[0]).toBe(true);
+ });
+
+ it("should set true/false", () => {
+ const { result } = renderHook(() => useBoolean(false));
+ act(() => result.current[1].setTrue());
+ expect(result.current[0]).toBe(true);
+ act(() => result.current[1].setFalse());
+ expect(result.current[0]).toBe(false);
+ });
+
+ it("should handle event.stopPropagation", () => {
+ const { result } = renderHook(() => useBoolean(false));
+ const mockEvent = { stopPropagation: vi.fn() };
+ act(() => result.current[1].setTrue(mockEvent));
+ expect(mockEvent.stopPropagation).toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/hooks/__tests__/useDebounce.test.tsx b/frontend/src/hooks/__tests__/useDebounce.test.tsx
new file mode 100644
index 0000000000..5b368d7207
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useDebounce.test.tsx
@@ -0,0 +1,118 @@
+import { expect, describe, it, vi, beforeEach, afterEach } from "vitest";
+import { renderHook, act } from "@testing-library/react";
+import { useDebounce, useDebounceControlledState } from "../useDebounce";
+
+describe("useDebounce", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.useRealTimers();
+ });
+
+ it("should update value after delay", () => {
+ const { result, rerender } = renderHook(
+ ({ value }) => useDebounce(value, 1000),
+ { initialProps: { value: "initial" } },
+ );
+
+ expect(result.current).toBe("initial");
+
+ rerender({ value: "updated" });
+
+ act(() => {
+ vi.runAllTimers();
+ });
+
+ act(() => {
+ vi.runAllTimers();
+ });
+
+ expect(result.current).toBe("updated");
+ });
+
+ it("should cleanup on unmount", () => {
+ const { result, rerender, unmount } = renderHook(
+ ({ value }) => useDebounce(value, 1000),
+ { initialProps: { value: "initial" } },
+ );
+
+ expect(result.current).toBe("initial");
+
+ rerender({ value: "updated" });
+ unmount();
+
+ act(() => {
+ vi.runAllTimers();
+ });
+
+ expect(result.current).toBe("initial");
+ });
+});
+
+describe("useDebounceControlledState", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.useRealTimers();
+ });
+
+ it("should call onChange with initial value immediately", () => {
+ const onChange = vi.fn();
+ renderHook(() =>
+ useDebounceControlledState({
+ initialValue: "initial",
+ onChange,
+ delay: 1000,
+ }),
+ );
+
+ expect(onChange).toHaveBeenCalledWith("initial");
+ });
+
+ it("should debounce subsequent onChange calls", () => {
+ const onChange = vi.fn();
+ const { result } = renderHook(() =>
+ useDebounceControlledState({
+ initialValue: "initial",
+ onChange,
+ delay: 1000,
+ }),
+ );
+
+ onChange.mockClear();
+
+ act(() => {
+ result.current.onChange("updated");
+ });
+
+ act(() => {
+ vi.runAllTimers();
+ });
+
+ act(() => {
+ vi.runAllTimers();
+ });
+
+ expect(onChange).toHaveBeenCalledWith("updated");
+ });
+
+ it("should not call onChange if disabled", () => {
+ const onChange = vi.fn();
+ renderHook(() =>
+ useDebounceControlledState({
+ initialValue: "initial",
+ onChange,
+ delay: 1000,
+ disabled: true,
+ }),
+ );
+
+ expect(onChange).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/hooks/__tests__/useInterval.test.tsx b/frontend/src/hooks/__tests__/useInterval.test.tsx
new file mode 100644
index 0000000000..fd439cac83
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useInterval.test.tsx
@@ -0,0 +1,68 @@
+import { expect, describe, it, vi, beforeEach, afterEach } from "vitest";
+import { renderHook } from "@testing-library/react";
+import { useInterval } from "../useInterval";
+
+describe("useInterval", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("should call callback after delay", () => {
+ const callback = vi.fn();
+ renderHook(() =>
+ useInterval(callback, { delayMs: 1000, whenVisible: false }),
+ );
+
+ expect(callback).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1000);
+ expect(callback).toHaveBeenCalledTimes(1);
+ });
+
+ it("should not call callback when disabled", () => {
+ const callback = vi.fn();
+ renderHook(() =>
+ useInterval(callback, {
+ delayMs: 1000,
+ whenVisible: false,
+ disabled: true,
+ }),
+ );
+
+ vi.advanceTimersByTime(1000);
+ expect(callback).not.toHaveBeenCalled();
+ });
+
+ it("should respect visibility when whenVisible is true", () => {
+ const callback = vi.fn();
+ Object.defineProperty(document, "visibilityState", {
+ value: "hidden",
+ writable: true,
+ });
+
+ renderHook(() =>
+ useInterval(callback, { delayMs: 1000, whenVisible: true }),
+ );
+
+ vi.advanceTimersByTime(1000);
+ expect(callback).not.toHaveBeenCalled();
+
+ Object.defineProperty(document, "visibilityState", { value: "visible" });
+ vi.advanceTimersByTime(1000);
+ expect(callback).toHaveBeenCalled();
+ });
+
+ it("should cleanup on unmount", () => {
+ const callback = vi.fn();
+ const { unmount } = renderHook(() =>
+ useInterval(callback, { delayMs: 1000, whenVisible: false }),
+ );
+
+ unmount();
+ vi.advanceTimersByTime(1000);
+ expect(callback).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/hooks/__tests__/useTimer.test.tsx b/frontend/src/hooks/__tests__/useTimer.test.tsx
new file mode 100644
index 0000000000..14b0883dce
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useTimer.test.tsx
@@ -0,0 +1,60 @@
+import { expect, describe, it, vi, beforeEach, afterEach } from "vitest";
+import { renderHook, act } from "@testing-library/react";
+import { useTimer } from "../useTimer";
+
+describe("useTimer", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.useRealTimers();
+ });
+
+ it("should start at 0", () => {
+ const { result } = renderHook(() => useTimer());
+ expect(result.current.time).toBe("0.0");
+ });
+
+ it("should increment time when started", () => {
+ const { result } = renderHook(() => useTimer());
+
+ act(() => {
+ result.current.start();
+ vi.runOnlyPendingTimers();
+ });
+
+ expect(result.current.time).toBe("0.1");
+ });
+
+ it("should stop incrementing when stopped", () => {
+ const { result } = renderHook(() => useTimer());
+
+ act(() => {
+ result.current.start();
+ vi.runOnlyPendingTimers();
+ });
+
+ act(() => {
+ result.current.stop();
+ vi.runOnlyPendingTimers();
+ });
+
+ expect(result.current.time).toBe("0.1");
+ });
+
+ it("should reset to 0 when cleared", () => {
+ const { result } = renderHook(() => useTimer());
+ act(() => result.current.clear());
+ expect(result.current.time).toBe("0.0");
+ });
+
+ it("should cleanup interval on unmount", () => {
+ const { result, unmount } = renderHook(() => useTimer());
+ act(() => result.current.start());
+ unmount();
+ vi.advanceTimersByTime(100);
+ expect(result.current.time).toBe("0.0");
+ });
+});