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.

1.5 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { act, renderHook } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
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");
  });
});