fix(aio): avoid loading the ToC component until it is necessary (#23944)

PR Close #23944
This commit is contained in:
George Kalpakas 2018-05-16 20:37:04 +03:00 committed by Matias Niemelä
parent 6e05ae02a2
commit 7866684f2b
2 changed files with 20 additions and 10 deletions

View File

@ -92,11 +92,11 @@ describe('AppComponent', () => {
}); });
describe('hasFloatingToc', () => { describe('hasFloatingToc', () => {
it('should initially be true', () => { it('should initially be false', () => {
const fixture2 = TestBed.createComponent(AppComponent); const fixture2 = TestBed.createComponent(AppComponent);
const component2 = fixture2.componentInstance; const component2 = fixture2.componentInstance;
expect(component2.hasFloatingToc).toBe(true); expect(component2.hasFloatingToc).toBe(false);
}); });
it('should be false on narrow screens', () => { it('should be false on narrow screens', () => {
@ -632,27 +632,28 @@ describe('AppComponent', () => {
toc = tocContainer && tocContainer.querySelector('aio-toc'); toc = tocContainer && tocContainer.querySelector('aio-toc');
}; };
beforeEach(() => setHasFloatingToc(true));
it('should show/hide `<aio-toc>` based on `hasFloatingToc`', () => { it('should show/hide `<aio-toc>` based on `hasFloatingToc`', () => {
expect(tocContainer).toBeTruthy();
expect(toc).toBeTruthy();
setHasFloatingToc(false);
expect(tocContainer).toBeFalsy(); expect(tocContainer).toBeFalsy();
expect(toc).toBeFalsy(); expect(toc).toBeFalsy();
setHasFloatingToc(true); setHasFloatingToc(true);
expect(tocContainer).toBeTruthy(); expect(tocContainer).toBeTruthy();
expect(toc).toBeTruthy(); expect(toc).toBeTruthy();
setHasFloatingToc(false);
expect(tocContainer).toBeFalsy();
expect(toc).toBeFalsy();
}); });
it('should have a non-embedded `<aio-toc>` element', () => { it('should have a non-embedded `<aio-toc>` element', () => {
setHasFloatingToc(true);
expect(toc!.classList.contains('embedded')).toBe(false); expect(toc!.classList.contains('embedded')).toBe(false);
}); });
it('should update the TOC container\'s `maxHeight` based on `tocMaxHeight`', () => { it('should update the TOC container\'s `maxHeight` based on `tocMaxHeight`', () => {
setHasFloatingToc(true);
expect(tocContainer!.style['max-height']).toBe(''); expect(tocContainer!.style['max-height']).toBe('');
component.tocMaxHeight = '100'; component.tocMaxHeight = '100';
@ -665,11 +666,20 @@ describe('AppComponent', () => {
const restrainScrolling = spyOn(component, 'restrainScrolling'); const restrainScrolling = spyOn(component, 'restrainScrolling');
const evt = new MouseEvent('mousewheel'); const evt = new MouseEvent('mousewheel');
setHasFloatingToc(true);
expect(restrainScrolling).not.toHaveBeenCalled(); expect(restrainScrolling).not.toHaveBeenCalled();
tocContainer!.dispatchEvent(evt); tocContainer!.dispatchEvent(evt);
expect(restrainScrolling).toHaveBeenCalledWith(evt); expect(restrainScrolling).toHaveBeenCalledWith(evt);
}); });
it('should not be loaded/registered until necessary', () => {
const loader: TestElementsLoader = fixture.debugElement.injector.get(ElementsLoader);
expect(loader.loadCustomElement).not.toHaveBeenCalled();
setHasFloatingToc(true);
expect(loader.loadCustomElement).toHaveBeenCalledWith('aio-toc');
});
}); });
describe('footer', () => { describe('footer', () => {
@ -1377,7 +1387,7 @@ class TestHttpClient {
const id = match[1]!; const id = match[1]!;
// Make up a title for test purposes // Make up a title for test purposes
const title = id.split('/').pop()!.replace(/^([a-z])/, (_, letter) => letter.toUpperCase()); const title = id.split('/').pop()!.replace(/^([a-z])/, (_, letter) => letter.toUpperCase());
const h1 = (id === 'no-title') ? '' : `<h1>${title}</h1>`; const h1 = (id === 'no-title') ? '' : `<h1 class="no-toc">${title}</h1>`;
const contents = `${h1}<h2 id="#somewhere">Some heading</h2>`; const contents = `${h1}<h2 id="#somewhere">Some heading</h2>`;
data = { id, contents }; data = { id, contents };
} }

View File

@ -69,7 +69,7 @@ export class AppComponent implements OnInit {
topMenuNodes: NavigationNode[]; topMenuNodes: NavigationNode[];
topMenuNarrowNodes: NavigationNode[]; topMenuNarrowNodes: NavigationNode[];
hasFloatingToc = true; hasFloatingToc = false;
private showFloatingToc = new BehaviorSubject(false); private showFloatingToc = new BehaviorSubject(false);
private showFloatingTocWidth = 800; private showFloatingTocWidth = 800;
tocMaxHeight: string; tocMaxHeight: string;