@@ -7,24 +7,24 @@ import {Platform} from '../platform/platform';
7
7
8
8
describe ( 'FocusTrap' , ( ) => {
9
9
10
- describe ( 'with default element' , ( ) => {
11
-
12
- let fixture : ComponentFixture < FocusTrapTestApp > ;
13
- let focusTrapInstance : FocusTrap ;
14
- let platform : Platform = new Platform ( ) ;
10
+ beforeEach ( async ( ( ) => {
11
+ TestBed . configureTestingModule ( {
12
+ declarations : [ FocusTrapDirective , FocusTrapWithBindings , SimpleFocusTrap , FocusTrapTargets ] ,
13
+ providers : [ InteractivityChecker , Platform , FocusTrapFactory ]
14
+ } ) ;
15
15
16
- beforeEach ( async ( ( ) => {
17
- TestBed . configureTestingModule ( {
18
- declarations : [ FocusTrapDirective , FocusTrapTestApp ] ,
19
- providers : [ InteractivityChecker , Platform , FocusTrapFactory ]
20
- } ) ;
16
+ TestBed . compileComponents ( ) ;
17
+ } ) ) ;
21
18
22
- TestBed . compileComponents ( ) ;
19
+ describe ( 'with default element' , ( ) => {
20
+ let fixture : ComponentFixture < SimpleFocusTrap > ;
21
+ let focusTrapInstance : FocusTrap ;
23
22
24
- fixture = TestBed . createComponent ( FocusTrapTestApp ) ;
23
+ beforeEach ( ( ) => {
24
+ fixture = TestBed . createComponent ( SimpleFocusTrap ) ;
25
25
fixture . detectChanges ( ) ;
26
26
focusTrapInstance = fixture . componentInstance . focusTrapDirective . focusTrap ;
27
- } ) ) ;
27
+ } ) ;
28
28
29
29
it ( 'wrap focus from end to start' , ( ) => {
30
30
// Because we can't mimic a real tab press focus change in a unit test, just call the
@@ -41,12 +41,28 @@ describe('FocusTrap', () => {
41
41
focusTrapInstance . focusLastTabbableElement ( ) ;
42
42
43
43
// In iOS button elements are never tabbable, so the last element will be the input.
44
- let lastElement = platform . IOS ? 'input' : 'button' ;
44
+ let lastElement = new Platform ( ) . IOS ? 'input' : 'button' ;
45
45
46
46
expect ( document . activeElement . nodeName . toLowerCase ( ) )
47
47
. toBe ( lastElement , `Expected ${ lastElement } element to be focused` ) ;
48
48
} ) ;
49
49
50
+ it ( 'should be enabled by default' , ( ) => {
51
+ expect ( focusTrapInstance . enabled ) . toBe ( true ) ;
52
+ } ) ;
53
+
54
+ } ) ;
55
+
56
+ describe ( 'with bindings' , ( ) => {
57
+ let fixture : ComponentFixture < FocusTrapWithBindings > ;
58
+ let focusTrapInstance : FocusTrap ;
59
+
60
+ beforeEach ( ( ) => {
61
+ fixture = TestBed . createComponent ( FocusTrapWithBindings ) ;
62
+ fixture . detectChanges ( ) ;
63
+ focusTrapInstance = fixture . componentInstance . focusTrapDirective . focusTrap ;
64
+ } ) ;
65
+
50
66
it ( 'should clean up its anchor sibling elements on destroy' , ( ) => {
51
67
const rootElement = fixture . debugElement . nativeElement as HTMLElement ;
52
68
@@ -73,21 +89,14 @@ describe('FocusTrap', () => {
73
89
} ) ;
74
90
75
91
describe ( 'with focus targets' , ( ) => {
76
- let fixture : ComponentFixture < FocusTrapTargetTestApp > ;
92
+ let fixture : ComponentFixture < FocusTrapTargets > ;
77
93
let focusTrapInstance : FocusTrap ;
78
94
79
- beforeEach ( async ( ( ) => {
80
- TestBed . configureTestingModule ( {
81
- declarations : [ FocusTrapDirective , FocusTrapTargetTestApp ] ,
82
- providers : [ InteractivityChecker , Platform , FocusTrapFactory ]
83
- } ) ;
84
-
85
- TestBed . compileComponents ( ) ;
86
-
87
- fixture = TestBed . createComponent ( FocusTrapTargetTestApp ) ;
95
+ beforeEach ( ( ) => {
96
+ fixture = TestBed . createComponent ( FocusTrapTargets ) ;
88
97
fixture . detectChanges ( ) ;
89
98
focusTrapInstance = fixture . componentInstance . focusTrapDirective . focusTrap ;
90
- } ) ) ;
99
+ } ) ;
91
100
92
101
it ( 'should be able to prioritize the first focus target' , ( ) => {
93
102
// Because we can't mimic a real tab press focus change in a unit test, just call the
@@ -106,6 +115,19 @@ describe('FocusTrap', () => {
106
115
} ) ;
107
116
108
117
118
+ @Component ( {
119
+ template : `
120
+ <div cdkTrapFocus>
121
+ <input>
122
+ <button>SAVE</button>
123
+ </div>
124
+ `
125
+ } )
126
+ class SimpleFocusTrap {
127
+ @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
128
+ }
129
+
130
+
109
131
@Component ( {
110
132
template : `
111
133
<div *ngIf="renderFocusTrap" [cdkTrapFocus]="isFocusTrapEnabled">
@@ -114,7 +136,7 @@ describe('FocusTrap', () => {
114
136
</div>
115
137
`
116
138
} )
117
- class FocusTrapTestApp {
139
+ class FocusTrapWithBindings {
118
140
@ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
119
141
renderFocusTrap = true ;
120
142
isFocusTrapEnabled = true ;
@@ -131,6 +153,6 @@ class FocusTrapTestApp {
131
153
</div>
132
154
`
133
155
} )
134
- class FocusTrapTargetTestApp {
156
+ class FocusTrapTargets {
135
157
@ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
136
158
}
0 commit comments