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