|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A Tab visibility row keeps its name input across keystrokes (objectui#26748). |
| 5 | + * |
| 6 | + * Each row was keyed by the tab name its input edits, so every keystroke |
| 7 | + * renamed the key, remounted the row, and the input lost focus after the first |
| 8 | + * character: typing `showcase_app` stored `{"s":"hidden"}`. |
| 9 | + * |
| 10 | + * What is pinned: |
| 11 | + * - a name typed one key at a time (not pasted) into a new row keeps focus |
| 12 | + * and lands whole in `tabPermissions`, with the row's visibility; |
| 13 | + * - renaming an existing row keeps the other rows and their order; |
| 14 | + * - a name that passes through another row's name merges nothing, and the |
| 15 | + * row writes again once the name is free; |
| 16 | + * - the remove button drops only its own row. |
| 17 | + * |
| 18 | + * DIRECTION: with `key={tab}` restored the typed-name pin is red (focus leaves |
| 19 | + * after the first key). |
| 20 | + */ |
| 21 | + |
| 22 | +import "@testing-library/jest-dom/vitest"; |
| 23 | +import * as React from "react"; |
| 24 | +import { afterEach, describe, expect, it } from "vitest"; |
| 25 | +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; |
| 26 | +import userEvent from "@testing-library/user-event"; |
| 27 | +import { PermissionAdvancedFacets } from "./PermissionAdvancedFacets"; |
| 28 | + |
| 29 | +afterEach(cleanup); |
| 30 | + |
| 31 | +const t = (k: string) => k; |
| 32 | + |
| 33 | +type Draft = Record<string, unknown>; |
| 34 | + |
| 35 | +/** A stateful host, so each write re-renders the facets like the editor does. */ |
| 36 | +function Host({ |
| 37 | + initial, |
| 38 | + onDraft, |
| 39 | +}: { |
| 40 | + initial: Draft; |
| 41 | + onDraft: (d: Draft) => void; |
| 42 | +}) { |
| 43 | + const [draft, setDraft] = React.useState<Draft>(initial); |
| 44 | + React.useEffect(() => { |
| 45 | + onDraft(draft); |
| 46 | + }, [draft, onDraft]); |
| 47 | + return ( |
| 48 | + <PermissionAdvancedFacets |
| 49 | + draft={draft} |
| 50 | + setDraft={setDraft} |
| 51 | + writable |
| 52 | + allSetNames={[]} |
| 53 | + t={t} |
| 54 | + /> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +function mount(tabPermissions: Record<string, string>) { |
| 59 | + let latest: Draft = {}; |
| 60 | + render( |
| 61 | + <Host |
| 62 | + initial={{ tabPermissions }} |
| 63 | + onDraft={(d) => { |
| 64 | + latest = d; |
| 65 | + }} |
| 66 | + />, |
| 67 | + ); |
| 68 | + fireEvent.click(screen.getByText("perm.tabs.title")); |
| 69 | + return { tabs: () => latest.tabPermissions }; |
| 70 | +} |
| 71 | + |
| 72 | +describe("a Tab visibility row keeps its name input across keystrokes (objectui#26748)", () => { |
| 73 | + it("stores a name typed key by key into a new row, focus kept", async () => { |
| 74 | + const user = userEvent.setup(); |
| 75 | + const { tabs } = mount({ "": "hidden" }); |
| 76 | + const input = screen.getByDisplayValue(""); |
| 77 | + await user.click(input); |
| 78 | + await user.type(input, "showcase_app"); |
| 79 | + expect(document.activeElement).toBe(input); |
| 80 | + expect(input).toHaveValue("showcase_app"); |
| 81 | + expect(tabs()).toEqual({ showcase_app: "hidden" }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("renames an existing row, keeping the others in order", async () => { |
| 85 | + const user = userEvent.setup(); |
| 86 | + const { tabs } = mount({ a_tab: "hidden", b_tab: "visible" }); |
| 87 | + const input = screen.getByDisplayValue("a_tab"); |
| 88 | + await user.type(input, "_x"); |
| 89 | + expect(document.activeElement).toBe(input); |
| 90 | + expect(JSON.stringify(tabs())).toBe( |
| 91 | + '{"a_tab_x":"hidden","b_tab":"visible"}', |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it("merges nothing while the typed name is another row’s", async () => { |
| 96 | + const user = userEvent.setup(); |
| 97 | + const { tabs } = mount({ app: "visible", "": "hidden" }); |
| 98 | + const input = screen.getByDisplayValue(""); |
| 99 | + await user.type(input, "app"); |
| 100 | + expect(input).toHaveValue("app"); |
| 101 | + expect(JSON.stringify(tabs())).toBe('{"app":"visible","ap":"hidden"}'); |
| 102 | + await user.type(input, "_x"); |
| 103 | + expect(JSON.stringify(tabs())).toBe('{"app":"visible","app_x":"hidden"}'); |
| 104 | + expect(screen.getByDisplayValue("app")).not.toBe(input); |
| 105 | + }); |
| 106 | + |
| 107 | + it("removes only its own row", () => { |
| 108 | + const { tabs } = mount({ a_tab: "hidden", b_tab: "visible" }); |
| 109 | + fireEvent.click(screen.getAllByLabelText("perm.tabs.remove")[0]!); |
| 110 | + expect(tabs()).toEqual({ b_tab: "visible" }); |
| 111 | + expect(screen.getByDisplayValue("b_tab")).toBeInTheDocument(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments