Files
openscreen/src/components/launch/SourceSelector.test.tsx
T
makaradam 0f554e72ae test: import jest-dom matchers in SourceSelector test
SourceSelector.test.tsx uses toBeInTheDocument but never imported
@testing-library/jest-dom, and vitest.config.ts has no setupFiles to
register the matchers globally. It only passed when test ordering
happened to leak the matcher from another file, making CI intermittently
red on main (e.g. the #652 merge). Importing jest-dom directly makes the
test self-sufficient and deterministic.
2026-05-31 09:48:21 +02:00

79 lines
2.4 KiB
TypeScript

import "@testing-library/jest-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SourceSelector } from "./SourceSelector";
vi.mock("@/contexts/I18nContext", () => ({
useScopedT: (namespace: string) => {
if (namespace === "common") {
return (key: string) => {
if (key === "actions.cancel") return "Cancel";
if (key === "actions.share") return "Share";
if (key === "actions.reload") return "Reload";
return key;
};
}
return (key: string, vars?: Record<string, string>) => {
if (key === "sourceSelector.loading") return "Loading sources...";
if (key === "sourceSelector.emptyTitle") return "No screens or windows found";
if (key === "sourceSelector.emptyDescription") {
return "If you just granted screen recording permission, reload this picker. On macOS you may need to reopen OpenScreen.";
}
if (key === "sourceSelector.loadFailedDescription") {
return "OpenScreen could not load capture sources. Reload this picker and try again.";
}
if (key === "sourceSelector.screens") return `Screens (${vars?.count ?? "0"})`;
if (key === "sourceSelector.windows") return `Windows (${vars?.count ?? "0"})`;
return key;
};
},
}));
describe("SourceSelector", () => {
beforeEach(() => {
window.electronAPI = {
...window.electronAPI,
getSources: vi.fn().mockResolvedValue([]),
selectSource: vi.fn(),
} as typeof window.electronAPI;
});
it("shows a retry state when no capture sources are available", async () => {
render(<SourceSelector />);
await screen.findByText("No screens or windows found");
expect(screen.getByRole("button", { name: "Reload" })).toBeInTheDocument();
});
it("reloads capture sources from the empty state", async () => {
const getSources = vi
.fn()
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{
id: "screen:1:0",
name: "Display 1",
thumbnail: "data:image/png;base64,abc",
display_id: "1",
appIcon: null,
},
]);
window.electronAPI = {
...window.electronAPI,
getSources,
selectSource: vi.fn(),
} as typeof window.electronAPI;
render(<SourceSelector />);
await screen.findByText("No screens or windows found");
fireEvent.click(screen.getByRole("button", { name: "Reload" }));
await waitFor(() => {
expect(screen.getByText("Display 1")).toBeInTheDocument();
});
expect(getSources).toHaveBeenCalledTimes(2);
});
});