On Github marcysutton / a11y-testing-angular2
By Marcy Sutton / @MarcySutton Senior Front-End Engineer, Deque Systems
…by making the Internet more accessible.
1 of 2
Good for:
describe('MdCheckbox', function() { var builder: TestComponentBuilder; beforeEach(inject([TestComponentBuilder], function(tcb: TestComponentBuilder) { builder = tcb; })); it('sets the "aria-labelledby" attribute to the id of the label', function(done: () => void) { builder.createAsync(CheckboxController).then(function(fixture) { fixture.detectChanges(); let el = fixture.debugElement.query(By.css('.md-checkbox')); let label = el.nativeElement.querySelector('label'); expect(el.nativeElement.getAttribute('aria-labelledby')).toEqual(label.id); }).then(done).catch(done); }); });
describe('keyboard support', function() { var fixture: ComponentFixture; var controller: MenuController; var el: DebugElement; beforeEach(function(done: () => void) { builder.createAsync(MenuController).then(function(f) { fixture = f; controller = fixture.componentInstance; fixture.detectChanges(); el = fixture.debugElement.query(By.css('.menuitem')); }).then(done).catch(done); }); it('should toggle with the spacebar', function(done: () => void) { keyEvent('keydown', el, ' ', fixture); expect(el.nativeElement.className).toContain('menuitem-active'); keyEvent('keydown', el, ' ', fixture); expect(el.nativeElement.className).not.toContain('menuitem-active'); }); }); function keyEvent(name: string, el: DebugElement, key: string, fixture: ComponentFixture): Event { var kbdEvent: Event; if (BROWSER_SUPPORTS_EVENT_CONSTRUCTORS) { kbdEvent = new KeyboardEvent(name); } else { kbdEvent = document.createEvent('Event'); kbdEvent.initEvent(name, true, true); // Hack DOM Level 3 Events "key" prop into keyboard event. Object.defineProperty(kbdEvent, 'key', { value: key, enumerable: false, writable: false, configurable: true }); spyOn(kbdEvent, 'preventDefault').and.callThrough(); spyOn(kbdEvent, 'stopPropagation').and.callThrough(); el.nativeElement.dispatchEvent(kbdEvent); fixture.detectChanges(); return kbdEvent; }Material 2 checkbox
Mocking Keyboard Events
it('toggles "aria-checked" on the host element', function() { builder.createAsync(CheckboxController).then(function(fixture) { let el = fixture.debugElement.query(By.css('.md-checkbox')); expect(el.nativeElement.getAttribute('aria-checked')).toEqual('false'); controller = fixture.componentInstance; changePromise = waitForChange(); controller.isChecked = true; fixture.detectChanges(); expect(el.nativeElement.getAttribute('aria-checked')).toEqual('true'); }); });
2 of 2
Save yourself from testing:
https://github.com/dequelabs/axe-core
†There’s also a supported/enterprise option
npm install axe-core
require('axe-core/axe.js'); declare var axe: any; ... it('should have a form label', injectAsync([ TestComponentBuilder ], (tcb: TestComponentBuilder) => { return tcb.createAsync(Home).then((componentFixture: ComponentFixture) => { const element = componentFixture.nativeElement; axe.a11yCheck(element, null, (result) => { console.log(result); expect(result.violations.length).toBe(0); }); }); }));
*CommonJS module format, SystemJS support coming