fix(aio): add small delay before autoscrolling

This delay allows any async layout to complete
before we autoscroll to a heading anchor.

Closes #16242
This commit is contained in:
Peter Bacon Darwin 2017-05-18 14:37:45 +01:00
parent 9466908c22
commit 0e13a5956c
2 changed files with 13 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core'; import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core';
import { async, inject, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, inject, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Title } from '@angular/platform-browser'; import { Title } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common'; import { APP_BASE_HREF } from '@angular/common';
import { Http } from '@angular/http'; import { Http } from '@angular/http';
@ -317,6 +317,7 @@ describe('AppComponent', () => {
}); });
describe('auto-scrolling', () => { describe('auto-scrolling', () => {
const scrollDelay = 500;
let scrollService: ScrollService; let scrollService: ScrollService;
let scrollSpy: jasmine.Spy; let scrollSpy: jasmine.Spy;
@ -350,16 +351,20 @@ describe('AppComponent', () => {
expect(scrollSpy.calls.count()).toBe(2); expect(scrollSpy.calls.count()).toBe(2);
}); });
it('should scroll when call onDocRendered directly', () => { it('should scroll after a delay when call onDocRendered directly', fakeAsync(() => {
component.onDocRendered(); component.onDocRendered();
expect(scrollSpy).not.toHaveBeenCalled();
tick(scrollDelay);
expect(scrollSpy).toHaveBeenCalled(); expect(scrollSpy).toHaveBeenCalled();
}); }));
it('should scroll (via onDocRendered) when finish navigating to a new doc', () => { it('should scroll (via onDocRendered) when finish navigating to a new doc', fakeAsync(() => {
locationService.go('guide/pipes'); locationService.go('guide/pipes');
fixture.detectChanges(); // triggers the event that calls onDocRendered fixture.detectChanges(); // triggers the event that calls onDocRendered
expect(scrollSpy).not.toHaveBeenCalled();
tick(scrollDelay);
expect(scrollSpy).toHaveBeenCalled(); expect(scrollSpy).toHaveBeenCalled();
}); }));
}); });
describe('initial rendering', () => { describe('initial rendering', () => {

View File

@ -159,8 +159,9 @@ export class AppComponent implements OnInit {
} }
onDocRendered() { onDocRendered() {
// Scroll after the doc-viewer has finished rendering the new doc // Scroll 500ms after the doc-viewer has finished rendering the new doc
this.autoScroll(); // The delay is to allow time for async layout to complete
setTimeout(() => this.autoScroll(), 500);
this.isStarting = false; this.isStarting = false;
} }