|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 |
| -/** |
12 |
| - * @license |
13 |
| - * Copyright Google LLC All Rights Reserved. |
14 |
| - * |
15 |
| - * Use of this source code is governed by an MIT-style license that can be |
16 |
| - * found in the LICENSE file at https://angular.io/license |
17 |
| - */ |
18 |
| - |
19 | 9 | import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
20 | 10 | import {BrowserViewportScroller, ViewportScroller} from '../src/viewport_scroller';
|
21 | 11 |
|
22 |
| -{ |
23 |
| - describe('BrowserViewportScroller', () => { |
24 |
| - let scroller: ViewportScroller; |
25 |
| - let documentSpy: any; |
26 |
| - let windowSpy: any; |
| 12 | +describe('BrowserViewportScroller', () => { |
| 13 | + let scroller: ViewportScroller; |
| 14 | + let documentSpy: any; |
| 15 | + let windowSpy: any; |
27 | 16 |
|
28 |
| - beforeEach(() => { |
29 |
| - windowSpy = jasmine.createSpyObj('window', ['history']); |
30 |
| - windowSpy.scrollTo = 1; |
31 |
| - windowSpy.history.scrollRestoration = 'auto'; |
32 |
| - |
33 |
| - documentSpy = jasmine.createSpyObj('document', ['querySelector']); |
34 |
| - scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!); |
35 |
| - }); |
| 17 | + beforeEach(() => { |
| 18 | + windowSpy = jasmine.createSpyObj('window', ['history']); |
| 19 | + windowSpy.scrollTo = 1; |
| 20 | + windowSpy.history.scrollRestoration = 'auto'; |
| 21 | + documentSpy = jasmine.createSpyObj('document', ['getElementById', 'getElementsByName']); |
| 22 | + scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!); |
| 23 | + }); |
36 | 24 |
|
| 25 | + describe('setHistoryScrollRestoration', () => { |
37 | 26 | it('should not crash when scrollRestoration is not writable', () => {
|
38 | 27 | Object.defineProperty(windowSpy.history, 'scrollRestoration', {
|
39 | 28 | value: 'auto',
|
40 | 29 | configurable: true,
|
41 | 30 | });
|
42 | 31 | expect(() => scroller.setHistoryScrollRestoration('manual')).not.toThrow();
|
43 | 32 | });
|
| 33 | + }); |
| 34 | + |
| 35 | + describe('scrollToAnchor', () => { |
| 36 | + const anchor = 'anchor'; |
| 37 | + const el = document.createElement('a'); |
| 38 | + |
| 39 | + it('should only call getElementById when an element is found by id', () => { |
| 40 | + documentSpy.getElementById.and.returnValue(el); |
| 41 | + spyOn<any>(scroller, 'scrollToElement'); |
| 42 | + scroller.scrollToAnchor(anchor); |
| 43 | + expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor); |
| 44 | + expect(documentSpy.getElementsByName).not.toHaveBeenCalled(); |
| 45 | + expect((scroller as any).scrollToElement).toHaveBeenCalledWith(el); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should call getElementById and getElementsByName when an element is found by name', () => { |
| 49 | + documentSpy.getElementsByName.and.returnValue([el]); |
| 50 | + spyOn<any>(scroller, 'scrollToElement'); |
| 51 | + scroller.scrollToAnchor(anchor); |
| 52 | + expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor); |
| 53 | + expect(documentSpy.getElementsByName).toHaveBeenCalledWith(anchor); |
| 54 | + expect((scroller as any).scrollToElement).toHaveBeenCalledWith(el); |
| 55 | + }); |
44 | 56 |
|
45 |
| - it('escapes invalid characters selectors', () => { |
46 |
| - const invalidSelectorChars = `"' :.[],=`; |
47 |
| - // Double escaped to make sure we match the actual value passed to `querySelector` |
48 |
| - const escapedInvalids = `\\"\\' \\:\\.\\[\\]\\,\\=`; |
49 |
| - scroller.scrollToAnchor(`specials=${invalidSelectorChars}`); |
50 |
| - expect(documentSpy.querySelector).toHaveBeenCalledWith(`#specials\\=${escapedInvalids}`); |
51 |
| - expect(documentSpy.querySelector) |
52 |
| - .toHaveBeenCalledWith(`[name='specials\\=${escapedInvalids}']`); |
| 57 | + it('should not call scrollToElement when an element is not found by its id or its name', () => { |
| 58 | + documentSpy.getElementsByName.and.returnValue([]); |
| 59 | + spyOn<any>(scroller, 'scrollToElement'); |
| 60 | + scroller.scrollToAnchor(anchor); |
| 61 | + expect(documentSpy.getElementById).toHaveBeenCalledWith(anchor); |
| 62 | + expect(documentSpy.getElementsByName).toHaveBeenCalledWith(anchor); |
| 63 | + expect((scroller as any).scrollToElement).not.toHaveBeenCalled(); |
53 | 64 | });
|
54 | 65 | });
|
55 |
| -} |
| 66 | +}); |
0 commit comments