Skip to content

Commit d758b94

Browse files
fix: invoke initialNavigation when a router is imported (#286)
1 parent b349939 commit d758b94

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

lint-staged.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
'*.{ts,js}': ['eslint --fix'],
3-
'*.{json,md}': ['prettier --write'],
3+
'*.{ts,js,json,md}': ['prettier --write'],
44
};

projects/testing-library/src/lib/testing-library.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ export async function render<SutType, WrapperType = SutType>(
116116
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
117117
};
118118

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

121+
const router = inject(Router);
122+
router?.initialNavigation();
123+
124+
const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
126125
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
127126
const [path, params] = (basePath + href).split('?');
128127
const queryParams = params

projects/testing-library/tests/issues/issue-280.spec.ts

+21-39
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,48 @@
1-
import {Component, NgModule} from '@angular/core';
2-
import {render, screen} from '@testing-library/angular';
3-
import {Router, RouterModule, Routes} from "@angular/router";
4-
import {RouterTestingModule} from "@angular/router/testing";
5-
import userEvent from "@testing-library/user-event";
6-
import {Location} from "@angular/common";
7-
import {TestBed} from "@angular/core/testing";
1+
import { Location } from '@angular/common';
2+
import { Component, NgModule } from '@angular/core';
3+
import { RouterModule, Routes } from '@angular/router';
4+
import { RouterTestingModule } from '@angular/router/testing';
5+
import userEvent from '@testing-library/user-event';
6+
import { render, screen } from '../../src/public_api';
87

98
@Component({
10-
template: `<div>Navigate</div> <router-outlet></router-outlet>`,
9+
template: `<div>Navigate</div>
10+
<router-outlet></router-outlet>`,
1111
})
1212
class MainComponent {}
1313

1414
@Component({
15-
template: `<div>first page</div><a routerLink="/second">go to second</a>`
15+
template: `<div>first page</div>
16+
<a routerLink="/second">go to second</a>`,
1617
})
1718
class FirstComponent {}
1819

1920
@Component({
20-
template: `<div>second page</div><button (click)="goBack()">navigate back</button>`
21+
template: `<div>second page</div>
22+
<button (click)="goBack()">navigate back</button>`,
2123
})
2224
class SecondComponent {
23-
constructor(private location: Location) { }
24-
goBack() {this.location.back();}
25+
constructor(private location: Location) {}
26+
goBack() {
27+
this.location.back();
28+
}
2529
}
2630

2731
const routes: Routes = [
28-
{path: '', redirectTo: '/first', pathMatch: 'full'},
29-
{path: 'first', component: FirstComponent},
30-
{path: 'second', component: SecondComponent}
32+
{ path: '', redirectTo: '/first', pathMatch: 'full' },
33+
{ path: 'first', component: FirstComponent },
34+
{ path: 'second', component: SecondComponent },
3135
];
3236

3337
@NgModule({
3438
declarations: [FirstComponent, SecondComponent],
3539
imports: [RouterModule.forRoot(routes)],
36-
exports: [RouterModule]
40+
exports: [RouterModule],
3741
})
3842
class AppRoutingModule {}
3943

40-
41-
// test('navigate to second page and back', async () => {
42-
// const subject = await render(MainComponent, {imports: [AppRoutingModule, RouterTestingModule]});
43-
// await subject.navigate('/');
44-
//
45-
// expect(await screen.findByText('Navigate')).toBeInTheDocument();
46-
// expect(await screen.findByText('first page')).toBeInTheDocument();
47-
//
48-
// userEvent.click(await screen.findByText('go to second'));
49-
//
50-
// expect(await screen.findByText('second page')).toBeInTheDocument();
51-
// expect(await screen.findByText('navigate back')).toBeInTheDocument();
52-
//
53-
// userEvent.click(await screen.findByText('navigate back'));
54-
//
55-
// expect(await screen.findByText('first page')).toBeInTheDocument();
56-
// });
57-
5844
test('navigate to second page and back', async () => {
59-
const subject = await render(MainComponent, {imports: [AppRoutingModule, RouterTestingModule]});
60-
61-
TestBed.inject(Router).initialNavigation();
62-
63-
await subject.navigate('/');
45+
await render(MainComponent, { imports: [AppRoutingModule, RouterTestingModule] });
6446

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

0 commit comments

Comments
 (0)