Skip to content

docs: reproduce #373 #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions apps/example-app-karma/src/app/issues/issue-373.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Component } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/angular';

@Component({
selector: 'app-login',
template: `<h1>Login</h1>

<form [formGroup]="form" (submit)="onSubmit(form)">
<input type="email" aria-label="email" formControlName="email" />
<div *ngIf="email.invalid && (email.dirty || email.touched)">
<div *ngIf="email.errors?.['required']">Email is required</div>
</div>
<input type="password" aria-label="password" formControlName="password" />
<div *ngIf="password.invalid && (password.dirty || password.touched)">
<div *ngIf="password.errors?.['required']">Password is required</div>
</div>
<button type="submit" aria-label="submit" [disabled]="form.invalid">Submit</button>
</form> `,
})
class LoginComponent {
form: FormGroup = this.fb.group({
email: ['', [Validators.required]],
password: ['', [Validators.required]],
});

constructor(private fb: FormBuilder) {}

get email(): FormControl {
return this.form.get('email') as FormControl;
}

get password(): FormControl {
return this.form.get('password') as FormControl;
}

onSubmit(_fg: FormGroup): void {
// do nothing
}
}

describe('LoginComponent', () => {
const setup = async () => {
return render(LoginComponent, {
imports: [ReactiveFormsModule],
});
};

it('should create a component with inputs and a button to submit', async () => {
await setup();

expect(screen.getByRole('textbox', { name: 'email' })).toBeInTheDocument();
expect(screen.getByLabelText('password')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'submit' })).toBeInTheDocument();
});

it('should show a message required to password and login and a button must be disabled', async () => {
await setup();

await userEvent.click(screen.getByRole('textbox', { name: 'email' }));
await userEvent.click(screen.getByLabelText('password'));
await userEvent.tab();
await userEvent.click(screen.getByRole('button', { name: 'submit' }));

expect(screen.getAllByText(/required/i).length).toBe(2);
expect(screen.getByRole('button', { name: 'submit' })).toBeDisabled();
});
});