The listbox was clipped inside the sheet, sat at 24px, and ignored arrow keys, so Anyone with the link could not be chosen by mouse or touch.
240 lines
7.6 KiB
TypeScript
240 lines
7.6 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 { patchRuleShare } from "../../../lib/create/api";
|
||
import type { RuleShareSettings } from "../../../lib/ruleShareTypes";
|
||
|
||
vi.mock("../../../lib/create/api", () => ({
|
||
patchRuleShare: vi.fn(),
|
||
fetchRuleShare: vi.fn(),
|
||
addRuleStakeholder: vi.fn(),
|
||
}));
|
||
|
||
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);
|
||
|
||
const sharedSettings: RuleShareSettings = {
|
||
...previewSettings,
|
||
visibility: "shared_by_link",
|
||
shareSlug: "k7f2-9qxm",
|
||
shareUrl: "https://communityrule.info/r/k7f2-9qxm",
|
||
shareUrlDisplay: "communityrule.info/r/k7f2-9qxm",
|
||
};
|
||
|
||
beforeEach(() => {
|
||
writeText.mockReset();
|
||
writeText.mockResolvedValue(undefined);
|
||
vi.mocked(patchRuleShare).mockReset();
|
||
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("keeps the link on and says so when turning the Rule private does not save", async () => {
|
||
vi.mocked(patchRuleShare).mockResolvedValueOnce({
|
||
ok: false,
|
||
status: 500,
|
||
error: "nope",
|
||
});
|
||
const user = userEvent.setup();
|
||
render(
|
||
<Share
|
||
isOpen
|
||
onClose={vi.fn()}
|
||
ruleId="rule-1"
|
||
previewSettings={sharedSettings}
|
||
/>,
|
||
);
|
||
await user.click(
|
||
screen.getByRole("button", { name: /Anyone with the link/i }),
|
||
);
|
||
await user.click(
|
||
await screen.findByRole("option", { name: "Only people you invite" }),
|
||
);
|
||
expect(await screen.findByText("That change didn’t save")).toBeInTheDocument();
|
||
expect(
|
||
screen.getByRole("button", { name: "Turn off link" }),
|
||
).toBeInTheDocument();
|
||
expect(
|
||
screen.queryByText("Only people you invite can open it."),
|
||
).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("keeps the link on and says so when turning the link off does not save", async () => {
|
||
vi.mocked(patchRuleShare).mockResolvedValueOnce({
|
||
ok: false,
|
||
status: 500,
|
||
error: "nope",
|
||
});
|
||
const user = userEvent.setup();
|
||
render(
|
||
<Share
|
||
isOpen
|
||
onClose={vi.fn()}
|
||
ruleId="rule-1"
|
||
previewSettings={sharedSettings}
|
||
/>,
|
||
);
|
||
await user.click(screen.getByRole("button", { name: "Turn off link" }));
|
||
expect(await screen.findByText("That change didn’t save")).toBeInTheDocument();
|
||
expect(
|
||
screen.getByDisplayValue("communityrule.info/r/k7f2-9qxm"),
|
||
).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows both access options and picks one with arrow keys", async () => {
|
||
const user = userEvent.setup();
|
||
openShare();
|
||
const trigger = screen.getByRole("button", {
|
||
name: /Only people you invite/i,
|
||
});
|
||
trigger.focus();
|
||
await user.keyboard("{ArrowDown}");
|
||
const inviteOnly = await screen.findByRole("option", {
|
||
name: "Only people you invite",
|
||
});
|
||
const anyone = screen.getByRole("option", { name: "Anyone with the link" });
|
||
expect(inviteOnly).toBeVisible();
|
||
expect(anyone).toBeVisible();
|
||
await user.keyboard("{ArrowDown}{Enter}");
|
||
expect(
|
||
screen.getByText(
|
||
"Anyone with the link can read it without logging in and pass it on. Check with your community first.",
|
||
),
|
||
).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);
|
||
});
|
||
});
|