-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy path03-forms.spec.ts
48 lines (38 loc) · 1.79 KB
/
03-forms.spec.ts
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
import { render, screen, fireEvent } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { FormsComponent } from './03-forms';
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
const user = userEvent.setup();
await render(FormsComponent);
const nameControl = screen.getByRole('textbox', { name: /name/i });
const scoreControl = screen.getByRole('spinbutton', { name: /score/i });
const colorControl = screen.getByRole('combobox', { name: /color/i });
const errors = screen.getByRole('alert');
expect(errors).toContainElement(screen.queryByText('name is required'));
expect(errors).toContainElement(screen.queryByText('score must be greater than 1'));
expect(errors).toContainElement(screen.queryByText('color is required'));
expect(nameControl).toBeInvalid();
await user.type(nameControl, 'Tim');
await user.clear(scoreControl);
await user.type(scoreControl, '12');
fireEvent.blur(scoreControl);
await user.selectOptions(colorControl, 'G');
expect(screen.queryByText('name is required')).not.toBeInTheDocument();
expect(screen.getByText('score must be lesser than 10')).toBeInTheDocument();
expect(screen.queryByText('color is required')).not.toBeInTheDocument();
expect(scoreControl).toBeInvalid();
await user.clear(scoreControl);
await user.type(scoreControl, '7');
fireEvent.blur(scoreControl);
expect(scoreControl).toBeValid();
expect(errors).not.toBeInTheDocument();
expect(nameControl).toHaveValue('Tim');
expect(scoreControl).toHaveValue(7);
expect(colorControl).toHaveValue('G');
const form = screen.getByRole('form');
expect(form).toHaveFormValues({
name: 'Tim',
score: 7,
color: 'G',
});
});