Share asks the person who holds the pen to confirm their community agreed before a link turns on, and deleting an account lists each owned Rule with PDF and RULE.md export. Case-study Share copies the page link instead of opening the privacy modal, and the sign-in banner stays below the header so Share remains clickable.
150 lines
4.9 KiB
TypeScript
150 lines
4.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { fireEvent } from "@testing-library/react";
|
|
import {
|
|
renderWithProviders as render,
|
|
screen,
|
|
waitFor,
|
|
} from "../../utils/test-utils";
|
|
import "@testing-library/jest-dom/vitest";
|
|
import Share from "../../../app/components/modals/Share";
|
|
import type { RuleShareSettings } from "../../../lib/ruleShareTypes";
|
|
|
|
const previewSettings: RuleShareSettings = {
|
|
visibility: "private",
|
|
shareSlug: null,
|
|
shareUrl: "https://communityrule.info/rules/rule-1",
|
|
shareUrlDisplay: null,
|
|
title: "Juniper Street Food Share",
|
|
ownerEmail: "name@example.org",
|
|
ownerInitials: "N",
|
|
readers: [
|
|
{ email: "rosa@example.org", initials: "R", role: "reader" },
|
|
{ email: "a@example.org", initials: "A", role: "reader" },
|
|
{ email: "b@example.org", initials: "B", role: "reader" },
|
|
{ email: "c@example.org", initials: "C", role: "reader" },
|
|
{ email: "d@example.org", initials: "D", role: "reader" },
|
|
{ email: "e@example.org", initials: "E", role: "reader" },
|
|
],
|
|
copyEnabled: true,
|
|
};
|
|
|
|
function openShare(overrides: Partial<RuleShareSettings> = {}) {
|
|
return render(
|
|
<Share
|
|
isOpen
|
|
onClose={vi.fn()}
|
|
previewSettings={{ ...previewSettings, ...overrides }}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
const writeText = vi.fn().mockResolvedValue(undefined);
|
|
|
|
beforeEach(() => {
|
|
writeText.mockReset();
|
|
writeText.mockResolvedValue(undefined);
|
|
Object.defineProperty(window.navigator, "clipboard", {
|
|
configurable: true,
|
|
value: { writeText },
|
|
});
|
|
});
|
|
|
|
describe("Share modal", () => {
|
|
it("does not render dialog when closed", () => {
|
|
render(
|
|
<Share
|
|
isOpen={false}
|
|
onClose={vi.fn()}
|
|
previewSettings={previewSettings}
|
|
/>,
|
|
);
|
|
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("renders invite-only copy and people with access", () => {
|
|
openShare();
|
|
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("heading", { level: 1, name: "Share this Rule" }),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByText("Juniper Street Food Share")).toBeInTheDocument();
|
|
expect(screen.getByText("You")).toBeInTheDocument();
|
|
expect(screen.getByText("rosa@example.org")).toBeInTheDocument();
|
|
expect(screen.getByText("Five more people can read it")).toBeInTheDocument();
|
|
expect(screen.getByText("Only people you invite can open it.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("copies the private Rule URL", async () => {
|
|
openShare();
|
|
fireEvent.click(screen.getByRole("button", { name: /Copy link/i }));
|
|
await waitFor(() => {
|
|
expect(screen.getByText("Link copied")).toBeInTheDocument();
|
|
});
|
|
expect(writeText).toHaveBeenCalledWith(
|
|
"https://communityrule.info/rules/rule-1",
|
|
);
|
|
expect(screen.getByRole("button", { name: /Copied/i })).toHaveAttribute(
|
|
"aria-pressed",
|
|
"true",
|
|
);
|
|
});
|
|
|
|
it("asks for community agreement before turning a link on", async () => {
|
|
const user = userEvent.setup();
|
|
openShare();
|
|
await user.click(screen.getByRole("button", { name: /Only people you invite/i }));
|
|
await user.click(
|
|
await screen.findByRole("option", { name: "Anyone with the link" }),
|
|
);
|
|
expect(
|
|
screen.getByText(
|
|
"Anyone with the link can read it without logging in and pass it on. Check with your community first.",
|
|
),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Turn on link" })).toBeDisabled();
|
|
expect(screen.getByRole("button", { name: /Copy link/i })).toBeDisabled();
|
|
await user.click(
|
|
screen.getByRole("checkbox", {
|
|
name: "Our community agreed to share this Rule",
|
|
}),
|
|
);
|
|
expect(screen.getByRole("button", { name: "Turn on link" })).toBeEnabled();
|
|
});
|
|
|
|
it("turns the preview link on after community agreement", async () => {
|
|
const user = userEvent.setup();
|
|
openShare();
|
|
await user.click(screen.getByRole("button", { name: /Only people you invite/i }));
|
|
await user.click(
|
|
await screen.findByRole("option", { name: "Anyone with the link" }),
|
|
);
|
|
await user.click(
|
|
screen.getByRole("checkbox", {
|
|
name: "Our community agreed to share this Rule",
|
|
}),
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "Turn on link" }));
|
|
expect(
|
|
screen.getByDisplayValue("communityrule.info/r/k7f2-9qxm"),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("button", { name: "Turn off link" }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("calls onClose when Done is clicked", async () => {
|
|
const user = userEvent.setup();
|
|
const onClose = vi.fn();
|
|
render(
|
|
<Share
|
|
isOpen
|
|
onClose={onClose}
|
|
previewSettings={previewSettings}
|
|
/>,
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "Done" }));
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|