|
| 1 | +import { |
| 2 | + inject, |
| 3 | + TestComponentBuilder, ComponentFixture |
| 4 | +} from 'angular2/testing'; |
| 5 | +import { |
| 6 | + it, |
| 7 | + describe, |
| 8 | + expect, |
| 9 | + beforeEach, |
| 10 | +} from '../../core/facade/testing'; |
| 11 | +import {Component} from 'angular2/core'; |
| 12 | +import {By} from 'angular2/platform/browser'; |
| 13 | +import {MdAriaLive} from './aria-live'; |
| 14 | + |
| 15 | +export function main() { |
| 16 | + describe('MdAriaLive', () => { |
| 17 | + let builder: TestComponentBuilder; |
| 18 | + let timeoutMock: any; |
| 19 | + |
| 20 | + beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { |
| 21 | + builder = tcb; |
| 22 | + })); |
| 23 | + |
| 24 | + beforeAll(() => { |
| 25 | + timeoutMock = mockTimeout(); |
| 26 | + }); |
| 27 | + |
| 28 | + afterAll(() => { |
| 29 | + timeoutMock.restoreTimeout(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should correctly update the announce text', (done: () => void) => { |
| 33 | + return builder |
| 34 | + .createAsync(TestApp) |
| 35 | + .then((view: ComponentFixture) => { |
| 36 | + let announcerElement = view.debugElement.query(By.css('._md-aria-announcer')); |
| 37 | + let buttonElement = view.debugElement.query(By.css('button')); |
| 38 | + |
| 39 | + view.detectChanges(); |
| 40 | + |
| 41 | + buttonElement.nativeElement.click(); |
| 42 | + |
| 43 | + timeoutMock.flushTimeout(); |
| 44 | + |
| 45 | + expect(announcerElement.nativeElement.textContent).toBe('Test'); |
| 46 | + |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + function mockTimeout() { |
| 52 | + let browserTimeout = window.setTimeout; |
| 53 | + let stack: any = {}; |
| 54 | + |
| 55 | + window.setTimeout = (fn: Function, timeout: number) => { |
| 56 | + var id = browserTimeout(() => { |
| 57 | + stack[id] = null; |
| 58 | + fn(); |
| 59 | + }, timeout); |
| 60 | + |
| 61 | + stack[id] = fn; |
| 62 | + return id; |
| 63 | + }; |
| 64 | + |
| 65 | + return { |
| 66 | + flushTimeout: () => { |
| 67 | + for (let id in stack) { |
| 68 | + if (stack.hasOwnProperty(id)) { |
| 69 | + stack[id](); |
| 70 | + stack[id] = null; |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + restoreTimeout: () => { |
| 75 | + window.setTimeout = browserTimeout; |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +@Component({ |
| 85 | + selector: 'test-app', |
| 86 | + template: ` |
| 87 | + <md-aria-live #announcer> |
| 88 | + <button (click)="announcer.announce('Test')">Announce</button> |
| 89 | + </md-aria-live> |
| 90 | + `, |
| 91 | + directives: [MdAriaLive], |
| 92 | +}) |
| 93 | +class TestApp { |
| 94 | +} |
| 95 | + |
0 commit comments