Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d958c5

Browse files
fix: extract router params in navigate (testing-library#93)
Closes testing-library#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

Lines changed: 20 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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(/Backtoparent/));
7474
expect(screen.queryByText(/Detailthree/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(/Detailtwo/i)).toBeInTheDocument();
78+
expect(screen.queryByText(/HelloWorld/i)).toBeInTheDocument();
79+
7880
await navigate('/hidden-detail', basePath);
7981
expect(screen.queryByText(/Youfoundthetreasure!/i)).toBeInTheDocument();
8082
});

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

Lines changed: 4 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /