-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathControlDropdown.test.tsx
More file actions
219 lines (178 loc) · 6.65 KB
/
Copy pathControlDropdown.test.tsx
File metadata and controls
219 lines (178 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* eslint-disable testing-library/no-node-access */
import { screen } from '@testing-library/react';
import { vi } from 'vitest';
import { setup } from '../../utils/tests/setup';
import ControlDropdown from './ControlDropdown';
describe('ControlDropdown', () => {
const defaultProps = {
title: 'Test Dropdown',
parentRef: { current: document.createElement('div') },
isOpen: true,
onClose: vi.fn(),
onSubmit: vi.fn(),
};
it('should render when open', () => {
setup(<ControlDropdown {...defaultProps}>Content</ControlDropdown>);
expect(screen.getByText('Test Dropdown')).toBeInTheDocument();
expect(screen.getByText('Content')).toBeInTheDocument();
});
it('should not render when closed', () => {
setup(
<ControlDropdown {...defaultProps} isOpen={false}>
Content
</ControlDropdown>,
);
expect(screen.queryByText('Test Dropdown')).not.toBeInTheDocument();
expect(screen.queryByText('Content')).not.toBeInTheDocument();
});
it('should not render when parentRef is null', () => {
setup(
<ControlDropdown {...defaultProps} parentRef={{ current: null }}>
Content
</ControlDropdown>,
);
expect(screen.queryByText('Test Dropdown')).not.toBeInTheDocument();
expect(screen.queryByText('Content')).not.toBeInTheDocument();
});
it('should trigger onClose when pressing ESC key', async () => {
const onCloseMock = vi.fn();
const { user } = setup(
<ControlDropdown {...defaultProps} onClose={onCloseMock}>
Content
</ControlDropdown>,
);
await user.keyboard('{Escape}');
expect(onCloseMock).toHaveBeenCalled();
});
it('should trigger onClose when pressing ESC key while focused on content inside dropdown', async () => {
const onCloseMock = vi.fn();
const { user } = setup(
<ControlDropdown {...defaultProps} onClose={onCloseMock}>
<button type="button" data-testid="internal-button">
Click me
</button>
</ControlDropdown>,
);
const internalButton = screen.getByTestId('internal-button');
// eslint-disable-next-line testing-library/no-node-access
await user.click(internalButton);
await user.keyboard('{Escape}');
expect(onCloseMock).toHaveBeenCalled();
});
it('should call onSubmit when Apply button is clicked', async () => {
const onSubmitMock = vi.fn();
const { user } = setup(
<ControlDropdown {...defaultProps} onSubmit={onSubmitMock}>
Content
</ControlDropdown>,
);
const applyButton = screen.getByRole('button', { name: /apply/i });
// eslint-disable-next-line testing-library/no-node-access
await user.click(applyButton);
expect(onSubmitMock).toHaveBeenCalled();
});
it('should call onClose when Close button is clicked', async () => {
const onCloseMock = vi.fn();
const { user } = setup(
<ControlDropdown {...defaultProps} onClose={onCloseMock}>
Content
</ControlDropdown>,
);
// Get all close buttons and click the one in the footer (not the header)
const closeButtons = screen.getAllByRole('button', { name: /close/i });
// eslint-disable-next-line testing-library/no-node-access
const footerCloseButton = closeButtons[1]; // The second one is in the footer
// eslint-disable-next-line testing-library/no-node-access
await user.click(footerCloseButton);
expect(onCloseMock).toHaveBeenCalled();
});
it('should call onReset when Reset button is clicked', async () => {
const onResetMock = vi.fn();
const { user } = setup(
<ControlDropdown {...defaultProps} onReset={onResetMock}>
Content
</ControlDropdown>,
);
const resetButton = screen.getByRole('button', {
name: /reset to defaults/i,
});
// eslint-disable-next-line testing-library/no-node-access
await user.click(resetButton);
expect(onResetMock).toHaveBeenCalled();
});
it('should not show Reset button when onReset is not provided', () => {
setup(<ControlDropdown {...defaultProps}>Content</ControlDropdown>);
expect(
screen.queryByRole('button', { name: /reset to defaults/i }),
).not.toBeInTheDocument();
});
it('should use custom labels when provided', () => {
setup(
<ControlDropdown
{...defaultProps}
submitLabel="Custom Submit"
closeLabel="Custom Close"
resetLabel="Custom Reset"
onReset={vi.fn()}
>
Content
</ControlDropdown>,
);
expect(
screen.getByRole('button', { name: /custom submit/i }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: /custom close/i }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: /custom reset/i }),
).toBeInTheDocument();
});
it('should trap focus and cycle correctly with Shift+Tab navigation', async () => {
const { user } = setup(
<ControlDropdown {...defaultProps}>
<button type="button" data-testid="content-button">
Content Button
</button>
</ControlDropdown>,
);
// Wait for the dropdown to be fully rendered
await new Promise<void>((resolve) => {
setTimeout(resolve, 50);
});
// Get all focusable elements in the expected tab order
const closeButton = screen.getByRole('button', { name: /close dropdown/i });
const contentButton = screen.getByTestId('content-button');
const closeButtons = screen.getAllByRole('button', {
name: /close/i,
});
// eslint-disable-next-line testing-library/no-node-access
const closeFooterButton = closeButtons[1]; // Footer close button
const applyButton = screen.getByRole('button', { name: /apply/i });
// Focus the first element to start the test
// eslint-disable-next-line testing-library/no-node-access
await user.click(closeButton);
// Test forward tab navigation
await user.tab();
expect(contentButton).toHaveFocus();
await user.tab();
expect(closeFooterButton).toHaveFocus();
await user.tab();
expect(applyButton).toHaveFocus();
// Test that focus cycles back to first element when tabbing past the last
await user.tab();
expect(closeButton).toHaveFocus();
// Test reverse tab navigation (Shift+Tab)
await user.tab({ shift: true });
expect(applyButton).toHaveFocus();
await user.tab({ shift: true });
expect(closeFooterButton).toHaveFocus();
await user.tab({ shift: true });
expect(contentButton).toHaveFocus();
await user.tab({ shift: true });
expect(closeButton).toHaveFocus();
// Test that focus cycles to last element when Shift+Tabbing past the first
await user.tab({ shift: true });
expect(applyButton).toHaveFocus();
});
});