2019-03-28 18:00:51 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-03-28 18:00:51 -04:00
|
|
|
*
|
|
|
|
* 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 {describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
|
|
|
import {BrowserViewportScroller, ViewportScroller} from '../src/viewport_scroller';
|
|
|
|
|
2020-07-16 04:58:17 -04:00
|
|
|
describe('BrowserViewportScroller', () => {
|
|
|
|
let scroller: ViewportScroller;
|
|
|
|
let documentSpy: any;
|
|
|
|
let windowSpy: any;
|
2019-05-23 08:44:46 -04:00
|
|
|
|
2020-07-16 04:58:17 -04:00
|
|
|
beforeEach(() => {
|
2020-08-14 13:32:55 -04:00
|
|
|
windowSpy = jasmine.createSpyObj('window', ['history', 'scrollTo']);
|
2020-07-16 04:58:17 -04:00
|
|
|
windowSpy.history.scrollRestoration = 'auto';
|
|
|
|
documentSpy = jasmine.createSpyObj('document', ['getElementById', 'getElementsByName']);
|
|
|
|
scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!);
|
|
|
|
});
|
2019-05-23 08:44:46 -04:00
|
|
|
|
2020-07-16 04:58:17 -04:00
|
|
|
describe('setHistoryScrollRestoration', () => {
|
2020-08-14 13:32:55 -04:00
|
|
|
function createNonWritableScrollRestoration() {
|
2019-05-23 08:44:46 -04:00
|
|
|
Object.defineProperty(windowSpy.history, 'scrollRestoration', {
|
|
|
|
value: 'auto',
|
|
|
|
configurable: true,
|
|
|
|
});
|
2020-08-14 13:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should not crash when scrollRestoration is not writable', () => {
|
|
|
|
createNonWritableScrollRestoration();
|
2019-05-23 08:44:46 -04:00
|
|
|
expect(() => scroller.setHistoryScrollRestoration('manual')).not.toThrow();
|
|
|
|
});
|
2020-08-14 13:32:55 -04:00
|
|
|
|
|
|
|
it('should still allow scrolling if scrollRestoration is not writable', () => {
|
|
|
|
createNonWritableScrollRestoration();
|
|
|
|
scroller.scrollToPosition([10, 10]);
|
|
|
|
expect(windowSpy.scrollTo as jasmine.Spy).toHaveBeenCalledWith(10, 10);
|
|
|
|
});
|
2020-07-16 04:58:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('scrollToAnchor', () => {
|
|
|
|
const anchor = 'anchor';
|
|
|
|
const el = document.createElement('a');
|
|
|
|
|
|
|
|
it('should only call getElementById when an element is found by id', () => {
|
|
|
|
documentSpy.getElementById.and.returnValue(el);
|
|
|
|
spyOn<any>(scroller, 'scrollToElement');
|
|
|
|
scroller.scrollToAnchor(anchor);
|
|
|
|
expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor);
|
|
|
|
expect(documentSpy.getElementsByName).not.toHaveBeenCalled();
|
|
|
|
expect((scroller as any).scrollToElement).toHaveBeenCalledWith(el);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call getElementById and getElementsByName when an element is found by name', () => {
|
|
|
|
documentSpy.getElementsByName.and.returnValue([el]);
|
|
|
|
spyOn<any>(scroller, 'scrollToElement');
|
|
|
|
scroller.scrollToAnchor(anchor);
|
|
|
|
expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor);
|
|
|
|
expect(documentSpy.getElementsByName).toHaveBeenCalledWith(anchor);
|
|
|
|
expect((scroller as any).scrollToElement).toHaveBeenCalledWith(el);
|
|
|
|
});
|
2019-05-23 08:44:46 -04:00
|
|
|
|
2020-07-16 04:58:17 -04:00
|
|
|
it('should not call scrollToElement when an element is not found by its id or its name', () => {
|
|
|
|
documentSpy.getElementsByName.and.returnValue([]);
|
|
|
|
spyOn<any>(scroller, 'scrollToElement');
|
|
|
|
scroller.scrollToAnchor(anchor);
|
|
|
|
expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor);
|
|
|
|
expect(documentSpy.getElementsByName).toHaveBeenCalledWith(anchor);
|
|
|
|
expect((scroller as any).scrollToElement).not.toHaveBeenCalled();
|
2019-03-28 18:00:51 -04:00
|
|
|
});
|
|
|
|
});
|
2020-07-16 04:58:17 -04:00
|
|
|
});
|