Skip to content

fix: invoke initialNavigation when a router is injected #286

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
Feb 25, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'*.{ts,js}': ['eslint --fix'],
'*.{json,md}': ['prettier --write'],
'*.{ts,js,json,md}': ['prettier --write'],
};
9 changes: 4 additions & 5 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ export async function render<SutType, WrapperType = SutType>(
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
};

let router = routes ? inject(Router) : null;
const zone = inject(NgZone);
const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
if (!router) {
router = inject(Router);
}

const router = inject(Router);
router?.initialNavigation();
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

It will be available when the PR is merged (now) 😀


const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
const [path, params] = (basePath + href).split('?');
const queryParams = params
Expand Down
60 changes: 21 additions & 39 deletions projects/testing-library/tests/issues/issue-280.spec.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,48 @@
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";
import { Location } from '@angular/common';
import { Component, NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import userEvent from '@testing-library/user-event';
import { render, screen } from '../../src/public_api';

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

@Component({
template: `<div>first page</div><a routerLink="/second">go to second</a>`
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>`
template: `<div>second page</div>
<button (click)="goBack()">navigate back</button>`,
})
class SecondComponent {
constructor(private location: Location) { }
goBack() {this.location.back();}
constructor(private location: Location) {}
goBack() {
this.location.back();
}
}

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

@NgModule({
declarations: [FirstComponent, SecondComponent],
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
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('/');
await render(MainComponent, { imports: [AppRoutingModule, RouterTestingModule] });

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