Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

2.1 KB
Raw Blame
import { Children, isValidElement, type ComponentProps, type MouseEvent, type ReactNode } from "react";
import { describe, expect, it, vi } from "vitest";
import { Btn, Chip, SwitchItem } from "../src/client/components/living/primitives";

vi.mock("react", async importOriginal => ({
  ...await importOriginal<typeof import("react")>(),
  useId: () => "control",
}));

vi.mock("../src/client/ui/copy", () => ({
  useCopy: () => ({ vis: false, t: (key: string) => key }),
}));

function button(node: ReactNode): ComponentProps<"button"> | undefined {
  if (!isValidElement<{ children?: ReactNode }>(node)) return;
  if (node.type === "button") return node.props;
  return Children.toArray(node.props.children).map(button).find(Boolean);
}

const controls = {
  button: (activate: () => void) => Btn({ icon: "play", title: "playback.play", hint: "hint-quality", onClick: activate }),
  chip: (activate: () => void) => Chip({ selected: false, title: "720p", hint: "hint-quality", onClick: activate, children: "720p" }),
  switch: (activate: () => void) => SwitchItem({ checked: false, label: "Setting", hint: "hint-quality", onChange: activate }),
};

describe("Hinted control activation", () => {
  it.each(Object.entries(controls))("%s applies the chosen action before focus-leave commits a draft", (_name, render) => {
    const changes: string[] = [];
    let pendingDraft = true;
    const control = button(render(() => { pendingDraft = false; changes.push("chosen action"); }))!;
    const blur = vi.fn(() => { if (pendingDraft) changes.push("unconfirmed draft"); });

    control.onClick!({ detail: 1, currentTarget: { blur } } as unknown as MouseEvent<HTMLButtonElement>);

    expect(changes).toEqual(["chosen action"]);
    expect(blur).toHaveBeenCalledOnce();
  });

  it.each(Object.entries(controls))("%s keeps keyboard focus when activated", (_name, render) => {
    const activate = vi.fn();
    const blur = vi.fn();
    button(render(activate))!.onClick!({ detail: 0, currentTarget: { blur } } as unknown as MouseEvent<HTMLButtonElement>);
    expect(activate).toHaveBeenCalledOnce();
    expect(blur).not.toHaveBeenCalled();
  });
});