|
| 1 | +import { |
| 2 | + inject, |
| 3 | + fakeAsync, |
| 4 | + flushMicrotasks, |
| 5 | +} from 'angular2/testing'; |
| 6 | +import { |
| 7 | + it, |
| 8 | + describe, |
| 9 | + expect, |
| 10 | + beforeEach, |
| 11 | +} from '../../../core/facade/testing'; |
| 12 | +import {BrowserDomAdapter} from '../../platform/browser/browser_adapter'; |
| 13 | +import {DOM} from '../../platform/dom/dom_adapter'; |
| 14 | +import {GlobalPositionStrategy} from './global-position-strategy'; |
| 15 | + |
| 16 | + |
| 17 | +export function main() { |
| 18 | + describe('GlobalPositonStrategy', () => { |
| 19 | + BrowserDomAdapter.makeCurrent(); |
| 20 | + let element: HTMLElement; |
| 21 | + let strategy: GlobalPositionStrategy; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + element = DOM.createElement('div'); |
| 25 | + strategy = new GlobalPositionStrategy(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should set explicit (top, left) position to the element', fakeAsyncTest(() => { |
| 29 | + strategy.top('10px').left('40%').apply(element); |
| 30 | + |
| 31 | + flushMicrotasks(); |
| 32 | + |
| 33 | + expect(element.style.top).toBe('10px'); |
| 34 | + expect(element.style.left).toBe('40%'); |
| 35 | + expect(element.style.bottom).toBe(''); |
| 36 | + expect(element.style.right).toBe(''); |
| 37 | + })); |
| 38 | + |
| 39 | + it('should set explicit (bottom, right) position to the element', fakeAsyncTest(() => { |
| 40 | + strategy.bottom('70px').right('15em').apply(element); |
| 41 | + |
| 42 | + flushMicrotasks(); |
| 43 | + |
| 44 | + expect(element.style.top).toBe(''); |
| 45 | + expect(element.style.left).toBe(''); |
| 46 | + expect(element.style.bottom).toBe('70px'); |
| 47 | + expect(element.style.right).toBe('15em'); |
| 48 | + })); |
| 49 | + |
| 50 | + it('should overwrite previously applied positioning', fakeAsyncTest(() => { |
| 51 | + strategy.centerHorizontally().centerVertically().apply(element); |
| 52 | + flushMicrotasks(); |
| 53 | + |
| 54 | + strategy.top('10px').left('40%').apply(element); |
| 55 | + flushMicrotasks(); |
| 56 | + |
| 57 | + expect(element.style.top).toBe('10px'); |
| 58 | + expect(element.style.left).toBe('40%'); |
| 59 | + expect(element.style.bottom).toBe(''); |
| 60 | + expect(element.style.right).toBe(''); |
| 61 | + expect(element.style.transform).not.toContain('translate'); |
| 62 | + |
| 63 | + strategy.bottom('70px').right('15em').apply(element); |
| 64 | + |
| 65 | + flushMicrotasks(); |
| 66 | + |
| 67 | + expect(element.style.top).toBe(''); |
| 68 | + expect(element.style.left).toBe(''); |
| 69 | + expect(element.style.bottom).toBe('70px'); |
| 70 | + expect(element.style.right).toBe('15em'); |
| 71 | + expect(element.style.transform).not.toContain('translate'); |
| 72 | + })); |
| 73 | + |
| 74 | + it('should center the element', fakeAsyncTest(() => { |
| 75 | + strategy.centerHorizontally().centerVertically().apply(element); |
| 76 | + |
| 77 | + flushMicrotasks(); |
| 78 | + |
| 79 | + expect(element.style.top).toBe('50%'); |
| 80 | + expect(element.style.left).toBe('50%'); |
| 81 | + expect(element.style.transform).toContain('translateX(-50%)'); |
| 82 | + expect(element.style.transform).toContain('translateY(-50%)'); |
| 83 | + })); |
| 84 | + |
| 85 | + it('should center the element with an offset', fakeAsyncTest(() => { |
| 86 | + strategy.centerHorizontally('10px').centerVertically('15px').apply(element); |
| 87 | + |
| 88 | + flushMicrotasks(); |
| 89 | + |
| 90 | + expect(element.style.top).toBe('50%'); |
| 91 | + expect(element.style.left).toBe('50%'); |
| 92 | + expect(element.style.transform).toContain('translateX(-50%)'); |
| 93 | + expect(element.style.transform).toContain('translateX(10px)'); |
| 94 | + expect(element.style.transform).toContain('translateY(-50%)'); |
| 95 | + expect(element.style.transform).toContain('translateY(15px)'); |
| 96 | + })); |
| 97 | + |
| 98 | + it('should default the element to position: absolute', fakeAsyncTest(() => { |
| 99 | + strategy.apply(element); |
| 100 | + |
| 101 | + flushMicrotasks(); |
| 102 | + |
| 103 | + expect(element.style.position).toBe('absolute'); |
| 104 | + })); |
| 105 | + |
| 106 | + it('should make the element position: fixed', fakeAsyncTest(() => { |
| 107 | + strategy.fixed().apply(element); |
| 108 | + |
| 109 | + flushMicrotasks(); |
| 110 | + |
| 111 | + expect(element.style.position).toBe('fixed'); |
| 112 | + })); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +function fakeAsyncTest(fn: () => void) { |
| 117 | + return inject([], fakeAsync(fn)); |
| 118 | +} |
0 commit comments