Skip to content

Commit 2039307

Browse files
committed
test(plugin-detail): pin the inline-edit pencil's keyboard focus reveal
Red first: the read-mode pencil in DetailSection and HeaderHighlight is revealed on hover only, so a Tab stop lands on an invisible control. Claude-Session: https://claude.ai/code/session_01SMps7wq7rBTk53e8KTSEup Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2409c9c commit 2039307

1 file changed

Lines changed: 205 additions & 0 deletions

File tree

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
import { describe, it, expect, vi, beforeAll } from 'vitest';
10+
import { render, fireEvent, screen } from '@testing-library/react';
11+
import { InlineEditProvider } from '@object-ui/react';
12+
import type { DetailViewSection } from '@object-ui/types';
13+
import { DetailSection } from '../DetailSection';
14+
import { HeaderHighlight } from '../HeaderHighlight';
15+
16+
/**
17+
* objectstack#25883 item 1 — the read-mode inline-edit pencil was revealed on
18+
* HOVER only (`opacity-0 group-hover:opacity-100`). It is a real `<button>`, so
19+
* a keyboard Tab stop landed on it while it stayed fully transparent: the
20+
* focus ring the shared `Button` draws was painted at opacity 0.
21+
*
22+
* The pencil must now also show while it, or anything else in its field, has
23+
* keyboard focus, in both places it is drawn: the details body
24+
* (`DetailSection`) and the highlights strip (`HeaderHighlight`). Hover and
25+
* double-click behave exactly as before.
26+
*
27+
* jsdom has no layout and no CSS engine, so "visible on focus" cannot be
28+
* measured here. What these tests pin is the contract a browser turns into
29+
* visibility: the pencil is a focusable button, it carries the focus-reveal
30+
* utilities next to the unchanged hover reveal, the shared focus ring is still
31+
* on it, and the `group` its `group-*` variants resolve against is its own
32+
* field. Activation and double-click are asserted behaviourally.
33+
*
34+
* The pencil is found by its icon, not by its localized label, so the tests
35+
* hold in every locale.
36+
*/
37+
38+
const pencilButtons = (root: HTMLElement): HTMLButtonElement[] =>
39+
Array.from(root.querySelectorAll('svg.lucide-pencil')).map(
40+
(svg) => svg.closest('button') as HTMLButtonElement,
41+
);
42+
43+
function expectFocusRevealContract(pencil: HTMLButtonElement) {
44+
// A real, enabled, tabbable control: the Tab stop the defect is about.
45+
expect(pencil.tagName).toBe('BUTTON');
46+
expect(pencil).not.toBeDisabled();
47+
expect(pencil.getAttribute('tabindex')).not.toBe('-1');
48+
pencil.focus();
49+
expect(pencil).toHaveFocus();
50+
51+
const cls = pencil.classList;
52+
// Hidden at rest, revealed on hover: unchanged.
53+
expect(cls.contains('opacity-0')).toBe(true);
54+
expect(cls.contains('group-hover:opacity-100')).toBe(true);
55+
// Revealed on keyboard focus of the pencil itself …
56+
expect(cls.contains('focus-visible:opacity-100')).toBe(true);
57+
// … and while focus is anywhere inside its field (e.g. a lookup's link).
58+
expect(cls.contains('group-focus-within:opacity-100')).toBe(true);
59+
// The shared Button's focus ring survives the className merge, so the
60+
// revealed pencil also shows where focus is.
61+
expect(cls.contains('focus-visible:ring-2')).toBe(true);
62+
63+
// `group-*` variants resolve against the nearest `.group` ancestor; it must
64+
// be this field's own wrapper, so focus in one field reveals only its pencil.
65+
const group = pencil.closest('.group');
66+
expect(group).not.toBeNull();
67+
expect(pencilButtons(group as HTMLElement)).toEqual([pencil]);
68+
}
69+
70+
describe('DetailSection read-mode pencil — keyboard focus reveal (#25883)', () => {
71+
beforeAll(() => {
72+
// useIsMobile() keys off window.innerWidth; the desktop row carries the pencil.
73+
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 });
74+
});
75+
76+
const objectSchema = {
77+
fields: {
78+
title: { type: 'text' },
79+
city: { type: 'text' },
80+
score: { type: 'formula' },
81+
},
82+
};
83+
const section: DetailViewSection = {
84+
fields: [
85+
{ name: 'title', label: 'Title' },
86+
{ name: 'city', label: 'City' },
87+
{ name: 'score', label: 'Score' },
88+
],
89+
} as DetailViewSection;
90+
const data = { title: 'Hello', city: 'Paris', score: 42 };
91+
92+
it('draws one focus-revealed pencil per editable field, none on a computed one', () => {
93+
const { container } = render(
94+
<DetailSection
95+
section={section}
96+
data={data}
97+
objectSchema={objectSchema}
98+
onEnterInlineEdit={vi.fn()}
99+
/>,
100+
);
101+
const pencils = pencilButtons(container);
102+
expect(pencils).toHaveLength(2); // title + city; the formula has none
103+
pencils.forEach(expectFocusRevealContract);
104+
});
105+
106+
it('activating the focused pencil enters inline edit on its own field', () => {
107+
const onEnter = vi.fn();
108+
const { container } = render(
109+
<DetailSection
110+
section={section}
111+
data={data}
112+
objectSchema={objectSchema}
113+
onEnterInlineEdit={onEnter}
114+
/>,
115+
);
116+
const [titlePencil, cityPencil] = pencilButtons(container);
117+
cityPencil.focus();
118+
// Enter / Space on a native <button> dispatch `click`.
119+
fireEvent.click(cityPencil);
120+
expect(onEnter).toHaveBeenCalledTimes(1);
121+
expect(onEnter).toHaveBeenCalledWith('city');
122+
expect(titlePencil).not.toHaveFocus();
123+
});
124+
125+
it('double-click on the value still enters inline edit', () => {
126+
const onEnter = vi.fn();
127+
render(
128+
<DetailSection
129+
section={section}
130+
data={data}
131+
objectSchema={objectSchema}
132+
onEnterInlineEdit={onEnter}
133+
/>,
134+
);
135+
fireEvent.doubleClick(screen.getByText('Hello'));
136+
expect(onEnter).toHaveBeenCalledWith('title');
137+
});
138+
139+
it('draws no pencil when the record offers no inline edit', () => {
140+
const { container } = render(
141+
<DetailSection section={section} data={data} objectSchema={objectSchema} />,
142+
);
143+
expect(pencilButtons(container)).toHaveLength(0);
144+
});
145+
});
146+
147+
describe('HeaderHighlight pencil — keyboard focus reveal (#25883)', () => {
148+
beforeAll(() => {
149+
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 });
150+
});
151+
152+
const objectSchema = {
153+
fields: {
154+
owner: { type: 'text' },
155+
score: { type: 'formula' },
156+
},
157+
};
158+
const fields = [
159+
{ name: 'owner', label: 'Owner' },
160+
{ name: 'score', label: 'Score' },
161+
] as any;
162+
const data = { owner: 'Alice', score: '99' };
163+
164+
it('draws a focus-revealed pencil on the editable highlight only', () => {
165+
const { container } = render(
166+
<InlineEditProvider canEdit>
167+
<HeaderHighlight fields={fields} data={data} objectSchema={objectSchema} />
168+
</InlineEditProvider>,
169+
);
170+
const pencils = pencilButtons(container);
171+
expect(pencils).toHaveLength(1); // owner; the formula has none
172+
expectFocusRevealContract(pencils[0]);
173+
});
174+
175+
it('activating the focused pencil enters the shared edit session', () => {
176+
const { container } = render(
177+
<InlineEditProvider canEdit>
178+
<HeaderHighlight fields={fields} data={data} objectSchema={objectSchema} />
179+
</InlineEditProvider>,
180+
);
181+
const [pencil] = pencilButtons(container);
182+
pencil.focus();
183+
fireEvent.click(pencil);
184+
expect(screen.getByDisplayValue('Alice')).toBeInTheDocument();
185+
});
186+
187+
it('double-click on the highlight still enters the shared edit session', () => {
188+
render(
189+
<InlineEditProvider canEdit>
190+
<HeaderHighlight fields={fields} data={data} objectSchema={objectSchema} />
191+
</InlineEditProvider>,
192+
);
193+
fireEvent.doubleClick(screen.getByText('Alice'));
194+
expect(screen.getByDisplayValue('Alice')).toBeInTheDocument();
195+
});
196+
197+
it('draws no pencil when the record is not editable', () => {
198+
const { container } = render(
199+
<InlineEditProvider canEdit={false}>
200+
<HeaderHighlight fields={fields} data={data} objectSchema={objectSchema} />
201+
</InlineEditProvider>,
202+
);
203+
expect(pencilButtons(container)).toHaveLength(0);
204+
});
205+
});

0 commit comments

Comments
 (0)