|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {describe, expect, it} from '@angular/core/testing/src/testing_internal'; |
| 10 | +import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; |
10 | 11 | import {BrowserViewportScroller, ViewportScroller} from '../src/viewport_scroller'; |
11 | 12 |
|
12 | 13 | describe('BrowserViewportScroller', () => { |
@@ -44,44 +45,103 @@ describe('BrowserViewportScroller', () => { |
44 | 45 | // Testing scroll behavior does not make sense outside a browser |
45 | 46 | if (isNode) return; |
46 | 47 | const anchor = 'anchor'; |
47 | | - let tallItem: HTMLDivElement; |
48 | | - let el: HTMLAnchorElement; |
49 | 48 | let scroller: BrowserViewportScroller; |
50 | 49 |
|
51 | 50 | beforeEach(() => { |
52 | 51 | scroller = new BrowserViewportScroller(document, window); |
53 | 52 | scroller.scrollToPosition([0, 0]); |
54 | | - |
55 | | - tallItem = document.createElement('div'); |
56 | | - tallItem.style.height = '3000px'; |
57 | | - document.body.appendChild(tallItem); |
58 | | - |
59 | | - el = document.createElement('a'); |
60 | | - el.innerText = 'some link'; |
61 | | - el.href = '#'; |
62 | | - document.body.appendChild(el); |
63 | | - }); |
64 | | - |
65 | | - afterEach(() => { |
66 | | - document.body.removeChild(tallItem); |
67 | | - document.body.removeChild(el); |
68 | 53 | }); |
69 | 54 |
|
70 | 55 | it('should scroll when element with matching id is found', () => { |
71 | | - el.id = anchor; |
| 56 | + const {anchorNode, cleanup} = createTallElement(); |
| 57 | + anchorNode.id = anchor; |
72 | 58 | scroller.scrollToAnchor(anchor); |
73 | 59 | expect(scroller.getScrollPosition()[1]).not.toEqual(0); |
| 60 | + cleanup(); |
74 | 61 | }); |
75 | 62 |
|
76 | 63 | it('should scroll when anchor with matching name is found', () => { |
77 | | - el.name = anchor; |
| 64 | + const {anchorNode, cleanup} = createTallElement(); |
| 65 | + anchorNode.name = anchor; |
78 | 66 | scroller.scrollToAnchor(anchor); |
79 | 67 | expect(scroller.getScrollPosition()[1]).not.toEqual(0); |
| 68 | + cleanup(); |
80 | 69 | }); |
81 | 70 |
|
82 | 71 | it('should not scroll when no matching element is found', () => { |
| 72 | + const {cleanup} = createTallElement(); |
83 | 73 | scroller.scrollToAnchor(anchor); |
84 | 74 | expect(scroller.getScrollPosition()[1]).toEqual(0); |
| 75 | + cleanup(); |
85 | 76 | }); |
| 77 | + |
| 78 | + it('should scroll when element with matching id is found inside the shadow DOM', () => { |
| 79 | + // This test is only relevant for browsers that support shadow DOM. |
| 80 | + if (!browserDetection.supportsShadowDom) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const {anchorNode, cleanup} = createTallElementWithShadowRoot(); |
| 85 | + anchorNode.id = anchor; |
| 86 | + scroller.scrollToAnchor(anchor); |
| 87 | + expect(scroller.getScrollPosition()[1]).not.toEqual(0); |
| 88 | + cleanup(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should scroll when anchor with matching name is found inside the shadow DOM', () => { |
| 92 | + // This test is only relevant for browsers that support shadow DOM. |
| 93 | + if (!browserDetection.supportsShadowDom) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const {anchorNode, cleanup} = createTallElementWithShadowRoot(); |
| 98 | + anchorNode.name = anchor; |
| 99 | + scroller.scrollToAnchor(anchor); |
| 100 | + expect(scroller.getScrollPosition()[1]).not.toEqual(0); |
| 101 | + cleanup(); |
| 102 | + }); |
| 103 | + |
| 104 | + function createTallElement() { |
| 105 | + const tallItem = document.createElement('div'); |
| 106 | + tallItem.style.height = '3000px'; |
| 107 | + document.body.appendChild(tallItem); |
| 108 | + const anchorNode = createAnchorNode(); |
| 109 | + document.body.appendChild(anchorNode); |
| 110 | + |
| 111 | + return { |
| 112 | + anchorNode, |
| 113 | + cleanup: () => { |
| 114 | + document.body.removeChild(tallItem); |
| 115 | + document.body.removeChild(anchorNode); |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + function createTallElementWithShadowRoot() { |
| 121 | + const tallItem = document.createElement('div'); |
| 122 | + tallItem.style.height = '3000px'; |
| 123 | + document.body.appendChild(tallItem); |
| 124 | + |
| 125 | + const elementWithShadowRoot = document.createElement('div'); |
| 126 | + const shadowRoot = elementWithShadowRoot.attachShadow({mode: 'open'}); |
| 127 | + const anchorNode = createAnchorNode(); |
| 128 | + shadowRoot.appendChild(anchorNode); |
| 129 | + document.body.appendChild(elementWithShadowRoot); |
| 130 | + |
| 131 | + return { |
| 132 | + anchorNode, |
| 133 | + cleanup: () => { |
| 134 | + document.body.removeChild(tallItem); |
| 135 | + document.body.removeChild(elementWithShadowRoot); |
| 136 | + } |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + function createAnchorNode() { |
| 141 | + const anchorNode = document.createElement('a'); |
| 142 | + anchorNode.innerText = 'some link'; |
| 143 | + anchorNode.href = '#'; |
| 144 | + return anchorNode; |
| 145 | + } |
86 | 146 | }); |
87 | 147 | }); |
0 commit comments