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