-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRequireAdmin.test.tsx
More file actions
84 lines (73 loc) · 2.41 KB
/
Copy pathRequireAdmin.test.tsx
File metadata and controls
84 lines (73 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as React from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render } from "../utils/render.tsx";
// Mirror @emdash-cms/auth Role levels (kept inline, matching RequireAdmin.tsx).
const ROLE_AUTHOR = 30;
const ROLE_EDITOR = 40;
const ROLE_ADMIN = 50;
const currentUser = vi.hoisted(() => ({
role: 50 as number,
isLoading: false,
signedIn: true,
}));
vi.mock("../../src/lib/api/current-user.js", () => ({
useCurrentUser: () => ({
data: currentUser.signedIn
? { id: "user-1", email: "user@example.com", role: currentUser.role }
: undefined,
isLoading: currentUser.isLoading,
}),
}));
// Import after mocks
const { RequireAdmin } = await import("../../src/components/RequireAdmin");
const CHILD_TEXT = "Protected settings content";
describe("RequireAdmin", () => {
beforeEach(() => {
currentUser.role = ROLE_ADMIN;
currentUser.isLoading = false;
currentUser.signedIn = true;
});
it("renders its children for an admin", async () => {
const screen = await render(
<RequireAdmin>
<p>{CHILD_TEXT}</p>
</RequireAdmin>,
);
await expect.element(screen.getByText(CHILD_TEXT)).toBeInTheDocument();
await expect.element(screen.getByText("Access denied")).not.toBeInTheDocument();
});
it.each([
["editor", ROLE_EDITOR],
["author", ROLE_AUTHOR],
])("shows Access denied instead of its children for an %s", async (_label, role) => {
currentUser.role = role;
const screen = await render(
<RequireAdmin>
<p>{CHILD_TEXT}</p>
</RequireAdmin>,
);
await expect.element(screen.getByText("Access denied")).toBeInTheDocument();
await expect.element(screen.getByText(CHILD_TEXT)).not.toBeInTheDocument();
});
it("treats a missing user as denied", async () => {
currentUser.signedIn = false;
const screen = await render(
<RequireAdmin>
<p>{CHILD_TEXT}</p>
</RequireAdmin>,
);
await expect.element(screen.getByText("Access denied")).toBeInTheDocument();
await expect.element(screen.getByText(CHILD_TEXT)).not.toBeInTheDocument();
});
it("renders neither children nor the denial while the user is loading", async () => {
currentUser.isLoading = true;
currentUser.signedIn = false;
const screen = await render(
<RequireAdmin>
<p>{CHILD_TEXT}</p>
</RequireAdmin>,
);
await expect.element(screen.getByText(CHILD_TEXT)).not.toBeInTheDocument();
await expect.element(screen.getByText("Access denied")).not.toBeInTheDocument();
});
});