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 { getDefaultStore } from "jotai";
import { beforeEach, describe, expect, it, vi } from "vitest";

// Mock the storage module to use our mock storage
vi.mock("@/utils/storage/storage", () => ({
  availableStorage: {
    getItem: vi.fn(),
    setItem: vi.fn(),
    removeItem: vi.fn(),
  },
}));

import { cellId } from "@/__tests__/branded";
import { availableStorage } from "@/utils/storage/storage";
import { ChartSchema } from "../schemas";
import type { TabName } from "../storage";
import { createTabId, KEY, tabsStorageAtom } from "../storage";
import { ChartType } from "../types";

describe("Chart Transforms Storage", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    // Reset the store before each test
    const store = getDefaultStore();
    store.set(tabsStorageAtom, new Map());
  });

  describe("tabsStorageAtom", () => {
    it("should initialize with an empty Map", () => {
      const store = getDefaultStore();
      const value = store.get(tabsStorageAtom);
      expect(value).toBeInstanceOf(Map);
      expect(value.size).toBe(0);
    });

    it("should store and retrieve tab data", () => {
      const store = getDefaultStore();
      const cid = cellId("cell-1");
      const tabData = {
        id: createTabId(),
        tabName: "Tab 1" as TabName,
        chartType: ChartType.LINE,
        config: ChartSchema.parse({
          general: {
            xColumn: {},
            yColumn: {},
          },
        }),
      };

      // Set the atom value
      const newMap = new Map();
      newMap.set(cid, [tabData]);
      store.set(tabsStorageAtom, newMap);

      // Verify the value was set
      const retrievedValue = store.get(tabsStorageAtom);
      expect(retrievedValue.get(cid)).toEqual([tabData]);
    });

    it("should handle multiple tabs for the same cell", () => {
      const store = getDefaultStore();
      const cid = cellId("cell-1");
      const tabData1 = {
        id: createTabId(),
        tabName: "Tab 1" as TabName,
        chartType: ChartType.LINE,
        config: ChartSchema.parse({
          general: {
            xColumn: {},
            yColumn: {},
          },
        }),
      };
      const tabData2 = {
        id: createTabId(),
        tabName: "Tab 2" as TabName,
        chartType: ChartType.BAR,
        config: ChartSchema.parse({
          general: {
            xColumn: {},
            yColumn: {},
          },
        }),
      };

      // Set the atom value
      const newMap = new Map();
      newMap.set(cid, [tabData1, tabData2]);
      store.set(tabsStorageAtom, newMap);

      // Verify the value was set
      const retrievedValue = store.get(tabsStorageAtom);
      expect(retrievedValue.get(cid)).toEqual([tabData1, tabData2]);
    });
  });

  describe("LocalStorage integration", () => {
    it("should call localStorage.setItem when setting data", () => {
      const store = getDefaultStore();
      const newMap = new Map();
      store.set(tabsStorageAtom, newMap);

      expect(availableStorage.setItem).toHaveBeenCalledWith(
        KEY,
        JSON.stringify([]),
      );
    });
  });
});