|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Pins what the Delegated Admin Scope facet's switches draw against the spec's |
| 5 | + * own defaults (objectstack#26706). |
| 6 | + * |
| 7 | + * A delegated-admin scope may leave a defaulted key unset; the server parses it |
| 8 | + * with `AdminScopeSchema`, so the unset key means that schema's default. Each |
| 9 | + * switch must therefore draw the spec default for an unset key, draw an |
| 10 | + * explicit boolean as itself, write nothing merely by being drawn, and write an |
| 11 | + * explicit boolean when the author flips it. |
| 12 | + * |
| 13 | + * Every expected value is read from the installed spec (`AdminScopeSchema`), |
| 14 | + * not restated here, so the pin follows the protocol: a default the spec moves |
| 15 | + * fails this file against the editor's restated |
| 16 | + * `ADMIN_SCOPE_SWITCH_DEFAULTS`. The CONTROL case keeps a reading that never |
| 17 | + * reached the schema from passing as a reading about the editor. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from "react"; |
| 21 | +import { describe, it, expect, afterEach } from "vitest"; |
| 22 | +import { render, screen, within, cleanup } from "@testing-library/react"; |
| 23 | +import userEvent from "@testing-library/user-event"; |
| 24 | +import { AdminScopeSchema } from "@objectstack/spec/security"; |
| 25 | +import { PermissionAdvancedFacets } from "./PermissionAdvancedFacets"; |
| 26 | + |
| 27 | +afterEach(cleanup); |
| 28 | + |
| 29 | +const t = (k: string) => k; |
| 30 | + |
| 31 | +type Draft = Record<string, unknown>; |
| 32 | + |
| 33 | +/** The draft as the host would hold it after the last update. */ |
| 34 | +let latest: Draft = {}; |
| 35 | + |
| 36 | +function Harness({ initial }: { initial: Draft }) { |
| 37 | + const [draft, setDraft] = React.useState<Draft>(initial); |
| 38 | + React.useEffect(() => { |
| 39 | + latest = draft; |
| 40 | + }, [draft]); |
| 41 | + return ( |
| 42 | + <PermissionAdvancedFacets |
| 43 | + draft={draft} |
| 44 | + setDraft={setDraft} |
| 45 | + writable |
| 46 | + allSetNames={[]} |
| 47 | + t={t} |
| 48 | + only="admin" |
| 49 | + /> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +function mount(initial: Draft) { |
| 54 | + const user = userEvent.setup(); |
| 55 | + latest = initial; |
| 56 | + render(<Harness initial={initial} />); |
| 57 | + return user; |
| 58 | +} |
| 59 | + |
| 60 | +const BU = "bu_east"; |
| 61 | + |
| 62 | +/** Each boolean key the facet draws a switch for, with the switch's caption. */ |
| 63 | +const SWITCHES = [ |
| 64 | + ["includeSubtree", "perm.admin.includeSubtree"], |
| 65 | + ["manageAssignments", "perm.admin.manageAssignments"], |
| 66 | +] as const; |
| 67 | + |
| 68 | +/** The spec's own parse of a scope that names its business unit and nothing else. */ |
| 69 | +const specParsed = () => AdminScopeSchema.parse({ businessUnit: BU }); |
| 70 | + |
| 71 | +/** A switch lives inside the `<label>` that carries its caption. */ |
| 72 | +const switchFor = (captionKey: string) => |
| 73 | + within(screen.getByText(captionKey).closest("label") as HTMLElement).getByRole("switch"); |
| 74 | + |
| 75 | +const drawn = (captionKey: string) => switchFor(captionKey).getAttribute("aria-checked"); |
| 76 | + |
| 77 | +describe("PermissionAdvancedFacets · delegated-admin switches draw the spec default (objectstack#26706)", () => { |
| 78 | + it("CONTROL — the installed spec parses each unset switch key to a boolean default", () => { |
| 79 | + const parsed = specParsed(); |
| 80 | + for (const [key] of SWITCHES) { |
| 81 | + expect(typeof parsed[key], key).toBe("boolean"); |
| 82 | + } |
| 83 | + expect(parsed.includeSubtree).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("an unset key draws the spec default", () => { |
| 87 | + mount({ adminScope: { businessUnit: BU } }); |
| 88 | + const parsed = specParsed(); |
| 89 | + for (const [key, caption] of SWITCHES) { |
| 90 | + expect(drawn(caption), key).toBe(String(parsed[key])); |
| 91 | + } |
| 92 | + expect(switchFor("perm.admin.includeSubtree")).toBeChecked(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("the switches draw the same defaults while they wait for a business unit", () => { |
| 96 | + mount({}); |
| 97 | + const parsed = specParsed(); |
| 98 | + for (const [key, caption] of SWITCHES) { |
| 99 | + expect(switchFor(caption)).toBeDisabled(); |
| 100 | + expect(drawn(caption), key).toBe(String(parsed[key])); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it("naming the business unit writes that key alone: drawing a default never writes it", async () => { |
| 105 | + const user = mount({}); |
| 106 | + await user.type(screen.getByRole("textbox"), BU); |
| 107 | + |
| 108 | + expect(latest.adminScope).toEqual({ businessUnit: BU }); |
| 109 | + const parsed = specParsed(); |
| 110 | + for (const [key, caption] of SWITCHES) { |
| 111 | + expect(drawn(caption), key).toBe(String(parsed[key])); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it("a flip writes an explicit boolean, the opposite of what was drawn, and draws it", async () => { |
| 116 | + const parsed = specParsed(); |
| 117 | + for (const [key, caption] of SWITCHES) { |
| 118 | + const user = mount({ adminScope: { businessUnit: BU } }); |
| 119 | + |
| 120 | + await user.click(switchFor(caption)); |
| 121 | + expect(latest.adminScope, key).toEqual({ businessUnit: BU, [key]: !parsed[key] }); |
| 122 | + expect(drawn(caption), key).toBe(String(!parsed[key])); |
| 123 | + |
| 124 | + // Flipped back, the key stays written: explicit, and equal to the default. |
| 125 | + await user.click(switchFor(caption)); |
| 126 | + expect(latest.adminScope, key).toEqual({ businessUnit: BU, [key]: parsed[key] }); |
| 127 | + expect(drawn(caption), key).toBe(String(parsed[key])); |
| 128 | + expect(AdminScopeSchema.safeParse(latest.adminScope).success, key).toBe(true); |
| 129 | + |
| 130 | + cleanup(); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it("an explicit boolean draws as itself, whichever the default", () => { |
| 135 | + for (const value of [true, false]) { |
| 136 | + mount({ adminScope: { businessUnit: BU, includeSubtree: value, manageAssignments: value } }); |
| 137 | + for (const [key, caption] of SWITCHES) { |
| 138 | + expect(drawn(caption), `${key}: ${value}`).toBe(String(value)); |
| 139 | + } |
| 140 | + cleanup(); |
| 141 | + } |
| 142 | + }); |
| 143 | +}); |
0 commit comments