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 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
76 changes: 76 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,76 @@
import {Component, NgModule} from '@angular/core';
import {render, screen} from '@testing-library/angular';
import {Router, RouterModule, Routes} from "@angular/router";
import {RouterTestingModule} from "@angular/router/testing";
import userEvent from "@testing-library/user-event";
import {Location} from "@angular/common";
import {TestBed} from "@angular/core/testing";

@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')).toBeInTheDocument();
// expect(await screen.findByText('first page')).toBeInTheDocument();
//
// userEvent.click(await screen.findByText('go to second'));
//
// expect(await screen.findByText('second page')).toBeInTheDocument();
// expect(await screen.findByText('navigate back')).toBeInTheDocument();
//
// userEvent.click(await screen.findByText('navigate back'));
//
// expect(await screen.findByText('first page')).toBeInTheDocument();
// });

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

TestBed.inject(Router).initialNavigation();

await subject.navigate('/');

expect(await screen.findByText('Navigate')).toBeInTheDocument();
expect(await screen.findByText('first page')).toBeInTheDocument();

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

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

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

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