Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be42079

Browse files
1 parent 3dceae0 commit be42079

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Component } from '@angular/core';
2+
import { FormBuilder, FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
3+
import userEvent from '@testing-library/user-event';
4+
import { render, screen } from '@testing-library/angular';
5+
6+
@Component({
7+
selector: 'app-login',
8+
template: `<h1>Login</h1>
9+
10+
<form [formGroup]="form" (submit)="onSubmit(form)">
11+
<input type="email" aria-label="email" formControlName="email" />
12+
<div *ngIf="email.invalid && (email.dirty || email.touched)">
13+
<div *ngIf="email.errors?.['required']">Email is required</div>
14+
</div>
15+
<input type="password" aria-label="password" formControlName="password" />
16+
<div *ngIf="password.invalid && (password.dirty || password.touched)">
17+
<div *ngIf="password.errors?.['required']">Password is required</div>
18+
</div>
19+
<button type="submit" aria-label="submit" [disabled]="form.invalid">Submit</button>
20+
</form> `,
21+
})
22+
class LoginComponent {
23+
form: FormGroup = this.fb.group({
24+
email: ['', [Validators.required]],
25+
password: ['', [Validators.required]],
26+
});
27+
28+
constructor(private fb: FormBuilder) {}
29+
30+
get email(): FormControl {
31+
return this.form.get('email') as FormControl;
32+
}
33+
34+
get password(): FormControl {
35+
return this.form.get('password') as FormControl;
36+
}
37+
38+
onSubmit(_fg: FormGroup): void {
39+
// do nothing
40+
}
41+
}
42+
43+
describe('LoginComponent', () => {
44+
const setup = async () => {
45+
return render(LoginComponent, {
46+
imports: [ReactiveFormsModule],
47+
});
48+
};
49+
50+
it('should create a component with inputs and a button to submit', async () => {
51+
await setup();
52+
53+
expect(screen.getByRole('textbox', { name: 'email' })).toBeInTheDocument();
54+
expect(screen.getByLabelText('password')).toBeInTheDocument();
55+
expect(screen.getByRole('button', { name: 'submit' })).toBeInTheDocument();
56+
});
57+
58+
it('should show a message required to password and login and a button must be disabled', async () => {
59+
await setup();
60+
61+
await userEvent.click(screen.getByRole('textbox', { name: 'email' }));
62+
await userEvent.click(screen.getByLabelText('password'));
63+
await userEvent.tab();
64+
await userEvent.click(screen.getByRole('button', { name: 'submit' }));
65+
66+
expect(screen.getAllByText(/required/i).length).toBe(2);
67+
expect(screen.getByRole('button', { name: 'submit' })).toBeDisabled();
68+
});
69+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /