-
Notifications
You must be signed in to change notification settings - Fork 26.4k
docs: add draft 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 37e39c7 to: https://ng-dev-previews-fw--pr-angular-angular-62445-adev-prev-wbsw2qej.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.
import { Router } from '@angular/router'; | ||
import { Location } from '@angular/common'; | ||
import { Component } from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; |
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.
You have a duplicate import of @angular/router
{ path: 'about', component: MockAboutComponent } | ||
]) | ||
] | ||
}).compileComponents(); |
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.
}).compileComponents(); | |
}) |
This isn't required unless you have a @defer
block + AOT: true.
Make sure to also remove the other ones.
imports: [RouterOutlet], | ||
template: ` | ||
<h1>Parent Component</h1> | ||
<router-outlet></router-outlet> |
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.
<router-outlet></router-outlet> | |
<router-outlet /> |
export class ParentComponent {} | ||
|
||
@Component({ | ||
selector: 'app-child', |
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.
Routed components don't require selectors. We can probably omit it here.
private route = inject(ActivatedRoute); | ||
searchTerm: string | null = null; | ||
|
||
ngOnInit() { |
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.
I'd argue to move that to the constructor since we don't have any dependency in inputs.
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.
There is big difference between ctor v.s. ngOnInit.
I would put no logic into the ctor.
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.
The recommendations have changed and no longer recommands to put init logic into the ngOnInit.
private route = inject(ActivatedRoute); | ||
userId: string | null = null; | ||
|
||
ngOnInit() { |
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.
We can probably move that to the constructor.
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.
If not applied for some reason, the test itself should not need to call component.ngOnInit();
.
<docs-code header="user-profile.component.spec.ts" language="typescript"> | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { UserProfileComponent } from './user-profile.component'; |
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.
I think the Component
class suffix and .component
suffix can be dropped from this and other imports/component names.
Also thank you for writing this guide. I am the worst with routing and testing respectively so this is great stuff to have
import { TestBed } from '@angular/core/testing'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { authGuard } from './auth.guard'; | ||
import { AuthService } from './auth.service'; |
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.
As a follow up of @michael-small style guide recommandation, we could rename the service to AuthStore
.
|
||
export const authGuard: CanActivateFn = () => { | ||
const authService = inject(AuthService); | ||
return authService.isAuthenticated(); |
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.
return authService.isAuthenticated(); | |
return inject(AuthService).isAuthenticated(); |
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.
refactor for a single line
await TestBed.configureTestingModule({ | ||
imports: [UserProfileComponent], | ||
providers: [ | ||
{ provide: ActivatedRoute, useValue: activatedRouteStub } |
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.
Instead of this I'd recommend to only test using RouterTestingHarness which should take care of most of the things.
I guess @atscott will agree to this
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