|
| 1 | +import {inject, TestBed, async} from '@angular/core/testing'; |
| 2 | +import {ComponentPortal, OverlayModule, BlockScrollStrategy, Platform} from '../../core'; |
| 3 | +import {ViewportRuler} from '../position/viewport-ruler'; |
| 4 | + |
| 5 | + |
| 6 | +describe('BlockScrollStrategy', () => { |
| 7 | + let platform = new Platform(); |
| 8 | + let strategy: BlockScrollStrategy; |
| 9 | + let viewport: ViewportRuler; |
| 10 | + let forceScrollElement: HTMLElement; |
| 11 | + |
| 12 | + beforeEach(async(() => { |
| 13 | + TestBed.configureTestingModule({ imports: [OverlayModule] }).compileComponents(); |
| 14 | + })); |
| 15 | + |
| 16 | + beforeEach(inject([ViewportRuler], (_viewportRuler: ViewportRuler) => { |
| 17 | + strategy = new BlockScrollStrategy(_viewportRuler); |
| 18 | + viewport = _viewportRuler; |
| 19 | + forceScrollElement = document.createElement('div'); |
| 20 | + document.body.appendChild(forceScrollElement); |
| 21 | + forceScrollElement.style.width = '100px'; |
| 22 | + forceScrollElement.style.height = '3000px'; |
| 23 | + })); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + document.body.removeChild(forceScrollElement); |
| 27 | + setScrollPosition(0, 0); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should toggle scroll blocking along the y axis', skipUnreliableBrowser(() => { |
| 31 | + forceScrollElement.style.height = '3000px'; |
| 32 | + |
| 33 | + setScrollPosition(0, 100); |
| 34 | + expect(viewport.getViewportScrollPosition().top).toBe(100, |
| 35 | + 'Expected viewport to be scrollable initially.'); |
| 36 | + |
| 37 | + strategy.enable(); |
| 38 | + expect(document.documentElement.style.top).toBe('-100px', |
| 39 | + 'Expected <html> element to be offset by the previous scroll amount along the y axis.'); |
| 40 | + |
| 41 | + setScrollPosition(0, 300); |
| 42 | + expect(viewport.getViewportScrollPosition().top).toBe(100, |
| 43 | + 'Expected the viewport not to scroll.'); |
| 44 | + |
| 45 | + strategy.disable(); |
| 46 | + expect(viewport.getViewportScrollPosition().top).toBe(100, |
| 47 | + 'Expected old scroll position to have bee restored after disabling.'); |
| 48 | + |
| 49 | + setScrollPosition(0, 300); |
| 50 | + expect(viewport.getViewportScrollPosition().top).toBe(300, |
| 51 | + 'Expected user to be able to scroll after disabling.'); |
| 52 | + })); |
| 53 | + |
| 54 | + |
| 55 | + it('should toggle scroll blocking along the x axis', skipUnreliableBrowser(() => { |
| 56 | + forceScrollElement.style.width = '3000px'; |
| 57 | + |
| 58 | + setScrollPosition(100, 0); |
| 59 | + expect(viewport.getViewportScrollPosition().left).toBe(100, |
| 60 | + 'Expected viewport to be scrollable initially.'); |
| 61 | + |
| 62 | + strategy.enable(); |
| 63 | + expect(document.documentElement.style.left).toBe('-100px', |
| 64 | + 'Expected <html> element to be offset by the previous scroll amount along the x axis.'); |
| 65 | + |
| 66 | + setScrollPosition(300, 0); |
| 67 | + expect(viewport.getViewportScrollPosition().left).toBe(100, |
| 68 | + 'Expected the viewport not to scroll.'); |
| 69 | + |
| 70 | + strategy.disable(); |
| 71 | + expect(viewport.getViewportScrollPosition().left).toBe(100, |
| 72 | + 'Expected old scroll position to have bee restored after disabling.'); |
| 73 | + |
| 74 | + setScrollPosition(300, 0); |
| 75 | + expect(viewport.getViewportScrollPosition().left).toBe(300, |
| 76 | + 'Expected user to be able to scroll after disabling.'); |
| 77 | + })); |
| 78 | + |
| 79 | + |
| 80 | + it('should toggle the `cdk-global-scrollblock` class', skipUnreliableBrowser(() => { |
| 81 | + forceScrollElement.style.height = '3000px'; |
| 82 | + |
| 83 | + expect(document.documentElement.classList).not.toContain('cdk-global-scrollblock'); |
| 84 | + |
| 85 | + strategy.enable(); |
| 86 | + expect(document.documentElement.classList).toContain('cdk-global-scrollblock'); |
| 87 | + |
| 88 | + strategy.disable(); |
| 89 | + expect(document.documentElement.classList).not.toContain('cdk-global-scrollblock'); |
| 90 | + })); |
| 91 | + |
| 92 | + it('should restore any previously-set inline styles', skipUnreliableBrowser(() => { |
| 93 | + const root = document.documentElement; |
| 94 | + |
| 95 | + forceScrollElement.style.height = '3000px'; |
| 96 | + root.style.top = '13px'; |
| 97 | + root.style.left = '37px'; |
| 98 | + |
| 99 | + strategy.enable(); |
| 100 | + |
| 101 | + expect(root.style.top).not.toBe('13px'); |
| 102 | + expect(root.style.left).not.toBe('37px'); |
| 103 | + |
| 104 | + strategy.disable(); |
| 105 | + |
| 106 | + expect(root.style.top).toBe('13px'); |
| 107 | + expect(root.style.left).toBe('37px'); |
| 108 | + })); |
| 109 | + |
| 110 | + it(`should't do anything if the page isn't scrollable`, skipUnreliableBrowser(() => { |
| 111 | + forceScrollElement.style.display = 'none'; |
| 112 | + strategy.enable(); |
| 113 | + expect(document.documentElement.classList).not.toContain('cdk-global-scrollblock'); |
| 114 | + })); |
| 115 | + |
| 116 | + |
| 117 | + // In the iOS simulator (BrowserStack & SauceLabs), adding content to the |
| 118 | + // body causes karma's iframe for the test to stretch to fit that content, |
| 119 | + // in addition to not allowing us to set the scroll position programmatically. |
| 120 | + // This renders the tests unusable and since we can't really do anything about it, |
| 121 | + // we have to skip them on iOS. |
| 122 | + function skipUnreliableBrowser(spec: Function) { |
| 123 | + return () => { |
| 124 | + if (!platform.IOS) { spec(); } |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + function setScrollPosition(x: number, y: number) { |
| 129 | + window.scroll(x, y); |
| 130 | + viewport._cacheViewportGeometry(); |
| 131 | + } |
| 132 | + |
| 133 | +}); |
0 commit comments