@tallyui/primitives 0.1.0 → 3.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +11 -8
- package/src/__tests__/accordion.test.tsx +0 -370
- package/src/__tests__/alert-dialog.test.tsx +0 -127
- package/src/__tests__/animated.test.tsx +0 -184
- package/src/__tests__/checkbox.test.tsx +0 -90
- package/src/__tests__/collapsible.test.tsx +0 -149
- package/src/__tests__/combobox.test.tsx +0 -292
- package/src/__tests__/context-menu.test.tsx +0 -226
- package/src/__tests__/dialog.test.tsx +0 -81
- package/src/__tests__/dropdown-menu.test.tsx +0 -119
- package/src/__tests__/hooks.test.tsx +0 -46
- package/src/__tests__/hover-card.test.tsx +0 -77
- package/src/__tests__/image.test.tsx +0 -53
- package/src/__tests__/label.test.tsx +0 -50
- package/src/__tests__/list.test.tsx +0 -63
- package/src/__tests__/popover.test.tsx +0 -63
- package/src/__tests__/portal.test.tsx +0 -66
- package/src/__tests__/progress.test.tsx +0 -70
- package/src/__tests__/radio-group.test.tsx +0 -164
- package/src/__tests__/select.test.tsx +0 -76
- package/src/__tests__/separator.test.tsx +0 -37
- package/src/__tests__/slider.test.tsx +0 -124
- package/src/__tests__/slot.test.tsx +0 -56
- package/src/__tests__/switch.test.tsx +0 -70
- package/src/__tests__/table.test.tsx +0 -172
- package/src/__tests__/tabs.test.tsx +0 -67
- package/src/__tests__/text.test.tsx +0 -59
- package/src/__tests__/toast.test.tsx +0 -137
- package/src/__tests__/toggle-group.test.tsx +0 -167
- package/src/__tests__/toggle.test.tsx +0 -60
- package/src/__tests__/tooltip.test.tsx +0 -77
- package/src/__tests__/types.test-d.ts +0 -106
- package/src/__tests__/utils.test.ts +0 -47
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
import { render, screen, fireEvent } from '@testing-library/react';
|
|
2
|
-
import { Pressable, View } from 'react-native';
|
|
3
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
4
|
-
import * as Combobox from '../combobox';
|
|
5
|
-
import { PortalHost } from '../portal';
|
|
6
|
-
|
|
7
|
-
const FRUITS = [
|
|
8
|
-
{ value: 'apple', label: 'Apple' },
|
|
9
|
-
{ value: 'banana', label: 'Banana' },
|
|
10
|
-
{ value: 'cherry', label: 'Cherry' },
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
describe('Combobox', () => {
|
|
14
|
-
const renderCombobox = (props: Record<string, any> = {}, items = FRUITS) =>
|
|
15
|
-
render(
|
|
16
|
-
<div>
|
|
17
|
-
<PortalHost />
|
|
18
|
-
<Combobox.Root {...props}>
|
|
19
|
-
<Combobox.Input testID="input" placeholder="Search fruits..." />
|
|
20
|
-
<Combobox.Trigger testID="trigger" />
|
|
21
|
-
<Combobox.Portal>
|
|
22
|
-
<Combobox.Content testID="content">
|
|
23
|
-
{items.map((fruit) => (
|
|
24
|
-
<Combobox.Item
|
|
25
|
-
key={fruit.value}
|
|
26
|
-
value={fruit.value}
|
|
27
|
-
label={fruit.label}
|
|
28
|
-
testID={`item-${fruit.value}`}
|
|
29
|
-
>
|
|
30
|
-
<Combobox.ItemText />
|
|
31
|
-
</Combobox.Item>
|
|
32
|
-
))}
|
|
33
|
-
{items.length === 0 && (
|
|
34
|
-
<Combobox.Empty testID="empty">
|
|
35
|
-
<span>No results</span>
|
|
36
|
-
</Combobox.Empty>
|
|
37
|
-
)}
|
|
38
|
-
</Combobox.Content>
|
|
39
|
-
</Combobox.Portal>
|
|
40
|
-
</Combobox.Root>
|
|
41
|
-
</div>
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
// -------------------------------------------------------------------------
|
|
45
|
-
// ARIA roles
|
|
46
|
-
// -------------------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
it('input has role="combobox"', () => {
|
|
49
|
-
renderCombobox({ defaultOpen: true });
|
|
50
|
-
expect(screen.getByTestId('input').getAttribute('role')).toBe('combobox');
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('content has role="listbox"', () => {
|
|
54
|
-
renderCombobox({ defaultOpen: true });
|
|
55
|
-
expect(screen.getByTestId('content').getAttribute('role')).toBe('listbox');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('items have role="option"', () => {
|
|
59
|
-
renderCombobox({ defaultOpen: true });
|
|
60
|
-
expect(screen.getByTestId('item-apple').getAttribute('role')).toBe('option');
|
|
61
|
-
expect(screen.getByTestId('item-banana').getAttribute('role')).toBe('option');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('trigger has role="button"', () => {
|
|
65
|
-
renderCombobox();
|
|
66
|
-
expect(screen.getByTestId('trigger').getAttribute('role')).toBe('button');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// -------------------------------------------------------------------------
|
|
70
|
-
// aria-expanded
|
|
71
|
-
// -------------------------------------------------------------------------
|
|
72
|
-
|
|
73
|
-
it('aria-expanded on input reflects open state', () => {
|
|
74
|
-
renderCombobox();
|
|
75
|
-
const input = screen.getByTestId('input');
|
|
76
|
-
expect(input.getAttribute('aria-expanded')).toBe('false');
|
|
77
|
-
|
|
78
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
79
|
-
expect(input.getAttribute('aria-expanded')).toBe('true');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// -------------------------------------------------------------------------
|
|
83
|
-
// aria-selected
|
|
84
|
-
// -------------------------------------------------------------------------
|
|
85
|
-
|
|
86
|
-
it('aria-selected is true on the selected item', () => {
|
|
87
|
-
renderCombobox({
|
|
88
|
-
defaultOpen: true,
|
|
89
|
-
value: { value: 'apple', label: 'Apple' },
|
|
90
|
-
});
|
|
91
|
-
expect(screen.getByTestId('item-apple').getAttribute('aria-selected')).toBe('true');
|
|
92
|
-
expect(screen.getByTestId('item-banana').getAttribute('aria-selected')).toBe('false');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// -------------------------------------------------------------------------
|
|
96
|
-
// Selection
|
|
97
|
-
// -------------------------------------------------------------------------
|
|
98
|
-
|
|
99
|
-
it('selecting an item updates value and closes dropdown', () => {
|
|
100
|
-
const onValueChange = vi.fn();
|
|
101
|
-
renderCombobox({ onValueChange, defaultOpen: true });
|
|
102
|
-
|
|
103
|
-
fireEvent.click(screen.getByTestId('item-apple'));
|
|
104
|
-
|
|
105
|
-
expect(onValueChange).toHaveBeenCalledWith(
|
|
106
|
-
expect.objectContaining({ value: 'apple', label: 'Apple' })
|
|
107
|
-
);
|
|
108
|
-
// Dropdown should be closed after selection
|
|
109
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// -------------------------------------------------------------------------
|
|
113
|
-
// Controlled value
|
|
114
|
-
// -------------------------------------------------------------------------
|
|
115
|
-
|
|
116
|
-
it('controlled value mode', () => {
|
|
117
|
-
const onValueChange = vi.fn();
|
|
118
|
-
const { rerender } = render(
|
|
119
|
-
<div>
|
|
120
|
-
<PortalHost />
|
|
121
|
-
<Combobox.Root
|
|
122
|
-
value={{ value: 'banana', label: 'Banana' }}
|
|
123
|
-
onValueChange={onValueChange}
|
|
124
|
-
defaultOpen
|
|
125
|
-
>
|
|
126
|
-
<Combobox.Input testID="input" placeholder="Search..." />
|
|
127
|
-
<Combobox.Portal>
|
|
128
|
-
<Combobox.Content testID="content">
|
|
129
|
-
<Combobox.Item value="apple" label="Apple" testID="item-apple">
|
|
130
|
-
<Combobox.ItemText />
|
|
131
|
-
</Combobox.Item>
|
|
132
|
-
<Combobox.Item value="banana" label="Banana" testID="item-banana">
|
|
133
|
-
<Combobox.ItemText />
|
|
134
|
-
</Combobox.Item>
|
|
135
|
-
</Combobox.Content>
|
|
136
|
-
</Combobox.Portal>
|
|
137
|
-
</Combobox.Root>
|
|
138
|
-
</div>
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
expect(screen.getByTestId('item-banana').getAttribute('aria-selected')).toBe('true');
|
|
142
|
-
|
|
143
|
-
fireEvent.click(screen.getByTestId('item-apple'));
|
|
144
|
-
expect(onValueChange).toHaveBeenCalledWith(
|
|
145
|
-
expect.objectContaining({ value: 'apple', label: 'Apple' })
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
// Re-render with new controlled value
|
|
149
|
-
rerender(
|
|
150
|
-
<div>
|
|
151
|
-
<PortalHost />
|
|
152
|
-
<Combobox.Root
|
|
153
|
-
value={{ value: 'apple', label: 'Apple' }}
|
|
154
|
-
onValueChange={onValueChange}
|
|
155
|
-
open
|
|
156
|
-
>
|
|
157
|
-
<Combobox.Input testID="input" placeholder="Search..." />
|
|
158
|
-
<Combobox.Portal>
|
|
159
|
-
<Combobox.Content testID="content">
|
|
160
|
-
<Combobox.Item value="apple" label="Apple" testID="item-apple">
|
|
161
|
-
<Combobox.ItemText />
|
|
162
|
-
</Combobox.Item>
|
|
163
|
-
<Combobox.Item value="banana" label="Banana" testID="item-banana">
|
|
164
|
-
<Combobox.ItemText />
|
|
165
|
-
</Combobox.Item>
|
|
166
|
-
</Combobox.Content>
|
|
167
|
-
</Combobox.Portal>
|
|
168
|
-
</Combobox.Root>
|
|
169
|
-
</div>
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
expect(screen.getByTestId('item-apple').getAttribute('aria-selected')).toBe('true');
|
|
173
|
-
expect(screen.getByTestId('item-banana').getAttribute('aria-selected')).toBe('false');
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
// -------------------------------------------------------------------------
|
|
177
|
-
// Controlled open
|
|
178
|
-
// -------------------------------------------------------------------------
|
|
179
|
-
|
|
180
|
-
it('controlled open mode', () => {
|
|
181
|
-
const onOpenChange = vi.fn();
|
|
182
|
-
renderCombobox({ open: true, onOpenChange });
|
|
183
|
-
|
|
184
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
185
|
-
|
|
186
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
187
|
-
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// -------------------------------------------------------------------------
|
|
191
|
-
// Search value
|
|
192
|
-
// -------------------------------------------------------------------------
|
|
193
|
-
|
|
194
|
-
it('search value updates on input change', () => {
|
|
195
|
-
const onSearchValueChange = vi.fn();
|
|
196
|
-
renderCombobox({ onSearchValueChange });
|
|
197
|
-
|
|
198
|
-
const input = screen.getByTestId('input');
|
|
199
|
-
fireEvent.change(input, { target: { value: 'app' } });
|
|
200
|
-
|
|
201
|
-
expect(onSearchValueChange).toHaveBeenCalledWith('app');
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// -------------------------------------------------------------------------
|
|
205
|
-
// Trigger toggles dropdown
|
|
206
|
-
// -------------------------------------------------------------------------
|
|
207
|
-
|
|
208
|
-
it('trigger toggles dropdown open and closed', () => {
|
|
209
|
-
renderCombobox();
|
|
210
|
-
|
|
211
|
-
// Initially closed
|
|
212
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
213
|
-
|
|
214
|
-
// Open
|
|
215
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
216
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
217
|
-
|
|
218
|
-
// Close
|
|
219
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
220
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// -------------------------------------------------------------------------
|
|
224
|
-
// Input opens dropdown on focus
|
|
225
|
-
// -------------------------------------------------------------------------
|
|
226
|
-
|
|
227
|
-
it('opens dropdown on input focus', () => {
|
|
228
|
-
renderCombobox();
|
|
229
|
-
|
|
230
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
231
|
-
|
|
232
|
-
fireEvent.focus(screen.getByTestId('input'));
|
|
233
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// -------------------------------------------------------------------------
|
|
237
|
-
// Input opens dropdown on typing
|
|
238
|
-
// -------------------------------------------------------------------------
|
|
239
|
-
|
|
240
|
-
it('opens dropdown when user types in input', () => {
|
|
241
|
-
renderCombobox();
|
|
242
|
-
|
|
243
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
244
|
-
|
|
245
|
-
fireEvent.change(screen.getByTestId('input'), { target: { value: 'b' } });
|
|
246
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
// -------------------------------------------------------------------------
|
|
250
|
-
// Empty component renders
|
|
251
|
-
// -------------------------------------------------------------------------
|
|
252
|
-
|
|
253
|
-
it('renders empty component when no items', () => {
|
|
254
|
-
renderCombobox({ defaultOpen: true }, []);
|
|
255
|
-
expect(screen.getByTestId('empty')).toBeTruthy();
|
|
256
|
-
expect(screen.getByText('No results')).toBeTruthy();
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
// -------------------------------------------------------------------------
|
|
260
|
-
// asChild
|
|
261
|
-
// -------------------------------------------------------------------------
|
|
262
|
-
|
|
263
|
-
it('asChild works on trigger', () => {
|
|
264
|
-
render(
|
|
265
|
-
<div>
|
|
266
|
-
<PortalHost />
|
|
267
|
-
<Combobox.Root>
|
|
268
|
-
<Combobox.Input testID="input" placeholder="Search..." />
|
|
269
|
-
<Combobox.Trigger asChild>
|
|
270
|
-
<Pressable testID="custom-trigger">
|
|
271
|
-
<View />
|
|
272
|
-
</Pressable>
|
|
273
|
-
</Combobox.Trigger>
|
|
274
|
-
<Combobox.Portal>
|
|
275
|
-
<Combobox.Content testID="content">
|
|
276
|
-
<Combobox.Item value="apple" label="Apple" testID="item-apple">
|
|
277
|
-
<Combobox.ItemText />
|
|
278
|
-
</Combobox.Item>
|
|
279
|
-
</Combobox.Content>
|
|
280
|
-
</Combobox.Portal>
|
|
281
|
-
</Combobox.Root>
|
|
282
|
-
</div>
|
|
283
|
-
);
|
|
284
|
-
|
|
285
|
-
const trigger = screen.getByTestId('custom-trigger');
|
|
286
|
-
// Slot merges role onto the child element
|
|
287
|
-
expect(trigger.getAttribute('role')).toBe('button');
|
|
288
|
-
|
|
289
|
-
fireEvent.click(trigger);
|
|
290
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
291
|
-
});
|
|
292
|
-
});
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { render, screen, fireEvent } from '@testing-library/react';
|
|
2
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
3
|
-
import * as ContextMenu from '../context-menu';
|
|
4
|
-
import { PortalHost } from '../portal';
|
|
5
|
-
|
|
6
|
-
describe('ContextMenu', () => {
|
|
7
|
-
const renderMenu = (props = {}) =>
|
|
8
|
-
render(
|
|
9
|
-
<div>
|
|
10
|
-
<PortalHost />
|
|
11
|
-
<ContextMenu.Root {...props}>
|
|
12
|
-
<ContextMenu.Trigger testID="trigger">Menu</ContextMenu.Trigger>
|
|
13
|
-
<ContextMenu.Portal>
|
|
14
|
-
<ContextMenu.Content testID="content">
|
|
15
|
-
<ContextMenu.Item testID="item-1">Item 1</ContextMenu.Item>
|
|
16
|
-
<ContextMenu.Separator testID="sep" />
|
|
17
|
-
<ContextMenu.Item testID="item-2">Item 2</ContextMenu.Item>
|
|
18
|
-
</ContextMenu.Content>
|
|
19
|
-
</ContextMenu.Portal>
|
|
20
|
-
</ContextMenu.Root>
|
|
21
|
-
</div>
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
it('is closed by default', () => {
|
|
25
|
-
renderMenu();
|
|
26
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('renders content when defaultOpen is true', () => {
|
|
30
|
-
renderMenu({ defaultOpen: true });
|
|
31
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('content has menu role', () => {
|
|
35
|
-
renderMenu({ defaultOpen: true });
|
|
36
|
-
expect(screen.getByTestId('content').getAttribute('role')).toBe('menu');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('items have menuitem role', () => {
|
|
40
|
-
renderMenu({ defaultOpen: true });
|
|
41
|
-
expect(screen.getByTestId('item-1').getAttribute('role')).toBe('menuitem');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('separator has separator role', () => {
|
|
45
|
-
renderMenu({ defaultOpen: true });
|
|
46
|
-
expect(screen.getByTestId('sep').getAttribute('role')).toBe('separator');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('trigger wires onLongPress handler', () => {
|
|
50
|
-
renderMenu();
|
|
51
|
-
const trigger = screen.getByTestId('trigger');
|
|
52
|
-
// In the DOM env, react-native-web maps onLongPress to a synthetic handler.
|
|
53
|
-
// We verify the trigger does NOT respond to a regular click (unlike DropdownMenu).
|
|
54
|
-
fireEvent.click(trigger);
|
|
55
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('fires item onPress and closes menu', () => {
|
|
59
|
-
const onPress = vi.fn();
|
|
60
|
-
render(
|
|
61
|
-
<div>
|
|
62
|
-
<PortalHost />
|
|
63
|
-
<ContextMenu.Root defaultOpen>
|
|
64
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
65
|
-
<ContextMenu.Portal>
|
|
66
|
-
<ContextMenu.Content>
|
|
67
|
-
<ContextMenu.Item testID="pressable-item" onPress={onPress}>
|
|
68
|
-
Click me
|
|
69
|
-
</ContextMenu.Item>
|
|
70
|
-
</ContextMenu.Content>
|
|
71
|
-
</ContextMenu.Portal>
|
|
72
|
-
</ContextMenu.Root>
|
|
73
|
-
</div>
|
|
74
|
-
);
|
|
75
|
-
fireEvent.click(screen.getByTestId('pressable-item'));
|
|
76
|
-
expect(onPress).toHaveBeenCalled();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('checkbox item toggles checked state', () => {
|
|
80
|
-
const onCheckedChange = vi.fn();
|
|
81
|
-
render(
|
|
82
|
-
<div>
|
|
83
|
-
<PortalHost />
|
|
84
|
-
<ContextMenu.Root defaultOpen>
|
|
85
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
86
|
-
<ContextMenu.Portal>
|
|
87
|
-
<ContextMenu.Content>
|
|
88
|
-
<ContextMenu.CheckboxItem
|
|
89
|
-
testID="checkbox"
|
|
90
|
-
checked={false}
|
|
91
|
-
onCheckedChange={onCheckedChange}
|
|
92
|
-
closeOnPress={false}
|
|
93
|
-
>
|
|
94
|
-
Toggle
|
|
95
|
-
</ContextMenu.CheckboxItem>
|
|
96
|
-
</ContextMenu.Content>
|
|
97
|
-
</ContextMenu.Portal>
|
|
98
|
-
</ContextMenu.Root>
|
|
99
|
-
</div>
|
|
100
|
-
);
|
|
101
|
-
fireEvent.click(screen.getByTestId('checkbox'));
|
|
102
|
-
expect(onCheckedChange).toHaveBeenCalledWith(true);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('radio items update group value', () => {
|
|
106
|
-
const onValueChange = vi.fn();
|
|
107
|
-
render(
|
|
108
|
-
<div>
|
|
109
|
-
<PortalHost />
|
|
110
|
-
<ContextMenu.Root defaultOpen>
|
|
111
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
112
|
-
<ContextMenu.Portal>
|
|
113
|
-
<ContextMenu.Content>
|
|
114
|
-
<ContextMenu.RadioGroup value="a" onValueChange={onValueChange}>
|
|
115
|
-
<ContextMenu.RadioItem testID="radio-b" value="b" closeOnPress={false}>
|
|
116
|
-
B
|
|
117
|
-
</ContextMenu.RadioItem>
|
|
118
|
-
</ContextMenu.RadioGroup>
|
|
119
|
-
</ContextMenu.Content>
|
|
120
|
-
</ContextMenu.Portal>
|
|
121
|
-
</ContextMenu.Root>
|
|
122
|
-
</div>
|
|
123
|
-
);
|
|
124
|
-
fireEvent.click(screen.getByTestId('radio-b'));
|
|
125
|
-
expect(onValueChange).toHaveBeenCalledWith('b');
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('item indicator renders when checkbox is checked', () => {
|
|
129
|
-
render(
|
|
130
|
-
<div>
|
|
131
|
-
<PortalHost />
|
|
132
|
-
<ContextMenu.Root defaultOpen>
|
|
133
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
134
|
-
<ContextMenu.Portal>
|
|
135
|
-
<ContextMenu.Content>
|
|
136
|
-
<ContextMenu.CheckboxItem
|
|
137
|
-
checked={true}
|
|
138
|
-
onCheckedChange={() => {}}
|
|
139
|
-
closeOnPress={false}
|
|
140
|
-
>
|
|
141
|
-
<ContextMenu.ItemIndicator testID="indicator">check</ContextMenu.ItemIndicator>
|
|
142
|
-
Checked
|
|
143
|
-
</ContextMenu.CheckboxItem>
|
|
144
|
-
</ContextMenu.Content>
|
|
145
|
-
</ContextMenu.Portal>
|
|
146
|
-
</ContextMenu.Root>
|
|
147
|
-
</div>
|
|
148
|
-
);
|
|
149
|
-
expect(screen.getByTestId('indicator')).toBeTruthy();
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('item indicator hidden when checkbox is unchecked', () => {
|
|
153
|
-
render(
|
|
154
|
-
<div>
|
|
155
|
-
<PortalHost />
|
|
156
|
-
<ContextMenu.Root defaultOpen>
|
|
157
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
158
|
-
<ContextMenu.Portal>
|
|
159
|
-
<ContextMenu.Content>
|
|
160
|
-
<ContextMenu.CheckboxItem
|
|
161
|
-
checked={false}
|
|
162
|
-
onCheckedChange={() => {}}
|
|
163
|
-
closeOnPress={false}
|
|
164
|
-
>
|
|
165
|
-
<ContextMenu.ItemIndicator testID="indicator">check</ContextMenu.ItemIndicator>
|
|
166
|
-
Unchecked
|
|
167
|
-
</ContextMenu.CheckboxItem>
|
|
168
|
-
</ContextMenu.Content>
|
|
169
|
-
</ContextMenu.Portal>
|
|
170
|
-
</ContextMenu.Root>
|
|
171
|
-
</div>
|
|
172
|
-
);
|
|
173
|
-
expect(screen.queryByTestId('indicator')).toBeNull();
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('controlled mode respects open prop', () => {
|
|
177
|
-
const { rerender } = render(
|
|
178
|
-
<div>
|
|
179
|
-
<PortalHost />
|
|
180
|
-
<ContextMenu.Root open={false}>
|
|
181
|
-
<ContextMenu.Trigger testID="trigger">M</ContextMenu.Trigger>
|
|
182
|
-
<ContextMenu.Portal>
|
|
183
|
-
<ContextMenu.Content testID="content">
|
|
184
|
-
<ContextMenu.Item>Item</ContextMenu.Item>
|
|
185
|
-
</ContextMenu.Content>
|
|
186
|
-
</ContextMenu.Portal>
|
|
187
|
-
</ContextMenu.Root>
|
|
188
|
-
</div>
|
|
189
|
-
);
|
|
190
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
191
|
-
|
|
192
|
-
rerender(
|
|
193
|
-
<div>
|
|
194
|
-
<PortalHost />
|
|
195
|
-
<ContextMenu.Root open={true}>
|
|
196
|
-
<ContextMenu.Trigger testID="trigger">M</ContextMenu.Trigger>
|
|
197
|
-
<ContextMenu.Portal>
|
|
198
|
-
<ContextMenu.Content testID="content">
|
|
199
|
-
<ContextMenu.Item>Item</ContextMenu.Item>
|
|
200
|
-
</ContextMenu.Content>
|
|
201
|
-
</ContextMenu.Portal>
|
|
202
|
-
</ContextMenu.Root>
|
|
203
|
-
</div>
|
|
204
|
-
);
|
|
205
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('group has group role', () => {
|
|
209
|
-
render(
|
|
210
|
-
<div>
|
|
211
|
-
<PortalHost />
|
|
212
|
-
<ContextMenu.Root defaultOpen>
|
|
213
|
-
<ContextMenu.Trigger>M</ContextMenu.Trigger>
|
|
214
|
-
<ContextMenu.Portal>
|
|
215
|
-
<ContextMenu.Content>
|
|
216
|
-
<ContextMenu.Group testID="group">
|
|
217
|
-
<ContextMenu.Item>Item</ContextMenu.Item>
|
|
218
|
-
</ContextMenu.Group>
|
|
219
|
-
</ContextMenu.Content>
|
|
220
|
-
</ContextMenu.Portal>
|
|
221
|
-
</ContextMenu.Root>
|
|
222
|
-
</div>
|
|
223
|
-
);
|
|
224
|
-
expect(screen.getByTestId('group').getAttribute('role')).toBe('group');
|
|
225
|
-
});
|
|
226
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { render, screen, fireEvent } from '@testing-library/react';
|
|
2
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
3
|
-
import * as Dialog from '../dialog';
|
|
4
|
-
import { PortalHost } from '../portal';
|
|
5
|
-
|
|
6
|
-
describe('Dialog', () => {
|
|
7
|
-
const renderDialog = (props = {}) =>
|
|
8
|
-
render(
|
|
9
|
-
<div>
|
|
10
|
-
<PortalHost />
|
|
11
|
-
<Dialog.Root {...props}>
|
|
12
|
-
<Dialog.Trigger testID="trigger">Open</Dialog.Trigger>
|
|
13
|
-
<Dialog.Portal>
|
|
14
|
-
<Dialog.Overlay testID="overlay" />
|
|
15
|
-
<Dialog.Content testID="content">
|
|
16
|
-
<Dialog.Title testID="title">Dialog Title</Dialog.Title>
|
|
17
|
-
<Dialog.Description testID="description">
|
|
18
|
-
Dialog description
|
|
19
|
-
</Dialog.Description>
|
|
20
|
-
<Dialog.Close testID="close">Close</Dialog.Close>
|
|
21
|
-
</Dialog.Content>
|
|
22
|
-
</Dialog.Portal>
|
|
23
|
-
</Dialog.Root>
|
|
24
|
-
</div>
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
it('is closed by default', () => {
|
|
28
|
-
renderDialog();
|
|
29
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('opens when trigger is clicked', () => {
|
|
33
|
-
renderDialog();
|
|
34
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
35
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('closes when close is clicked', () => {
|
|
39
|
-
renderDialog({ defaultOpen: true });
|
|
40
|
-
expect(screen.getByTestId('content')).toBeTruthy();
|
|
41
|
-
fireEvent.click(screen.getByTestId('close'));
|
|
42
|
-
expect(screen.queryByTestId('content')).toBeNull();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('supports controlled open state', () => {
|
|
46
|
-
const onOpenChange = vi.fn();
|
|
47
|
-
renderDialog({ open: false, onOpenChange });
|
|
48
|
-
fireEvent.click(screen.getByTestId('trigger'));
|
|
49
|
-
expect(onOpenChange).toHaveBeenCalledWith(true);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('content has dialog role', () => {
|
|
53
|
-
renderDialog({ defaultOpen: true });
|
|
54
|
-
expect(screen.getByTestId('content').getAttribute('role')).toBe('dialog');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('title has heading role', () => {
|
|
58
|
-
renderDialog({ defaultOpen: true });
|
|
59
|
-
expect(screen.getByTestId('title').getAttribute('role')).toBe('heading');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('trigger has button role', () => {
|
|
63
|
-
renderDialog();
|
|
64
|
-
expect(screen.getByTestId('trigger').getAttribute('role')).toBe('button');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('links title and description via nativeID/id', () => {
|
|
68
|
-
renderDialog({ defaultOpen: true });
|
|
69
|
-
const content = screen.getByTestId('content');
|
|
70
|
-
const title = screen.getByTestId('title');
|
|
71
|
-
const description = screen.getByTestId('description');
|
|
72
|
-
|
|
73
|
-
const labelledBy = content.getAttribute('aria-labelledby');
|
|
74
|
-
const describedBy = content.getAttribute('aria-describedby');
|
|
75
|
-
|
|
76
|
-
expect(labelledBy).toBeTruthy();
|
|
77
|
-
expect(describedBy).toBeTruthy();
|
|
78
|
-
expect(title.getAttribute('id') || title.id).toBe(labelledBy);
|
|
79
|
-
expect(description.getAttribute('id') || description.id).toBe(describedBy);
|
|
80
|
-
});
|
|
81
|
-
});
|