fix(router): RouterLinkActive should run CD when setting isActive (#21411)

When using the routerLinkActive directive inside a component that is using ChangeDetectionStrategy.OnPush and lazy loaded module routes the routerLinkActive directive does not update after clicking a link to a lazy loaded route that has not already been loaded.

Also the OnPush nav component does not set routerLinkActive correctly when the default route loads, the non-OnPush nav component works fine.

regression caused by #15943
closes #19934

PR Close #21411
This commit is contained in:
Manduro
2018-02-23 09:48:19 +01:00
committed by Andrew Kushnir
parent 5218916a7e
commit 80d0067048
3 changed files with 34 additions and 4 deletions

View File

@ -7,7 +7,7 @@
*/
import {CommonModule} from '@angular/common';
import {Component, ContentChild, NgModule, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {ChangeDetectionStrategy, Component, ContentChild, NgModule, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {Router} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
@ -109,6 +109,35 @@ describe('Integration', () => {
expect(fixture.nativeElement.innerHTML).toContain('isActive: false');
}));
it('should set isActive with OnPush change detection - #19934', fakeAsync(() => {
@Component({
template: `
<div routerLink="/simple" #rla="routerLinkActive" routerLinkActive>
isActive: {{rla.isActive}}
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class OnPushComponent {
}
@Component({template: 'simple'})
class SimpleCmp {
}
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([{path: 'simple', component: SimpleCmp}])],
declarations: [OnPushComponent, SimpleCmp]
});
const router: Router = TestBed.get(Router);
const fixture = createRoot(router, OnPushComponent);
router.navigateByUrl('/simple');
advance(fixture);
expect(fixture.nativeElement.innerHTML).toContain('isActive: true');
}));
});
});