Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

2.1 KB
Raw Blame
import type { ChangeEvent, ReactElement } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import Notes from "../src/routes/Notes";

const harness = vi.hoisted(() => ({
  effects: [] as Array<() => void | (() => void)>,
  setText: vi.fn(),
  get: vi.fn(),
}));

vi.mock("react", async (load) => ({
  ...await load<typeof import("react")>(),
  useState: (initial: unknown) => [initial, initial === "" ? harness.setText : vi.fn()],
  useEffect: (effect: () => void | (() => void)) => harness.effects.push(effect),
  useRef: (current: unknown) => ({ current }),
}));

vi.mock("@tauri-apps/plugin-store", () => ({
  LazyStore: class {
    get = harness.get;
    onKeyChange = vi.fn().mockResolvedValue(() => {});
  },
}));
vi.mock("use-intl", () => ({ useTranslations: () => (key: string) => key }));

beforeEach(() => {
  vi.clearAllMocks();
  harness.effects.length = 0;
});

function openNote() {
  let finishRead!: (value: string) => void;
  harness.get.mockReturnValueOnce(new Promise<string>((resolve) => { finishRead = resolve; }));
  const view = Notes() as ReactElement<{ children: ReactElement[] }>;
  const textarea = view.props.children[1] as ReactElement<{
    onChange: (event: ChangeEvent<HTMLTextAreaElement>) => void;
  }>;
  const dispose = harness.effects[0]();
  return {
    type: (value: string) =>
      textarea.props.onChange({ target: { value } } as ChangeEvent<HTMLTextAreaElement>),
    load: async (value: string) => {
      finishRead(value);
      await Promise.resolve();
    },
    dispose,
  };
}

describe("recording notes loading", () => {
  it("keeps text entered while the saved note is still loading", async () => {
    const note = openNote();
    note.type("new draft");
    await note.load("previous note");
    expect(harness.setText.mock.calls).toEqual([["new draft"]]);
    note.dispose?.();
  });

  it("loads saved text when the user has not started typing", async () => {
    const note = openNote();
    await note.load("previous note");
    expect(harness.setText.mock.calls).toEqual([["previous note"]]);
    note.dispose?.();
  });
});