Skip to content

Test case for the issue 280 #284

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 5 commits into from
Feb 16, 2022
Merged
Changes from 2 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
55 changes: 55 additions & 0 deletions projects/testing-library/tests/issues/issue-280.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {Component, NgModule} from '@angular/core';
import {render, screen} from '../../src/public_api';
import {RouterModule, Routes} from "@angular/router";
import {RouterTestingModule} from "@angular/router/testing";
import {click} from "@testing-library/user-event/dist/click";
import {Location} from "@angular/common";

@Component({
template: `<div>Navigate</div> <router-outlet></router-outlet>`,
})
class MainComponent {}

@Component({
template: `<div>first page</div><a routerLink="/second">go to second</a>`
})
class FirstComponent {}

@Component({
template: `<div>second page</div><button (click)="goBack()">navigate back</button>`
})
class SecondComponent {
constructor(private location: Location) { }
goBack() {this.location.back();}
}

const routes: Routes = [
{path: '', redirectTo: '/first', pathMatch: 'full'},
{path: 'first', component: FirstComponent},
{path: 'second', component: SecondComponent}
];

@NgModule({
declarations: [FirstComponent, SecondComponent],
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
class AppRoutingModule {}


test('navigate to second page and back', async () => {
const subject = await render(MainComponent, {imports: [AppRoutingModule, RouterTestingModule]});
await subject.navigate('/');

expect(await screen.findByText('Navigate')).toBeTruthy();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left extra expects in place, showing that there is successful navigation to second page.
I typically don't do that in my tests, please let me know if you want me to remove extras.

Thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, thanks!

expect(await screen.findByText('first page')).toBeTruthy();

click(await screen.findByText('go to second'));

expect(await screen.findByText('second page')).toBeTruthy();
expect(await screen.findByText('navigate back')).toBeTruthy();

click(await screen.findByText('navigate back'));

expect(await screen.findByText('first page')).toBeTruthy();
});