Skip to content

docs: add test case for #491 #494

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 1 commit into from
Oct 14, 2024
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
57 changes: 57 additions & 0 deletions apps/example-app-karma/src/app/issues/issue-491.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { render, screen, waitForElementToBeRemoved } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';

it('test click event with router.navigate', async () => {
const user = userEvent.setup();
await render(`<router-outlet></router-outlet>`, {
routes: [
{
path: '',
component: LoginComponent,
},
{
path: 'logged-in',
component: LoggedInComponent,
},
],
});

expect(await screen.findByRole('heading', { name: 'Login' })).toBeVisible();
expect(screen.getByRole('button', { name: 'submit' })).toBeInTheDocument();

const email = screen.getByRole('textbox', { name: 'email' });
const password = screen.getByLabelText('password');

await user.type(email, 'user@example.com');
await user.type(password, 'with_valid_password');

expect(screen.getByRole('button', { name: 'submit' })).toBeEnabled();

await user.click(screen.getByRole('button', { name: 'submit' }));

await waitForElementToBeRemoved(() => screen.queryByRole('heading', { name: 'Login' }));

expect(await screen.findByRole('heading', { name: 'Logged In' })).toBeVisible();
});

@Component({
template: `
<h1>Login</h1>
<button type="submit" (click)="onSubmit()">submit</button>
<input type="email" aria-label="email" />
<input type="password" aria-label="password" />
`,
})
class LoginComponent {
constructor(private router: Router) {}
onSubmit(): void {
this.router.navigate(['logged-in']);
}
}

@Component({
template: ` <h1>Logged In</h1> `,
})
class LoggedInComponent {}