Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(router): RouterLinkActive will always remove active classes when …
…links are not active (#54982) Previously, `RouterLinkActive` would only add or remove the active classes when its active state changed. This means that if you accidentally add one of the active classes to the static class attribute, it won't get removed until the link becomes active and then deactives (because the class is added at creation time and never removed until the `RouterLinkActive` state changes from active to inactive). fixes #54978 PR Close #54982
- Loading branch information
Showing
2 changed files
with
47 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {Component} from '@angular/core'; | ||
import {TestBed} from '@angular/core/testing'; | ||
import {Router, RouterLink, RouterLinkActive, provideRouter} from '@angular/router'; | ||
|
||
describe('RouterLinkActive', () => { | ||
it('removes initial active class even if never active', async () => { | ||
@Component({ | ||
standalone: true, | ||
imports: [RouterLinkActive, RouterLink], | ||
template: '<a class="active" routerLinkActive="active" routerLink="/abc123"></a>', | ||
}) | ||
class MyCmp {} | ||
|
||
TestBed.configureTestingModule({providers: [provideRouter([{path: '**', children: []}])]}); | ||
const fixture = TestBed.createComponent(MyCmp); | ||
fixture.autoDetectChanges(); | ||
await TestBed.inject(Router).navigateByUrl('/'); | ||
await fixture.whenStable(); | ||
expect(Array.from(fixture.nativeElement.querySelector('a').classList)).toEqual([]); | ||
}); | ||
}); |