|
| 1 | +import { |
| 2 | + inject, |
| 3 | + TestComponentBuilder, |
| 4 | + ComponentFixture, |
| 5 | + fakeAsync, |
| 6 | + flushMicrotasks, |
| 7 | + tick, beforeEachProviders |
| 8 | +} from 'angular2/testing'; |
| 9 | +import { |
| 10 | + it, |
| 11 | + describe, |
| 12 | + expect, |
| 13 | + beforeEach, |
| 14 | +} from '../../core/facade/testing'; |
| 15 | +import {Component} from 'angular2/core'; |
| 16 | +import {By} from 'angular2/platform/browser'; |
| 17 | +import {MdLiveAnnouncer} from './live-announcer'; |
| 18 | +import {DOM} from '../platform/dom/dom_adapter'; |
| 19 | + |
| 20 | +export function main() { |
| 21 | + describe('MdLiveAnnouncer', () => { |
| 22 | + let live: MdLiveAnnouncer; |
| 23 | + let builder: TestComponentBuilder; |
| 24 | + let announcerEl: Element; |
| 25 | + |
| 26 | + beforeEachProviders(() => [MdLiveAnnouncer]); |
| 27 | + |
| 28 | + beforeEach(inject([TestComponentBuilder, MdLiveAnnouncer], |
| 29 | + (tcb: TestComponentBuilder, _live: MdLiveAnnouncer) => { |
| 30 | + builder = tcb; |
| 31 | + live = _live; |
| 32 | + announcerEl = queryAnnouncerElement(); |
| 33 | + })); |
| 34 | + |
| 35 | + it('should correctly update the announce text', fakeAsyncTest(() => { |
| 36 | + let appFixture: ComponentFixture = null; |
| 37 | + |
| 38 | + builder.createAsync(TestApp).then(fixture => { |
| 39 | + appFixture = fixture; |
| 40 | + }); |
| 41 | + |
| 42 | + flushMicrotasks(); |
| 43 | + |
| 44 | + let buttonElement = appFixture.debugElement |
| 45 | + .query(By.css('button')).nativeElement; |
| 46 | + |
| 47 | + buttonElement.click(); |
| 48 | + |
| 49 | + // This flushes our 100ms timeout for the screenreaders. |
| 50 | + tick(100); |
| 51 | + |
| 52 | + expect(announcerEl.textContent).toBe('Test'); |
| 53 | + })); |
| 54 | + |
| 55 | + it('should correctly update the politeness attribute', fakeAsyncTest(() => { |
| 56 | + let appFixture: ComponentFixture = null; |
| 57 | + |
| 58 | + builder.createAsync(TestApp).then(fixture => { |
| 59 | + appFixture = fixture; |
| 60 | + }); |
| 61 | + |
| 62 | + flushMicrotasks(); |
| 63 | + |
| 64 | + live.announce('Hey Google', 'assertive'); |
| 65 | + |
| 66 | + // This flushes our 100ms timeout for the screenreaders. |
| 67 | + tick(100); |
| 68 | + |
| 69 | + expect(announcerEl.textContent).toBe('Hey Google'); |
| 70 | + expect(announcerEl.getAttribute('aria-live')).toBe('assertive'); |
| 71 | + })); |
| 72 | + |
| 73 | + it('should apply the aria-live value polite by default', fakeAsyncTest(() => { |
| 74 | + let appFixture: ComponentFixture = null; |
| 75 | + |
| 76 | + builder.createAsync(TestApp).then(fixture => { |
| 77 | + appFixture = fixture; |
| 78 | + }); |
| 79 | + |
| 80 | + flushMicrotasks(); |
| 81 | + |
| 82 | + live.announce('Hey Google'); |
| 83 | + |
| 84 | + // This flushes our 100ms timeout for the screenreaders. |
| 85 | + tick(100); |
| 86 | + |
| 87 | + expect(announcerEl.textContent).toBe('Hey Google'); |
| 88 | + expect(announcerEl.getAttribute('aria-live')).toBe('polite'); |
| 89 | + })); |
| 90 | + |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function fakeAsyncTest(fn: () => void) { |
| 95 | + return inject([], fakeAsync(fn)); |
| 96 | +} |
| 97 | + |
| 98 | +function queryAnnouncerElement() { |
| 99 | + let bodyEl = DOM.getGlobalEventTarget('body'); |
| 100 | + return DOM.querySelector(bodyEl, '.md-live-announcer'); |
| 101 | +} |
| 102 | + |
| 103 | +@Component({ |
| 104 | + selector: 'test-app', |
| 105 | + template: `<button (click)="announceText('Test')">Announce</button>`, |
| 106 | + providers: [MdLiveAnnouncer], |
| 107 | +}) |
| 108 | +class TestApp { |
| 109 | + |
| 110 | + constructor(private live: MdLiveAnnouncer) {}; |
| 111 | + |
| 112 | + announceText(message: string) { |
| 113 | + this.live.announce(message); |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | + |
0 commit comments