|
| 1 | +import {async, TestBed, ComponentFixture} from '@angular/core/testing'; |
| 2 | +import {ViewChild, Component, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; |
| 3 | +import {MaterialModule} from '@angular/material'; |
| 4 | + |
| 5 | +import {FocusElementDirective} from './focus-element'; |
| 6 | + |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'test-component', |
| 10 | + template: '<h1 focusElement>Whoa!</h1>' |
| 11 | +}) |
| 12 | +class TestComponent { |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +describe('FocusElementDirective', () => { |
| 17 | + let fixture: ComponentFixture<TestComponent>; |
| 18 | + |
| 19 | + beforeEach(async(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 22 | + declarations: [FocusElementDirective, TestComponent] |
| 23 | + }); |
| 24 | + |
| 25 | + fixture = TestBed.createComponent(TestComponent); |
| 26 | + })); |
| 27 | + |
| 28 | + it('should set the tabindex of the host component to 0', () => { |
| 29 | + fixture.detectChanges(); |
| 30 | + const tabindex = fixture |
| 31 | + .nativeElement |
| 32 | + .querySelector('h1') |
| 33 | + .getAttribute('tabindex'); |
| 34 | + expect(tabindex).toEqual('0'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should call focus to the host components nativeElement', () => { |
| 38 | + const el = fixture.nativeElement.querySelector('h1'); |
| 39 | + spyOn(el, 'focus'); |
| 40 | + fixture.detectChanges(); |
| 41 | + expect(el).not.toBeNull(); |
| 42 | + expect(el.focus).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | +}); |
0 commit comments