Skip to content

Commit

Permalink
docs: add test case for #491 (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Oct 14, 2024
1 parent be9c3d5 commit 2cfca82
Showing 1 changed file with 57 additions and 0 deletions.
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, '[email protected]');
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 {}

0 comments on commit 2cfca82

Please sign in to comment.