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.

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

import { afterEach, describe, expect, it } from "vitest";
import { configOverridesAtom, userConfigAtom } from "@/core/config/config";
import { defaultUserConfig } from "@/core/config/config-schema";
import { store } from "@/core/state/jotai";
import type { Theme } from "../useTheme";
import { resolvedThemeAtom, visibleForTesting } from "../useTheme";

const { themeFromQueryParam } = visibleForTesting;

function setQuery(search: string): void {
  window.history.replaceState({}, "", search === "" ? "/" : `/?${search}`);
}

function setConfigTheme(theme: Theme): void {
  const config = defaultUserConfig();
  store.set(userConfigAtom, {
    ...config,
    display: { ...config.display, theme },
  });
}

afterEach(() => {
  setQuery("");
  store.set(userConfigAtom, defaultUserConfig());
  store.set(configOverridesAtom, {});
});

describe("themeFromQueryParam", () => {
  it.each(["light", "dark", "system"] as const)(
    "returns the valid theme %s",
    (theme) => {
      setQuery(`theme=${theme}`);
      expect(themeFromQueryParam()).toBe(theme);
    },
  );

  it("returns undefined when the param is absent", () => {
    setQuery("");
    expect(themeFromQueryParam()).toBeUndefined();
  });

  it("returns undefined for an invalid value", () => {
    setQuery("theme=blue");
    expect(themeFromQueryParam()).toBeUndefined();
  });
});

describe("resolvedThemeAtom with a theme query param", () => {
  it("uses the saved config theme when no param is present", () => {
    setConfigTheme("dark");
    setQuery("");
    expect(store.get(resolvedThemeAtom)).toBe("dark");
  });

  it("lets the query param override the saved config theme", () => {
    setConfigTheme("light");
    setQuery("theme=dark");
    expect(store.get(resolvedThemeAtom)).toBe("dark");
  });

  it("falls back to the config theme for an invalid param value", () => {
    setConfigTheme("dark");
    setQuery("theme=blue");
    expect(store.get(resolvedThemeAtom)).toBe("dark");
  });
});