-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFormField.test.js
More file actions
413 lines (337 loc) · 16.6 KB
/
FormField.test.js
File metadata and controls
413 lines (337 loc) · 16.6 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import React from 'react';
import { FormField, Suggestion } from './index.js';
describe('ui', () => {
describe('FormField', () => {
it('renders a TextInput when no options and checked are provided', () => {
render(<FormField name="test-input" label="Test Label" />);
expect(screen.getByLabelText('Test Label')).toBeInTheDocument();
expect(screen.getByRole('textbox')).toBeInTheDocument();
});
it('renders a Select when options are provided', () => {
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
];
render(<FormField name="test-select" label="Test Label" options={options} />);
const select = screen.getByRole('combobox');
expect(screen.getByLabelText('Test Label')).toBeInTheDocument();
expect(select).toBeInTheDocument();
expect(select).toHaveAttribute('id', 'test-select');
options.forEach((option) => {
const optionElement = screen.getByText(option.label);
expect(optionElement).toBeInTheDocument();
expect(optionElement).toHaveAttribute('value', option.value);
});
});
it('renders a Checkbox when checked is provided', () => {
render(<FormField name="test-checkbox" label="Test Label" checked={true} />);
const checkbox = screen.getByRole('checkbox');
expect(screen.getByLabelText('Test Label')).toBeInTheDocument();
expect(checkbox).toBeInTheDocument();
expect(checkbox).toBeChecked();
expect(checkbox).toHaveAttribute('id', 'test-checkbox');
});
it('renders caption when provided', () => {
render(<FormField name="test-caption" label="Test Caption Label" caption="Test Caption" />);
expect(screen.getByLabelText('Test Caption Label')).toBeInTheDocument();
expect(screen.getByText('Test Caption')).toBeInTheDocument();
});
it('renders error when provided', () => {
render(<FormField name="test-error" label="Test Error Label" error="Test Error" />);
expect(screen.getByLabelText('Test Error Label')).toBeInTheDocument();
expect(screen.getByText('Test Error')).toBeInTheDocument();
});
it('renders nothing when hidden true is provided', () => {
const { container } = render(<FormField name="test-hidden" label="Test Hidden Label" hidden />);
expect(container).toBeEmptyDOMElement();
});
describe('PasswordInput', () => {
const passwordTestProps = {
name: 'test-password',
label: 'Test Label',
type: 'password',
};
it('renders a password input with the correct type', () => {
render(<FormField {...passwordTestProps} />);
const input = screen.getByLabelText('Test Label');
expect(input).toHaveAttribute('type', 'password');
});
it('toggles password visibility when the visibility button is clicked', async () => {
const user = userEvent.setup();
render(<FormField {...passwordTestProps} label="Password" />);
const button = screen.getByLabelText('Visualizar Password');
const passwordInput = screen.getByLabelText('Password');
expect(passwordInput.type).toBe('password');
await user.type(passwordInput, 'password123');
fireEvent.click(button);
await waitFor(() => {
expect(passwordInput).toHaveFocus();
});
expect(passwordInput.selectionStart).toBe(passwordInput.value.length);
expect(passwordInput.selectionEnd).toBe(passwordInput.value.length);
expect(passwordInput.type).toBe('text');
fireEvent.click(button);
await waitFor(() => {
expect(passwordInput).toHaveFocus();
});
expect(passwordInput.type).toBe('password');
expect(passwordInput.selectionStart).toBe(passwordInput.value.length);
expect(passwordInput.selectionEnd).toBe(passwordInput.value.length);
});
it('displays a caps lock warning message when caps lock is on', async () => {
const user = userEvent.setup();
render(<FormField {...passwordTestProps} />);
const passwordInput = screen.getByLabelText('Test Label');
expect(screen.queryByText('Caps Lock está ativado.')).not.toBeInTheDocument();
await user.type(passwordInput, '{capslock} now is on');
expect(screen.queryByText('Caps Lock está ativado.')).toBeInTheDocument();
});
it('hides the caps lock warning message when caps lock is off', async () => {
const user = userEvent.setup();
render(<FormField {...passwordTestProps} />);
const passwordInput = screen.getByLabelText('Test Label');
await user.type(passwordInput, '{capslock} now is on');
const capsLockMessage = screen.getByText('Caps Lock está ativado.');
expect(capsLockMessage).toBeInTheDocument();
await user.type(passwordInput, '{capslock} now is off');
expect(capsLockMessage).not.toBeInTheDocument();
});
it('calls onKeyDown prop when a key is pressed', async () => {
const user = userEvent.setup();
const onKeyDown = vi.fn();
render(<FormField {...passwordTestProps} onKeyDown={onKeyDown} />);
const passwordInput = screen.getByLabelText('Test Label');
user.type(passwordInput, 'a');
await waitFor(() => {
expect(onKeyDown).toHaveBeenCalled();
});
});
it('calls onKeyUp prop when a key is pressed', async () => {
const user = userEvent.setup();
const onKeyUp = vi.fn();
render(<FormField {...passwordTestProps} onKeyUp={onKeyUp} />);
const passwordInput = screen.getByLabelText('Test Label');
user.type(passwordInput, 'a');
await waitFor(() => {
expect(onKeyUp).toHaveBeenCalled();
});
});
it('calls onBlur prop when the input loses focus', () => {
const onBlur = vi.fn();
render(<FormField {...passwordTestProps} onBlur={onBlur} />);
const input = screen.getByLabelText('Test Label');
fireEvent.blur(input);
expect(onBlur).toHaveBeenCalled();
});
it('forwards ref to the input', () => {
const inputRef = React.createRef();
render(<FormField {...passwordTestProps} ref={inputRef} />);
expect(inputRef.current).not.toBeNull();
const passwordInput = screen.getByLabelText('Test Label');
expect(passwordInput).not.toHaveFocus();
inputRef.current.focus();
expect(passwordInput).toHaveFocus();
});
});
describe('validationStatus', () => {
describe('TextInput', () => {
const errorStyle = '.ijuZEL';
const successStyle = '.kLIpNb';
it('should set validationStatus to "error" when error prop is provided', () => {
render(<FormField name="test" error="This is an error" />);
const validationMessage = screen.getByText('This is an error');
const inputElement = screen.getByRole('textbox');
expect(validationMessage).toBeInTheDocument();
expect(inputElement).toHaveAttribute('aria-invalid', 'true');
expect(inputElement.parentElement).toHaveAttribute('data-validation', 'error');
});
it('should set validationStatus to "success" when isValid prop is true and no error is provided', () => {
render(<FormField name="test" isValid />);
const inputElement = screen.getByRole('textbox');
expect(inputElement).not.toHaveAttribute('aria-invalid', 'true');
expect(inputElement.parentElement).toHaveAttribute('data-validation', 'success');
});
it('should not set validationStatus when neither error nor isValid props are provided', () => {
const { container } = render(<FormField name="test" />);
const inputElement = screen.getByRole('textbox');
expect(container.querySelector(errorStyle)).not.toBeInTheDocument();
expect(container.querySelector(successStyle)).not.toBeInTheDocument();
expect(inputElement).not.toHaveAttribute('aria-invalid', 'true');
});
it('forwards ref to the input', () => {
const inputRef = React.createRef();
render(<FormField name="test" ref={inputRef} />);
expect(inputRef.current).not.toBeNull();
const inputElement = screen.getByRole('textbox');
expect(inputElement).not.toHaveFocus();
inputRef.current.focus();
expect(inputElement).toHaveFocus();
});
});
describe('Select', () => {
it('should set validationStatus to "error" when error prop is provided', () => {
render([
<FormField
key="1"
name="test"
error="Because of the error, isValid is ignored"
isValid={true}
options={[{ value: '1', label: 'Option 1' }]}
/>,
<FormField
key="2"
name="test"
error="This is an error"
isValid={false}
options={[{ value: '1', label: 'Option 1' }]}
/>,
]);
const selectElements = screen.getAllByRole('combobox');
selectElements.forEach((selectElement) => {
expect(selectElement).toHaveAttribute('aria-invalid', 'true');
expect(selectElement.parentElement).toHaveAttribute('data-validation', 'error');
});
});
it('should set validationStatus to "success" when isValid prop is true and no error is provided', () => {
render(<FormField name="test" isValid options={[{ value: '1', label: 'Option 1' }]} />);
const selectElement = screen.getByRole('combobox');
expect(selectElement).toHaveAttribute('aria-invalid', 'false');
expect(selectElement.parentElement).toHaveAttribute('data-validation', 'success');
});
it('should not set validationStatus when neither error nor isValid props are provided', () => {
render(<FormField name="test" options={[{ value: '1', label: 'Option 1' }]} />);
const selectElement = screen.getByRole('combobox');
expect(selectElement).toHaveAttribute('aria-invalid', 'false');
expect(selectElement.parentElement).not.toHaveAttribute('data-validation');
});
it('forwards ref to the input', () => {
const inputRef = React.createRef();
render(<FormField name="test" options={[{ value: '1', label: 'Option 1' }]} ref={inputRef} />);
expect(inputRef.current).not.toBeNull();
const selectElement = screen.getByRole('combobox');
expect(selectElement).not.toHaveFocus();
inputRef.current.focus();
expect(selectElement).toHaveFocus();
});
});
describe('Checkbox', () => {
it('should set "aria-invalid" to "true" when error prop is provided', () => {
render(<FormField name="test" error="This is an error" checked />);
const checkbox = screen.getByRole('checkbox');
expect(checkbox).toHaveAttribute('aria-invalid', 'true');
});
it('should not set "aria-invalid" to "false" when error prop is not provided', () => {
render(<FormField name="test" checked />);
const checkbox = screen.getByRole('checkbox');
expect(checkbox).toHaveAttribute('aria-invalid', 'false');
});
it('forwards ref to the input', () => {
const inputRef = React.createRef();
render(<FormField name="test" checked ref={inputRef} />);
expect(inputRef.current).not.toBeNull();
const checkbox = screen.getByRole('checkbox');
expect(checkbox).not.toHaveFocus();
inputRef.current.focus();
expect(checkbox).toHaveFocus();
});
});
describe('Suggestion', () => {
it('renders nothing when suggestion is not provided', () => {
const { container } = render(<Suggestion />);
expect(container).toBeEmptyDOMElement();
});
it('renders nothing when suggestion value is not provided', () => {
const { container } = render(<Suggestion suggestion={{}} />);
expect(container).toBeEmptyDOMElement();
});
it('renders nothing when suggestion is null', () => {
const { container } = render(<Suggestion suggestion={null} />);
expect(container).toBeEmptyDOMElement();
});
it('renders suggestion with default texts', () => {
render(<Suggestion suggestion={{ value: 'test', onClick: vi.fn(), ignoreClick: vi.fn() }} />);
expect(screen.getByText('Você quis dizer')).toBeInTheDocument();
expect(screen.getByText('?')).toBeInTheDocument();
expect(screen.getByText('Aceitar sugestão')).toBeInTheDocument();
expect(screen.getByText('Ignorar')).toBeInTheDocument();
expect(screen.getByText('Ignorar sugestão')).toBeInTheDocument();
});
it('renders suggestion with custom label', () => {
render(
<Suggestion suggestion={{ value: 'test', label: 'Did you mean:', labelEnd: '?!?', onClick: vi.fn() }} />,
);
expect(screen.getByText('Did you mean:')).toBeInTheDocument();
expect(screen.getByText('?!?')).toBeInTheDocument();
});
it('renders suggestion button with correct text', () => {
render(<Suggestion suggestion={{ value: 'test', pre: 'pre', mid: 'mid', post: 'post', onClick: vi.fn() }} />);
expect(screen.getByText('pre')).toBeInTheDocument();
expect(screen.getByText('mid')).toBeInTheDocument();
expect(screen.getByText('post')).toBeInTheDocument();
});
it('renders ignore button when ignore option is present', () => {
const suggestion = {
value: true,
ignoreTooltip: 'Ignore suggestion',
ignoreClick: vi.fn(),
ignoreLabel: 'Ignore',
};
render(<Suggestion suggestion={suggestion} />);
expect(screen.getByText('Ignore')).toBeInTheDocument();
expect(screen.getByText('Ignore suggestion')).toBeInTheDocument();
});
it('renders suggestion with all options', () => {
const suggestion = {
value: 'test',
label: 'Did you mean',
pre: 'pre',
mid: 'mid',
post: 'post',
labelEnd: '???',
tooltip: 'Accept suggestion',
onClick: vi.fn(),
ignoreLabel: 'Ignore',
ignoreTooltip: 'Ignore suggestion',
ignoreClick: vi.fn(),
};
render(<Suggestion suggestion={suggestion} />);
expect(screen.getByText('Did you mean')).toBeInTheDocument();
expect(screen.getByText('pre')).toBeInTheDocument();
expect(screen.getByText('mid')).toBeInTheDocument();
expect(screen.getByText('post')).toBeInTheDocument();
expect(screen.getByText('???')).toBeInTheDocument();
expect(screen.getByText('Accept suggestion')).toBeInTheDocument();
expect(screen.getByText('Ignore')).toBeInTheDocument();
expect(screen.getByText('Ignore suggestion')).toBeInTheDocument();
});
it('calls onClick when suggestion button is clicked', () => {
const handleClick = vi.fn();
render(<Suggestion suggestion={{ value: 'test', onClick: handleClick }} />);
fireEvent.click(screen.getByRole('button'));
expect(handleClick).toHaveBeenCalledOnce();
});
it('calls ignoreClick when ignore button is clicked', () => {
const ignoreClick = vi.fn();
const suggestion = { value: true, ignoreClick };
render(<Suggestion suggestion={suggestion} />);
fireEvent.click(screen.getByText('Ignorar'));
expect(ignoreClick).toHaveBeenCalled();
});
it('should not render error when suggestion is displayed', () => {
render(<FormField name="test" error="This is an error" suggestion={{ value: 'test', onClick: vi.fn() }} />);
expect(screen.queryByText('This is an error')).not.toBeInTheDocument();
});
it('should render without label', () => {
render(<Suggestion suggestion={{ value: 'test', label: '', onClick: vi.fn() }} />);
expect(screen.queryByText('Você quis dizer')).not.toBeInTheDocument();
});
it('should render without labelEnd', () => {
render(<Suggestion suggestion={{ value: 'test', labelEnd: '', onClick: vi.fn() }} />);
expect(screen.queryByText('?')).not.toBeInTheDocument();
});
});
});
});
});