-
Notifications
You must be signed in to change notification settings - Fork 26.5k
docs: add dedicated router testing guide #62445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Deployed adev-preview for 3a59419 to: https://ng-dev-previews-fw--pr-angular-angular-62445-adev-prev-na7fzsww.web.app Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, I droped some drive-by comments.
37e39c7
to
16782c4
Compare
16782c4
to
9bbcadd
Compare
91d05d4
to
83634f9
Compare
83634f9
to
3b2f36e
Compare
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a new guide for testing routing and navigation in Angular applications. The guide covers route parameters, guards, router outlets, nested routes, and query parameters. It also includes best practices for router testing.
3b2f36e
to
3a59419
Compare
userId: string | null; | ||
|
||
constructor() { | ||
this.userId = this.route.snapshot.paramMap.get('id'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userId: string | null; | |
constructor() { | |
this.userId = this.route.snapshot.paramMap.get('id'); | |
} | |
userId = this.route.snapshot.paramMap.get('id'); |
const instance = await harness.navigateByUrl('/user/123', UserProfile); | ||
|
||
expect(instance.userId).toBe('123'); | ||
expect(TestBed.inject(Router).url).toBe('/user/123'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing Angular instead of the component stuff itself. Still don't know if we should keep it or not. It would be better if this tested the template (DOM) with the result instead of the typescript part .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the correct way to write the tests. It is not simply "testing Angular", but testing the component's interaction with Angular and that the outcomes are correct. In addition, the Angular Router code is not static. If there's a breaking change in the router, intentional or not, your tests should catch it.
Edit: My apologies, I actually didn't look at the test here and thought this was testing something else (a guard). I agree that it would probably be better to test the template in this case since it's not testing anything in the application that causes or affects a navigation (in which case, the above would have applied)
|
||
it('allows navigation when user is authenticated', async () => { | ||
await setup(true); | ||
await harness.navigateByUrl('/protected'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we can pass the component that we expect to load, and the test will fail with the guard doesnt allow the route to be activated.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Adds a dedicated testing guide for router.
Does this PR introduce a breaking change?
Other information