Files
community-rule/tests/pages/how-it-works-share.test.tsx
T

54 lines
1.7 KiB
TypeScript

import "@testing-library/jest-dom/vitest";
import { describe, expect, it, vi, beforeEach } from "vitest";
import { fireEvent, waitFor } from "@testing-library/react";
import {
renderWithProviders as render,
screen,
} from "../utils/test-utils";
import HowItWorksShareRow from "../../app/(marketing)/how-it-works/_components/HowItWorksShareRow";
const writeText = vi.fn().mockResolvedValue(undefined);
describe("HowItWorksShareRow", () => {
beforeEach(() => {
writeText.mockReset();
writeText.mockResolvedValue(undefined);
Object.defineProperty(window.navigator, "clipboard", {
configurable: true,
value: { writeText },
});
});
it("copies wiki markdown and prints the page", async () => {
const print = vi.spyOn(window, "print").mockImplementation(() => {});
render(
<HowItWorksShareRow
sendLabel="Send this to your community"
copyLabel="Copy into your wiki"
printLabel="Print the card"
copiedTitle="Copied to clipboard"
copyFailedTitle="Could not copy"
mailtoHref="mailto:?subject=Guide"
wikiMarkdown="[Guide](https://example.com)"
/>,
);
expect(
screen.getByRole("link", { name: "Send this to your community" }),
).toHaveAttribute("href", "mailto:?subject=Guide");
fireEvent.click(
screen.getByRole("button", { name: "Copy into your wiki" }),
);
await waitFor(() => {
expect(writeText).toHaveBeenCalledWith("[Guide](https://example.com)");
});
expect(screen.getByText("Copied to clipboard")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Print the card" }));
expect(print).toHaveBeenCalled();
print.mockRestore();
});
});