Skip to content

Commit 1bdbcc3

Browse files
test(tablist): cover dynamic tab recovery and shortcut ownership (#4422)
Exercise TBL-001, TBL-004, TBL-005 and TBL-006 across owner/removal/disable recovery, empty eligibility and finite modifier/orientation matrices. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Srividya Ramaswamy <sriramas@microsoft.com> Copilot-Session: 4b66f3ef-85ac-4405-bd35-dcc167a077b1
1 parent 5885ae1 commit 1bdbcc3

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎.changeset/sparkly-clocks-train.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
---
4+
5+

‎packages/agentic/components/src/components/tablist/tablist.test.tsx‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,155 @@ function selected(tab: ReturnType<typeof getTabs>[number]) {
3737
}
3838

3939
describe('TabList', () => {
40+
it.each(
41+
(['controlled', 'uncontrolled'] as const).flatMap((owner) => (['remove', 'disable'] as const).map((change) => ({ owner, change }))),
42+
)('TBL-001 TBL-004 recovers after $change of the active Tab under $owner selection', async ({ owner, change }) => {
43+
const ref = React.createRef<React.ComponentRef<typeof Pressable>>();
44+
const onSelectionChange = jest.fn();
45+
let context: TabListContextValue | undefined;
46+
function Probe() {
47+
context = React.useContext(TabListContext);
48+
return null;
49+
}
50+
const scene = (invalid: boolean) => (
51+
<TabList
52+
selectedValue={owner === 'controlled' ? 'activity' : undefined}
53+
defaultSelectedValue="activity"
54+
onSelectionChange={onSelectionChange}
55+
>
56+
<Tab controls="overview-panel" content="Overview" accessibilityLabel="Overview" value="overview" ref={ref} />
57+
{!(invalid && change === 'remove') && (
58+
<Tab controls="activity-panel" content="Activity" accessibilityLabel="Activity" value="activity" disabled={invalid} />
59+
)}
60+
<Tab controls="files-panel" content="Files" accessibilityLabel="Files" value="files" />
61+
<Probe />
62+
</TabList>
63+
);
64+
const component = await render(scene(false));
65+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([false, true, false]);
66+
await fireEvent(component.getByRole('tab', { name: 'Activity' }), 'focus', {});
67+
expect(context?.focusedValue).toBe('activity');
68+
const focus = jest.spyOn(ref.current!, 'focus').mockImplementation(() => {
69+
const overview = component.getByRole('tab', { name: 'Overview' });
70+
expect(overview.props.focusable).toBe(true);
71+
expect(selected(overview)).toBe(owner === 'uncontrolled');
72+
});
73+
try {
74+
await component.rerender(scene(true));
75+
76+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual(change === 'remove' ? [true, false] : [true, false, false]);
77+
expect(selected(component.getByRole('tab', { name: 'Overview' }))).toBe(owner === 'uncontrolled');
78+
if (change === 'remove') {
79+
expect(component.queryByRole('tab', { name: 'Activity' })).toBeNull();
80+
expect(getTabs(component).map(selected)).toEqual([owner === 'uncontrolled', false]);
81+
} else {
82+
const activity = component.getByRole('tab', { name: 'Activity' });
83+
expect(activity).toBeDisabled();
84+
expect(selected(activity)).toBe(owner === 'controlled');
85+
}
86+
expect(onSelectionChange.mock.calls).toEqual(owner === 'controlled' ? [] : [['overview']]);
87+
expect(focus).toHaveBeenCalledTimes(1);
88+
expect(context?.focusedValue).toBeUndefined();
89+
90+
await fireEvent(component.getByRole('tab', { name: 'Overview' }), 'focus', {});
91+
expect(context?.focusedValue).toBe('overview');
92+
await component.rerender(scene(true));
93+
expect(focus).toHaveBeenCalledTimes(1);
94+
expect(onSelectionChange.mock.calls).toEqual(owner === 'controlled' ? [] : [['overview']]);
95+
} finally {
96+
focus.mockRestore();
97+
}
98+
});
99+
100+
it.each(['controlled', 'uncontrolled'] as const)(
101+
'TBL-004 exposes no tab stop while all Tabs are disabled and recovers eligibility for %s selection',
102+
async (owner) => {
103+
const onSelectionChange = jest.fn();
104+
const scene = (stage: 'initial' | 'disabled' | 'files') => (
105+
<TabList
106+
disabled={stage === 'disabled'}
107+
selectedValue={owner === 'controlled' ? 'activity' : undefined}
108+
defaultSelectedValue="activity"
109+
onSelectionChange={onSelectionChange}
110+
>
111+
<Tab controls="overview-panel" content="Overview" value="overview" disabled={stage === 'files'} />
112+
<Tab controls="activity-panel" content="Activity" value="activity" disabled={stage === 'files'} />
113+
<Tab controls="files-panel" content="Files" value="files" />
114+
</TabList>
115+
);
116+
const component = await render(scene('initial'));
117+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([false, true, false]);
118+
119+
await component.rerender(scene('disabled'));
120+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([false, false, false]);
121+
expect(getTabs(component).every((tab) => tab.props.accessibilityState.disabled)).toBe(true);
122+
expect(onSelectionChange).not.toHaveBeenCalled();
123+
124+
await component.rerender(scene('files'));
125+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([false, false, true]);
126+
expect(getTabs(component).map(selected)).toEqual(owner === 'controlled' ? [false, true, false] : [false, false, true]);
127+
expect(onSelectionChange.mock.calls).toEqual(owner === 'controlled' ? [] : [['files']]);
128+
},
129+
);
130+
131+
it.each(
132+
(['horizontal', 'vertical'] as const).flatMap((orientation) =>
133+
(['altKey', 'ctrlKey', 'metaKey', 'shiftKey'] as const).flatMap((modifier) =>
134+
[
135+
orientation === 'horizontal' ? 'ArrowLeft' : 'ArrowUp',
136+
orientation === 'horizontal' ? 'ArrowRight' : 'ArrowDown',
137+
'Home',
138+
'End',
139+
].map((key) => ({ orientation, modifier, key })),
140+
),
141+
),
142+
)('TBL-005 TBL-006 forwards $modifier+$key once in $orientation lists without consuming it', async ({ orientation, modifier, key }) => {
143+
const onKeyDown = jest.fn();
144+
const onSelectionChange = jest.fn();
145+
const component = await render(
146+
<TabList orientation={orientation} onSelectionChange={onSelectionChange}>
147+
<Tab controls="one" content="One" onKeyDown={onKeyDown} />
148+
<Tab controls="two" content="Two" />
149+
</TabList>,
150+
);
151+
const event = { nativeEvent: { key, [modifier]: true }, preventDefault: jest.fn(), stopPropagation: jest.fn() };
152+
153+
await fireEvent(getTabs(component)[0], 'keyDown', event);
154+
155+
expect(onKeyDown.mock.calls).toEqual([[event]]);
156+
expect(event.preventDefault).not.toHaveBeenCalled();
157+
expect(event.stopPropagation).not.toHaveBeenCalled();
158+
expect(onSelectionChange).not.toHaveBeenCalled();
159+
expect(getTabs(component).map(selected)).toEqual([true, false]);
160+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([true, false]);
161+
});
162+
163+
it.each([
164+
{ orientation: 'horizontal', key: 'ArrowUp' },
165+
{ orientation: 'horizontal', key: 'ArrowDown' },
166+
{ orientation: 'vertical', key: 'ArrowLeft' },
167+
{ orientation: 'vertical', key: 'ArrowRight' },
168+
] as const)('TBL-005 forwards the inactive-axis $key in $orientation lists', async ({ orientation, key }) => {
169+
const onKeyDown = jest.fn();
170+
const onSelectionChange = jest.fn();
171+
const component = await render(
172+
<TabList orientation={orientation} onSelectionChange={onSelectionChange}>
173+
<Tab controls="one" content="One" onKeyDown={onKeyDown} />
174+
<Tab controls="two" content="Two" />
175+
</TabList>,
176+
);
177+
const event = { nativeEvent: { key }, preventDefault: jest.fn(), stopPropagation: jest.fn() };
178+
179+
await fireEvent(getTabs(component)[0], 'keyDown', event);
180+
181+
expect(onKeyDown.mock.calls).toEqual([[event]]);
182+
expect(event.preventDefault).not.toHaveBeenCalled();
183+
expect(event.stopPropagation).not.toHaveBeenCalled();
184+
expect(onSelectionChange).not.toHaveBeenCalled();
185+
expect(getTabs(component).map(selected)).toEqual([true, false]);
186+
expect(getTabs(component).map((tab) => tab.props.focusable)).toEqual([true, false]);
187+
});
188+
40189
it('defaults to the first enabled Tab and exposes the list semantics', async () => {
41190
const component = await renderTabList();
42191
const root = getRoot(component);

0 commit comments

Comments
 (0)