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.

3.1 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import { act, renderHook } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { useInternalStateWithSync } from "../useInternalStateWithSync";

describe("useInternalStateWithSync", () => {
  it("should initialize with the given initial value", () => {
    const { result } = renderHook(() => useInternalStateWithSync("initial"));
    expect(result.current[0]).toBe("initial");
  });

  it("should update internal state when setState is called", () => {
    const { result } = renderHook(() => useInternalStateWithSync("initial"));

    act(() => {
      result.current[1]("updated");
    });

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

  it("should update internal state when initial value changes", () => {
    const { result, rerender } = renderHook(
      ({ value }) => useInternalStateWithSync(value),
      { initialProps: { value: "initial" } },
    );

    expect(result.current[0]).toBe("initial");

    rerender({ value: "new value" });

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

  it("preserves function values without calling them and resets when they change", () => {
    const initial = vi.fn(() => "initial");
    const draft = vi.fn(() => "draft");
    const changed = vi.fn(() => "changed");
    const { result, rerender } = renderHook(
      ({ value }) => useInternalStateWithSync(value),
      { initialProps: { value: initial } },
    );

    expect(result.current[0]).toBe(initial);
    act(() => result.current[1](() => draft));
    rerender({ value: initial });
    expect(result.current[0]).toBe(draft);
    rerender({ value: changed });
    expect(result.current[0]).toBe(changed);
    expect(initial).not.toHaveBeenCalled();
    expect(draft).not.toHaveBeenCalled();
    expect(changed).not.toHaveBeenCalled();
  });

  it("should not update internal state if initial value remains the same", () => {
    const { result, rerender } = renderHook(
      ({ value }) => useInternalStateWithSync(value),
      { initialProps: { value: "initial" } },
    );

    act(() => {
      result.current[1]("internal update");
    });

    expect(result.current[0]).toBe("internal update");

    rerender({ value: "initial" });

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

  it("should use custom equality function when provided", () => {
    const customEqualityFn = vi.fn(
      (a, b) => JSON.stringify(a) === JSON.stringify(b),
    );
    const initialValue = { key: "value" };

    const { result, rerender } = renderHook(
      ({ value }) => useInternalStateWithSync(value, customEqualityFn),
      { initialProps: { value: initialValue } },
    );

    expect(result.current[0]).toEqual(initialValue);

    // Rerender with a new object that has the same content
    rerender({ value: { key: "value" } });

    expect(customEqualityFn).toHaveBeenCalled();
    expect(result.current[0]).toEqual(initialValue);

    // Rerender with a new object that has different content
    rerender({ value: { key: "new value" } });

    expect(result.current[0]).toEqual({ key: "new value" });
  });
});