Skip to content

Commit 56ee50b

Browse files
fix: call navigate correctly without queryparams (#154)
1 parent 10d5079 commit 56ee50b

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
5-
import { Router } from '@angular/router';
5+
import { NavigationExtras, Router } from '@angular/router';
66
import { RouterTestingModule } from '@angular/router/testing';
77
import {
88
getQueriesForElement as dtlGetQueriesForElement,
99
prettyDOM as dtlPrettyDOM,
1010
waitFor as dtlWaitFor,
1111
waitForElementToBeRemoved as dtlWaitForElementToBeRemoved,
1212
screen as dtlScreen,
13-
queries as dtlQueries,
1413
waitForOptions as dtlWaitForOptions,
1514
configure as dtlConfigure,
1615
} from '@testing-library/dom';
@@ -134,15 +133,19 @@ export async function render<SutType, WrapperType = SutType>(
134133
const queryParams = params
135134
? params.split('&').reduce((qp, q) => {
136135
const [key, value] = q.split('=');
136+
// TODO(Breaking): group same keys qp[key] ? [...qp[key], value] : value
137137
qp[key] = value;
138138
return qp;
139139
}, {})
140140
: undefined;
141141

142-
const doNavigate = () =>
143-
router.navigate([path], {
144-
queryParams,
145-
});
142+
const navigateOptions: NavigationExtras = queryParams
143+
? {
144+
queryParams,
145+
}
146+
: undefined;
147+
148+
const doNavigate = () => (navigateOptions ? router.navigate([path], navigateOptions) : router.navigate([path]));
146149

147150
let result;
148151

projects/testing-library/tests/find-by.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from '@angular/core';
22
import { timer } from 'rxjs';
33
import { render, screen } from '../src/public_api';
4-
import { mapTo, timeout } from 'rxjs/operators';
4+
import { mapTo } from 'rxjs/operators';
55

66
@Component({
77
selector: 'fixture',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Component } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { Router } from '@angular/router';
4+
import { render } from '../src/public_api';
5+
6+
@Component({
7+
selector: 'fixture',
8+
template: ``,
9+
})
10+
class FixtureComponent {}
11+
12+
test('should navigate correctly', async () => {
13+
const { navigate } = await render(FixtureComponent, {
14+
routes: [{ path: 'details', component: FixtureComponent }],
15+
});
16+
17+
const router = TestBed.inject(Router);
18+
const navSpy = jest.spyOn(router, 'navigate');
19+
20+
navigate('details');
21+
22+
expect(navSpy).toBeCalledWith(['details']);
23+
});
24+
25+
test('should pass queryParams if provided', async () => {
26+
const { navigate } = await render(FixtureComponent, {
27+
routes: [{ path: 'details', component: FixtureComponent }],
28+
});
29+
30+
const router = TestBed.inject(Router);
31+
const navSpy = jest.spyOn(router, 'navigate');
32+
33+
navigate('details?sortBy=name&sortOrder=asc');
34+
35+
expect(navSpy).toBeCalledWith(['details'], {
36+
queryParams: {
37+
sortBy: 'name',
38+
sortOrder: 'asc',
39+
},
40+
});
41+
});

0 commit comments

Comments
 (0)