@@ -6,24 +6,21 @@ import {InteractivityChecker} from './interactivity-checker';
6
6
7
7
8
8
describe ( 'FocusTrap' , ( ) => {
9
- let checker : InteractivityChecker ;
10
- let fixture : ComponentFixture < FocusTrapTestApp > ;
11
-
12
9
describe ( 'with default element' , ( ) => {
10
+ let fixture : ComponentFixture < FocusTrapTestApp > ;
11
+ let focusTrapInstance : FocusTrap ;
12
+
13
13
beforeEach ( ( ) => TestBed . configureTestingModule ( {
14
14
declarations : [ FocusTrap , FocusTrapTestApp ] ,
15
15
providers : [ InteractivityChecker ]
16
16
} ) ) ;
17
17
18
18
beforeEach ( inject ( [ InteractivityChecker ] , ( c : InteractivityChecker ) => {
19
- checker = c ;
20
19
fixture = TestBed . createComponent ( FocusTrapTestApp ) ;
20
+ focusTrapInstance = fixture . debugElement . query ( By . directive ( FocusTrap ) ) . componentInstance ;
21
21
} ) ) ;
22
22
23
23
it ( 'wrap focus from end to start' , ( ) => {
24
- let focusTrap = fixture . debugElement . query ( By . directive ( FocusTrap ) ) ;
25
- let focusTrapInstance = focusTrap . componentInstance as FocusTrap ;
26
-
27
24
// Because we can't mimic a real tab press focus change in a unit test, just call the
28
25
// focus event handler directly.
29
26
focusTrapInstance . focusFirstTabbableElement ( ) ;
@@ -33,9 +30,6 @@ describe('FocusTrap', () => {
33
30
} ) ;
34
31
35
32
it ( 'should wrap focus from start to end' , ( ) => {
36
- let focusTrap = fixture . debugElement . query ( By . directive ( FocusTrap ) ) ;
37
- let focusTrapInstance = focusTrap . componentInstance as FocusTrap ;
38
-
39
33
// Because we can't mimic a real tab press focus change in a unit test, just call the
40
34
// focus event handler directly.
41
35
focusTrapInstance . focusLastTabbableElement ( ) ;
@@ -44,6 +38,35 @@ describe('FocusTrap', () => {
44
38
. toBe ( 'button' , 'Expected button element to be focused' ) ;
45
39
} ) ;
46
40
} ) ;
41
+
42
+ describe ( 'with focus targets' , ( ) => {
43
+ let fixture : ComponentFixture < FocusTrapTargetTestApp > ;
44
+ let focusTrapInstance : FocusTrap ;
45
+
46
+ beforeEach ( ( ) => TestBed . configureTestingModule ( {
47
+ declarations : [ FocusTrap , FocusTrapTargetTestApp ] ,
48
+ providers : [ InteractivityChecker ]
49
+ } ) ) ;
50
+
51
+ beforeEach ( inject ( [ InteractivityChecker ] , ( c : InteractivityChecker ) => {
52
+ fixture = TestBed . createComponent ( FocusTrapTargetTestApp ) ;
53
+ focusTrapInstance = fixture . debugElement . query ( By . directive ( FocusTrap ) ) . componentInstance ;
54
+ } ) ) ;
55
+
56
+ it ( 'should be able to prioritize the first focus target' , ( ) => {
57
+ // Because we can't mimic a real tab press focus change in a unit test, just call the
58
+ // focus event handler directly.
59
+ focusTrapInstance . focusFirstTabbableElement ( ) ;
60
+ expect ( document . activeElement . id ) . toBe ( 'first' ) ;
61
+ } ) ;
62
+
63
+ it ( 'should be able to prioritize the last focus target' , ( ) => {
64
+ // Because we can't mimic a real tab press focus change in a unit test, just call the
65
+ // focus event handler directly.
66
+ focusTrapInstance . focusLastTabbableElement ( ) ;
67
+ expect ( document . activeElement . id ) . toBe ( 'last' ) ;
68
+ } ) ;
69
+ } ) ;
47
70
} ) ;
48
71
49
72
@@ -56,3 +79,16 @@ describe('FocusTrap', () => {
56
79
`
57
80
} )
58
81
class FocusTrapTestApp { }
82
+
83
+
84
+ @Component ( {
85
+ template : `
86
+ <focus-trap>
87
+ <input>
88
+ <button id="last" md-focus-end></button>
89
+ <button id="first" md-focus-start>SAVE</button>
90
+ <input>
91
+ </focus-trap>
92
+ `
93
+ } )
94
+ class FocusTrapTargetTestApp { }
0 commit comments