diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 98de7cde..c171a4a1 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -121,9 +121,28 @@ export async function render( } const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href'); + const [path, params] = (basePath + href).split('?'); + const queryParams = params + ? params.split('&').reduce((qp, q) => { + const [key, value] = q.split('='); + qp[key] = value; + return qp; + }, {}) + : undefined; + + const doNavigate = () => + router.navigate([path], { + queryParams, + }); let result; - await zone.run(() => (result = router.navigate([basePath + href]))); + + if (zone) { + await zone.run(() => (result = doNavigate())); + } else { + result = doNavigate(); + } + detectChanges(); return result; }; diff --git a/src/app/examples/09-router.spec.ts b/src/app/examples/09-router.spec.ts index 43e4712a..d4eaca73 100644 --- a/src/app/examples/09-router.spec.ts +++ b/src/app/examples/09-router.spec.ts @@ -73,8 +73,10 @@ test('it can navigate to routes with a base path', async () => { await navigate(screen.getByText(/Back to parent/)); expect(screen.queryByText(/Detail three/i)).not.toBeInTheDocument(); - await navigate('base/detail/two'); // possible to just use strings + await navigate('base/detail/two?text=Hello&subtext=World'); // possible to just use strings expect(screen.queryByText(/Detail two/i)).toBeInTheDocument(); + expect(screen.queryByText(/Hello World/i)).toBeInTheDocument(); + await navigate('/hidden-detail', basePath); expect(screen.queryByText(/You found the treasure!/i)).toBeInTheDocument(); }); diff --git a/src/app/examples/09-router.ts b/src/app/examples/09-router.ts index e0350fcc..de4408f5 100644 --- a/src/app/examples/09-router.ts +++ b/src/app/examples/09-router.ts @@ -20,12 +20,16 @@ export class MasterComponent {} template: `

Detail {{ id | async }}

+

{{ text | async }} {{ subtext | async }}

+ Back to parent hidden x `, }) export class DetailComponent { id = this.route.paramMap.pipe(map(params => params.get('id'))); + text = this.route.queryParams.pipe(map(params => params['text'])); + subtext = this.route.queryParams.pipe(map(params => params['subtext'])); constructor(private route: ActivatedRoute) {} }