Skip to content

Commit 2d958c5

Browse files
fix: extract router params in navigate (#93)
Closes #91
1 parent 4cb5e5c commit 2d958c5

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,28 @@ export async function render<SutType, WrapperType = SutType>(
121121
}
122122

123123
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
124+
const [path, params] = (basePath + href).split('?');
125+
const queryParams = params
126+
? params.split('&').reduce((qp, q) => {
127+
const [key, value] = q.split('=');
128+
qp[key] = value;
129+
return qp;
130+
}, {})
131+
: undefined;
132+
133+
const doNavigate = () =>
134+
router.navigate([path], {
135+
queryParams,
136+
});
124137

125138
let result;
126-
await zone.run(() => (result = router.navigate([basePath + href])));
139+
140+
if (zone) {
141+
await zone.run(() => (result = doNavigate()));
142+
} else {
143+
result = doNavigate();
144+
}
145+
127146
detectChanges();
128147
return result;
129148
};

src/app/examples/09-router.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ test('it can navigate to routes with a base path', async () => {
7373
await navigate(screen.getByText(/Back to parent/));
7474
expect(screen.queryByText(/Detail three/i)).not.toBeInTheDocument();
7575

76-
await navigate('base/detail/two'); // possible to just use strings
76+
await navigate('base/detail/two?text=Hello&subtext=World'); // possible to just use strings
7777
expect(screen.queryByText(/Detail two/i)).toBeInTheDocument();
78+
expect(screen.queryByText(/Hello World/i)).toBeInTheDocument();
79+
7880
await navigate('/hidden-detail', basePath);
7981
expect(screen.queryByText(/You found the treasure!/i)).toBeInTheDocument();
8082
});

src/app/examples/09-router.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ export class MasterComponent {}
2020
template: `
2121
<h2>Detail {{ id | async }}</h2>
2222
23+
<p>{{ text | async }} {{ subtext | async }}</p>
24+
2325
<a [routerLink]="'../..'">Back to parent</a>
2426
<a routerLink="/hidden-detail">hidden x</a>
2527
`,
2628
})
2729
export class DetailComponent {
2830
id = this.route.paramMap.pipe(map(params => params.get('id')));
31+
text = this.route.queryParams.pipe(map(params => params['text']));
32+
subtext = this.route.queryParams.pipe(map(params => params['subtext']));
2933
constructor(private route: ActivatedRoute) {}
3034
}
3135

0 commit comments

Comments
 (0)