Skip to content

Commit c3e5118

Browse files
committed
feat(): test browser mode
1 parent 81a1a16 commit c3e5118

6 files changed

Lines changed: 324 additions & 197 deletions

File tree

apps/forms/61-simplest-signal-form/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
},
6565
"test": {
6666
"executor": "@angular/build:unit-test",
67-
"options": {}
67+
"options": {
68+
"runnerConfig": "apps/forms/61-simplest-signal-form/vitest-base.config.ts"
69+
}
6870
},
6971
"serve-static": {
7072
"continuous": true,
Lines changed: 140 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,243 +1,227 @@
1-
import { render, screen } from '@testing-library/angular';
2-
import '@testing-library/jest-dom';
3-
import userEvent from '@testing-library/user-event';
1+
import { TestBed } from '@angular/core/testing';
2+
import { page, userEvent } from 'vitest/browser';
43
import { AppComponent } from './app.component';
54

65
describe('AppComponent', () => {
6+
beforeEach(async () => {
7+
TestBed.createComponent(AppComponent);
8+
});
9+
710
describe('When component is rendered', () => {
811
it('Then should display the form title', async () => {
9-
await render(AppComponent);
10-
11-
expect(screen.getByText('Simple Form')).toBeInTheDocument();
12+
const heading = page.getByRole('heading', { name: /simple form/i });
13+
await expect.element(heading).toBeInTheDocument();
1214
});
1315

1416
it('Then should display all form fields', async () => {
15-
await render(AppComponent);
16-
17-
expect(screen.getByLabelText(/^Name/i)).toBeInTheDocument();
18-
expect(screen.getByLabelText(/last name/i)).toBeInTheDocument();
19-
expect(screen.getByLabelText(/age/i)).toBeInTheDocument();
20-
expect(screen.getByLabelText(/note/i)).toBeInTheDocument();
17+
await expect.element(page.getByLabelText('Name *')).toBeInTheDocument();
18+
await expect
19+
.element(page.getByLabelText(/last name/i))
20+
.toBeInTheDocument();
21+
await expect.element(page.getByLabelText(/age/i)).toBeInTheDocument();
22+
await expect.element(page.getByLabelText(/note/i)).toBeInTheDocument();
2123
});
2224

2325
it('Then submit button should be disabled initially', async () => {
24-
await render(AppComponent);
25-
26-
const submitButton = screen.getByRole('button', { name: /submit/i });
27-
expect(submitButton).toBeDisabled();
26+
const submitButton = page.getByRole('button', { name: /submit/i });
27+
await expect.element(submitButton).toBeDisabled();
2828
});
2929
});
3030

3131
describe('Given valid form data', () => {
3232
describe('When user fills in all required fields', () => {
3333
it('Then submit button should be enabled', async () => {
34-
const user = userEvent.setup();
35-
await render(AppComponent);
36-
37-
const nameInput = screen.getByLabelText(/^Name/i);
38-
await user.type(nameInput, 'John');
34+
const nameInput = page.getByLabelText('Name *');
35+
await nameInput.fill('John');
3936

40-
const submitButton = screen.getByRole('button', { name: /submit/i });
41-
expect(submitButton).toBeEnabled();
37+
const submitButton = page.getByRole('button', { name: /submit/i });
38+
await expect.element(submitButton).toBeEnabled();
4239
});
4340
});
4441

4542
describe('When user submits the form', () => {
4643
it('Then should display submitted data', async () => {
47-
const user = userEvent.setup();
48-
await render(AppComponent);
49-
50-
const nameInput = screen.getByLabelText(/^Name/i);
51-
const lastnameInput = screen.getByLabelText(/last name/i);
52-
const ageInput = screen.getByLabelText(/age/i);
53-
const noteInput = screen.getByLabelText(/note/i);
54-
55-
await user.type(nameInput, 'John');
56-
await user.type(lastnameInput, 'Doe');
57-
await user.type(ageInput, '25');
58-
await user.type(noteInput, 'Test note');
59-
60-
const submitButton = screen.getByRole('button', { name: /submit/i });
61-
await user.click(submitButton);
62-
63-
expect(screen.getByText('Submitted Data:')).toBeInTheDocument();
64-
expect(screen.getByText(/"name": "John"/)).toBeInTheDocument();
65-
expect(screen.getByText(/"lastname": "Doe"/)).toBeInTheDocument();
66-
expect(screen.getByText(/"age": 25/)).toBeInTheDocument();
67-
expect(screen.getByText(/"note": "Test note"/)).toBeInTheDocument();
44+
const nameInput = page.getByLabelText('Name *');
45+
const lastnameInput = page.getByLabelText(/last name/i);
46+
const ageInput = page.getByLabelText(/age/i);
47+
const noteInput = page.getByLabelText(/note/i);
48+
49+
await nameInput.fill('John');
50+
await lastnameInput.fill('Doe');
51+
await ageInput.fill('25');
52+
await noteInput.fill('Test note');
53+
54+
const submitButton = page.getByRole('button', { name: /submit/i });
55+
await submitButton.click();
56+
57+
await expect
58+
.element(page.getByText('Submitted Data:'))
59+
.toBeInTheDocument();
60+
await expect
61+
.element(page.getByText(/"name": "John"/))
62+
.toBeInTheDocument();
63+
await expect
64+
.element(page.getByText(/"lastname": "Doe"/))
65+
.toBeInTheDocument();
66+
await expect.element(page.getByText(/"age": 25/)).toBeInTheDocument();
67+
await expect
68+
.element(page.getByText(/"note": "Test note"/))
69+
.toBeInTheDocument();
6870
});
6971
});
7072
});
7173

7274
describe('Given name field validation', () => {
7375
describe('When name field is empty and touched', () => {
7476
it('Then should display required error', async () => {
75-
const user = userEvent.setup();
76-
await render(AppComponent);
77+
const nameInput = page.getByLabelText('Name *');
78+
await nameInput.click();
79+
await userEvent.keyboard('{Tab}');
7780

78-
const nameInput = screen.getByLabelText(/^Name/i);
79-
await user.click(nameInput);
80-
await user.tab();
81-
82-
expect(screen.getByText('Name is required')).toBeInTheDocument();
81+
await expect
82+
.element(page.getByText('Name is required'))
83+
.toBeInTheDocument();
8384
});
8485

8586
it('Then submit button should be disabled', async () => {
86-
const user = userEvent.setup();
87-
await render(AppComponent);
88-
89-
const nameInput = screen.getByLabelText(/^Name/i);
90-
await user.click(nameInput);
91-
await user.tab();
87+
const nameInput = page.getByLabelText('Name *');
88+
await nameInput.click();
89+
await userEvent.keyboard('{Tab}');
9290

93-
const submitButton = screen.getByRole('button', { name: /submit/i });
94-
expect(submitButton).toBeDisabled();
91+
const submitButton = page.getByRole('button', { name: /submit/i });
92+
await expect.element(submitButton).toBeDisabled();
9593
});
9694
});
9795
});
9896

9997
describe('Given age field validation', () => {
10098
describe('When age is less than 1', () => {
10199
it('Then should display min error', async () => {
102-
const user = userEvent.setup();
103-
await render(AppComponent);
104-
105-
const nameInput = screen.getByLabelText(/^Name/i);
106-
const ageInput = screen.getByLabelText(/age/i);
100+
const nameInput = page.getByLabelText('Name *');
101+
const ageInput = page.getByLabelText(/age/i);
107102

108-
await user.type(nameInput, 'John');
109-
await user.type(ageInput, '0');
110-
await user.tab();
103+
await nameInput.fill('John');
104+
await ageInput.fill('0');
105+
await userEvent.keyboard('{Tab}');
111106

112-
expect(screen.getByText('Age must be at least 1')).toBeInTheDocument();
107+
await expect
108+
.element(page.getByText('Age must be at least 1'))
109+
.toBeInTheDocument();
113110
});
114111
});
115112

116113
describe('When age is greater than 99', () => {
117114
it('Then should display max error', async () => {
118-
const user = userEvent.setup();
119-
await render(AppComponent);
115+
const nameInput = page.getByLabelText('Name *');
116+
const ageInput = page.getByLabelText(/age/i);
120117

121-
const nameInput = screen.getByLabelText(/^Name/i);
122-
const ageInput = screen.getByLabelText(/age/i);
118+
await nameInput.fill('John');
119+
await ageInput.fill('100');
120+
await userEvent.keyboard('{Tab}');
123121

124-
await user.type(nameInput, 'John');
125-
await user.type(ageInput, '100');
126-
await user.tab();
127-
128-
expect(screen.getByText('Age must be at most 99')).toBeInTheDocument();
122+
await expect
123+
.element(page.getByText('Age must be at most 99'))
124+
.toBeInTheDocument();
129125
});
130126
});
131127

132128
describe('When age is between 1 and 99', () => {
133129
it('Then should not display any error', async () => {
134-
const user = userEvent.setup();
135-
await render(AppComponent);
136-
137-
const nameInput = screen.getByLabelText(/^Name/i);
138-
const ageInput = screen.getByLabelText(/age/i);
130+
const nameInput = page.getByLabelText('Name *');
131+
const ageInput = page.getByLabelText(/age/i);
139132

140-
await user.type(nameInput, 'John');
141-
await user.type(ageInput, '50');
142-
await user.tab();
133+
await nameInput.fill('John');
134+
await ageInput.fill('50');
135+
await userEvent.keyboard('{Tab}');
143136

144-
expect(screen.queryByText(/age must be/i)).not.toBeInTheDocument();
137+
await expect
138+
.element(page.getByText(/age must be/i))
139+
.not.toBeInTheDocument();
145140
});
146141
});
147142
});
148143

149144
describe('Given optional fields', () => {
150145
describe('When lastname and note are empty', () => {
151146
it('Then should still allow form submission with valid name', async () => {
152-
const user = userEvent.setup();
153-
await render(AppComponent);
154-
155-
const nameInput = screen.getByLabelText(/^Name/i);
156-
await user.type(nameInput, 'John');
147+
const nameInput = page.getByLabelText('Name *');
148+
await nameInput.fill('John');
157149

158-
const submitButton = screen.getByRole('button', { name: /submit/i });
159-
expect(submitButton).toBeEnabled();
150+
const submitButton = page.getByRole('button', { name: /submit/i });
151+
await expect.element(submitButton).toBeEnabled();
160152

161-
await user.click(submitButton);
153+
await submitButton.click();
162154

163-
expect(screen.getByText('Submitted Data:')).toBeInTheDocument();
155+
await expect
156+
.element(page.getByText('Submitted Data:'))
157+
.toBeInTheDocument();
164158
});
165159
});
166160
});
167161

168162
describe('Given reset functionality', () => {
169163
describe('When user clicks reset button after filling form', () => {
170164
it('Then should clear all form fields', async () => {
171-
const user = userEvent.setup();
172-
await render(AppComponent);
173-
174-
const nameInput = screen.getByLabelText(/^Name/i) as HTMLInputElement;
175-
const lastnameInput = screen.getByLabelText(
176-
/last name/i,
177-
) as HTMLInputElement;
178-
const ageInput = screen.getByLabelText(/age/i) as HTMLInputElement;
179-
const noteInput = screen.getByLabelText(/note/i) as HTMLInputElement;
180-
181-
await user.type(nameInput, 'John');
182-
await user.type(lastnameInput, 'Doe');
183-
await user.type(ageInput, '25');
184-
await user.type(noteInput, 'Test note');
185-
186-
const resetButton = screen.getByRole('button', { name: /reset/i });
187-
await user.click(resetButton);
188-
189-
expect(nameInput.value).toBe('');
190-
expect(lastnameInput.value).toBe('');
191-
expect(ageInput.value).toBe('');
192-
expect(noteInput.value).toBe('');
165+
const nameInput = page.getByLabelText('Name *');
166+
const lastnameInput = page.getByLabelText(/last name/i);
167+
const ageInput = page.getByLabelText(/age/i);
168+
const noteInput = page.getByLabelText(/note/i);
169+
170+
await nameInput.fill('John');
171+
await lastnameInput.fill('Doe');
172+
await ageInput.fill('25');
173+
await noteInput.fill('Test note');
174+
175+
const resetButton = page.getByRole('button', { name: /reset/i });
176+
await resetButton.click();
177+
178+
await expect.element(nameInput).toHaveValue('');
179+
await expect.element(lastnameInput).toHaveValue('');
180+
await expect.element(ageInput).toHaveValue(null);
181+
await expect.element(noteInput).toHaveValue('');
193182
});
194183

195184
it('Then should hide submitted data if present', async () => {
196-
const user = userEvent.setup();
197-
await render(AppComponent);
185+
const nameInput = page.getByLabelText('Name *');
186+
await nameInput.fill('John');
198187

199-
const nameInput = screen.getByLabelText(/^Name/i);
200-
await user.type(nameInput, 'John');
188+
const submitButton = page.getByRole('button', { name: /submit/i });
189+
await submitButton.click();
201190

202-
const submitButton = screen.getByRole('button', { name: /submit/i });
203-
await user.click(submitButton);
191+
await expect
192+
.element(page.getByText('Submitted Data:'))
193+
.toBeInTheDocument();
204194

205-
expect(screen.getByText('Submitted Data:')).toBeInTheDocument();
195+
const resetButton = page.getByRole('button', { name: /reset/i });
196+
await resetButton.click();
206197

207-
const resetButton = screen.getByRole('button', { name: /reset/i });
208-
await user.click(resetButton);
209-
210-
expect(screen.queryByText('Submitted Data:')).not.toBeInTheDocument();
198+
await expect
199+
.element(page.getByText('Submitted Data:'))
200+
.not.toBeInTheDocument();
211201
});
212202
});
213203
});
214204

215-
describe('Given form styling', () => {
216-
describe('When field has validation error', () => {
217-
it('Then should display red border on name field', async () => {
218-
const user = userEvent.setup();
219-
await render(AppComponent);
220-
221-
const nameInput = screen.getByLabelText(/^Name/i);
222-
await user.click(nameInput);
223-
await user.tab();
224-
225-
expect(nameInput).toHaveClass('border-red-500');
226-
});
227-
228-
it('Then should display red border on age field when invalid', async () => {
229-
const user = userEvent.setup();
230-
await render(AppComponent);
231-
232-
const nameInput = screen.getByLabelText(/^Name/i);
233-
const ageInput = screen.getByLabelText(/age/i);
234-
235-
await user.type(nameInput, 'John');
236-
await user.type(ageInput, '0');
237-
await user.tab();
238-
239-
expect(ageInput).toHaveClass('border-red-500');
240-
});
241-
});
242-
});
205+
// describe('Given form styling', () => {
206+
// describe('When field has validation error', () => {
207+
// it('Then should display red border on name field', async () => {
208+
// const nameInput = page.getByLabelText(/^Name$/i);
209+
// await nameInput.click();
210+
// await userEvent.keyboard('{Tab}');
211+
//
212+
// await expect.element(nameInput).toHaveClass('border-red-500');
213+
// });
214+
//
215+
// it('Then should display red border on age field when invalid', async () => {
216+
// const nameInput = page.getByLabelText(/^Name$/i);
217+
// const ageInput = page.getByLabelText(/age/i);
218+
//
219+
// await nameInput.fill('John');
220+
// await ageInput.fill('0');
221+
// await page.keyboard.press('Tab');
222+
//
223+
// await expect.element(ageInput).toHaveClass('border-red-500');
224+
// });
225+
// });
226+
// });
243227
});

0 commit comments

Comments
 (0)