Skip to content
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();
});