Skip to content
Source
Sign inSign up
Open History to see the last commit

AuthorizedClientsSettings.browser.test.tsx

80 lines2.6 KB

1import {
2 EnvironmentId,
3 managedCapabilityDenial,
4 type ManagedPolicySummary,
5} from "@t3tools/contracts";
6import { describe, expect, it, vi } from "vite-plus/test";
7​
8import "~/index.css";
9import { render } from "../../../test/render";
10import { AuthorizedClientsHeaderAction } from "./AuthorizedClientsSettings";
11​
12const localId = EnvironmentId.make("local");
13const serviceId = EnvironmentId.make("service");
14​
15const state = vi.hoisted(() => ({
16 primaryEnvironmentId: null as string | null,
17 policies: new Map<string, unknown>(),
18}));
19​
20vi.mock(import("../../state/environments"), async (original) => ({
21 ...(await original()),
22 usePrimaryEnvironmentId: (() => state.primaryEnvironmentId) as never,
23}));
24vi.mock(import("../../state/entities"), async (original) => ({
25 ...(await original()),
26 useServerConfigs: (() =>
27 new Map(
28 [...state.policies].map(([environmentId, managedPolicy]) => [
29 environmentId,
30 { managedPolicy },
31 ]),
32 )) as never,
33}));
34vi.mock(import("../../state/use-atom-command"), async (original) => ({
35 ...(await original()),
36 useAtomCommand: (() => vi.fn()) as never,
37}));
38​
39const remoteClientsOff: ManagedPolicySummary = {
40 organization: "Example Corp",
41 lockedSettings: [],
42 limits: { clients: ["desktop"] },
43};
44​
45function setup(policies: ReadonlyArray<readonly [EnvironmentId, ManagedPolicySummary]>) {
46 state.primaryEnvironmentId = localId;
47 state.policies = new Map(policies);
49​
50const renderHeader = (environmentId: EnvironmentId) =>
51 render(
52 <AuthorizedClientsHeaderAction
53 environmentId={environmentId}
54 onPairingLinkCreated={() => {}}
55 clientSessions={null}
56 isRevokingOtherClients={false}
57 onRevokeOtherClients={() => {}}
58 delegatableScopes={[]}
59 />,
60 );
61​
62describe("AuthorizedClientsHeaderAction", () => {
63 const denial = managedCapabilityDenial(remoteClientsOff, "remoteClients")!;
64​
65 it("follows the selected service's policy, not the primary's", async () => {
66 setup([[serviceId, remoteClientsOff]]);
67 await using screen = await renderHeader(serviceId);
68​
69 await expect.element(screen.getByRole("group", { name: denial })).toBeVisible();
70 await expect.element(screen.getByRole("button", { name: "Create link" })).toBeDisabled();
71 });
72​
73 it("leaves pairing open on an unmanaged service when the primary is managed", async () => {
74 setup([[localId, remoteClientsOff]]);
75 await using screen = await renderHeader(serviceId);
76​
77 await expect.element(screen.getByRole("button", { name: "Create link" })).toBeEnabled();
78 expect(screen.getByRole("group", { name: denial }).query()).toBeNull();
79 });
80});