Files
angular__angular/packages/router/test/router_link_active.spec.ts
Andrew Scott eae75ff3f9 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
2024-03-27 10:16:16 -07:00

29 lines
1.0 KiB
TypeScript

/**
* @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([]);
});
});