|
| 1 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 2 | +import {Component} from '@angular/core'; |
| 3 | +import {StyleModule} from './index'; |
| 4 | +import {By} from '@angular/platform-browser'; |
| 5 | + |
| 6 | + |
| 7 | +describe('MdSlider', () => { |
| 8 | + beforeEach(async(() => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + imports: [StyleModule], |
| 11 | + declarations: [ |
| 12 | + ButtonWithFocusClasses, |
| 13 | + ], |
| 14 | + }); |
| 15 | + |
| 16 | + TestBed.compileComponents(); |
| 17 | + })); |
| 18 | + |
| 19 | + describe('cdkAddFocusClasses', () => { |
| 20 | + let fixture: ComponentFixture<ButtonWithFocusClasses>; |
| 21 | + let buttonElement: HTMLElement; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + fixture = TestBed.createComponent(ButtonWithFocusClasses); |
| 25 | + fixture.detectChanges(); |
| 26 | + |
| 27 | + buttonElement = fixture.debugElement.query(By.css('button')).nativeElement; |
| 28 | + }); |
| 29 | + |
| 30 | + it('should initially not be focused', () => { |
| 31 | + expect(buttonElement.classList.length).toBe(0, 'button should not have focus classes'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should detect focus via keyboard', async(() => { |
| 35 | + // Simulate focus via keyboard. |
| 36 | + dispatchKeydownEvent(document, 9 /* tab */); |
| 37 | + buttonElement.focus(); |
| 38 | + fixture.detectChanges(); |
| 39 | + |
| 40 | + setTimeout(() => { |
| 41 | + fixture.detectChanges(); |
| 42 | + |
| 43 | + expect(buttonElement.classList.length) |
| 44 | + .toBe(2, 'button should have exactly 2 focus classes'); |
| 45 | + expect(buttonElement.classList.contains('cdk-focused')) |
| 46 | + .toBe(true, 'button should have cdk-focused class'); |
| 47 | + expect(buttonElement.classList.contains('cdk-keyboard-focused')) |
| 48 | + .toBe(true, 'button should have cdk-keyboard-focused class'); |
| 49 | + }, 0); |
| 50 | + })); |
| 51 | + |
| 52 | + it('should detect focus via mouse', async(() => { |
| 53 | + // Simulate focus via mouse. |
| 54 | + dispatchMousedownEvent(document); |
| 55 | + buttonElement.focus(); |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + setTimeout(() => { |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + expect(buttonElement.classList.length) |
| 62 | + .toBe(2, 'button should have exactly 2 focus classes'); |
| 63 | + expect(buttonElement.classList.contains('cdk-focused')) |
| 64 | + .toBe(true, 'button should have cdk-focused class'); |
| 65 | + expect(buttonElement.classList.contains('cdk-mouse-focused')) |
| 66 | + .toBe(true, 'button should have cdk-mouse-focused class'); |
| 67 | + }, 0); |
| 68 | + })); |
| 69 | + |
| 70 | + it('should detect programmatic focus', async(() => { |
| 71 | + // Programmatically focus. |
| 72 | + buttonElement.focus(); |
| 73 | + fixture.detectChanges(); |
| 74 | + |
| 75 | + setTimeout(() => { |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + expect(buttonElement.classList.length) |
| 79 | + .toBe(2, 'button should have exactly 2 focus classes'); |
| 80 | + expect(buttonElement.classList.contains('cdk-focused')) |
| 81 | + .toBe(true, 'button should have cdk-focused class'); |
| 82 | + expect(buttonElement.classList.contains('cdk-programmatically-focused')) |
| 83 | + .toBe(true, 'button should have cdk-programmatically-focused class'); |
| 84 | + }, 0); |
| 85 | + })); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | + |
| 90 | +@Component({template: `<button cdkAddFocusClasses>focus me!</button>`}) |
| 91 | +class ButtonWithFocusClasses {} |
| 92 | + |
| 93 | + |
| 94 | +/** Dispatches a mousedown event on the specified element. */ |
| 95 | +function dispatchMousedownEvent(element: Node) { |
| 96 | + let event = document.createEvent('MouseEvent'); |
| 97 | + event.initMouseEvent( |
| 98 | + 'mousedown', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); |
| 99 | + element.dispatchEvent(event); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +/** Dispatches a keydown event on the specified element. */ |
| 104 | +function dispatchKeydownEvent(element: Node, keyCode: number) { |
| 105 | + let event: any = document.createEvent('KeyboardEvent'); |
| 106 | + (event.initKeyEvent || event.initKeyboardEvent).bind(event)( |
| 107 | + 'keydown', true, true, window, 0, 0, 0, 0, 0, keyCode); |
| 108 | + Object.defineProperty(event, 'keyCode', { |
| 109 | + get: function() { return keyCode; } |
| 110 | + }); |
| 111 | + element.dispatchEvent(event); |
| 112 | +} |
0 commit comments