1
1
import { Platform } from '@angular/cdk/platform' ;
2
2
import { Component , ViewChild } from '@angular/core' ;
3
3
import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
4
- import { FocusTrap , FocusTrapDirective , FocusTrapFactory } from './focus-trap' ;
4
+ import { FocusTrap , CdkTrapFocus , FocusTrapFactory } from './focus-trap' ;
5
5
import { InteractivityChecker } from './interactivity-checker' ;
6
6
7
7
@@ -10,12 +10,13 @@ describe('FocusTrap', () => {
10
10
beforeEach ( async ( ( ) => {
11
11
TestBed . configureTestingModule ( {
12
12
declarations : [
13
- FocusTrapDirective ,
13
+ CdkTrapFocus ,
14
14
FocusTrapWithBindings ,
15
15
SimpleFocusTrap ,
16
16
FocusTrapTargets ,
17
17
FocusTrapWithSvg ,
18
18
FocusTrapWithoutFocusableElements ,
19
+ FocusTrapWithAutoCapture ,
19
20
] ,
20
21
providers : [ InteractivityChecker , Platform , FocusTrapFactory ]
21
22
} ) ;
@@ -154,6 +155,27 @@ describe('FocusTrap', () => {
154
155
expect ( ( ) => focusTrapInstance . focusLastTabbableElement ( ) ) . not . toThrow ( ) ;
155
156
} ) ;
156
157
} ) ;
158
+
159
+ describe ( 'with autoCapture' , ( ) => {
160
+ it ( 'should automatically capture and return focus on init / destroy' , async ( ( ) => {
161
+ const fixture = TestBed . createComponent ( FocusTrapWithAutoCapture ) ;
162
+ fixture . detectChanges ( ) ;
163
+
164
+ const buttonOutsideTrappedRegion = fixture . nativeElement . querySelector ( 'button' ) ;
165
+ buttonOutsideTrappedRegion . focus ( ) ;
166
+ expect ( document . activeElement ) . toBe ( buttonOutsideTrappedRegion ) ;
167
+
168
+ fixture . componentInstance . showTrappedRegion = true ;
169
+ fixture . detectChanges ( ) ;
170
+
171
+ fixture . whenStable ( ) . then ( ( ) => {
172
+ expect ( document . activeElement . id ) . toBe ( 'auto-capture-target' ) ;
173
+
174
+ fixture . destroy ( ) ;
175
+ expect ( document . activeElement ) . toBe ( buttonOutsideTrappedRegion ) ;
176
+ } ) ;
177
+ } ) ) ;
178
+ } ) ;
157
179
} ) ;
158
180
159
181
@@ -166,7 +188,21 @@ describe('FocusTrap', () => {
166
188
`
167
189
} )
168
190
class SimpleFocusTrap {
169
- @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
191
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
192
+ }
193
+
194
+ @Component ( {
195
+ template : `
196
+ <button type="button">Toggle</button>
197
+ <div *ngIf="showTrappedRegion" cdkTrapFocus cdkTrapFocusAutoCapture>
198
+ <input id="auto-capture-target">
199
+ <button>SAVE</button>
200
+ </div>
201
+ `
202
+ } )
203
+ class FocusTrapWithAutoCapture {
204
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
205
+ showTrappedRegion = false ;
170
206
}
171
207
172
208
@@ -179,7 +215,7 @@ class SimpleFocusTrap {
179
215
`
180
216
} )
181
217
class FocusTrapWithBindings {
182
- @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
218
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
183
219
renderFocusTrap = true ;
184
220
_isFocusTrapEnabled = true ;
185
221
}
@@ -199,7 +235,7 @@ class FocusTrapWithBindings {
199
235
`
200
236
} )
201
237
class FocusTrapTargets {
202
- @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
238
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
203
239
}
204
240
205
241
@@ -213,7 +249,7 @@ class FocusTrapTargets {
213
249
`
214
250
} )
215
251
class FocusTrapWithSvg {
216
- @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
252
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
217
253
}
218
254
219
255
@Component ( {
@@ -224,5 +260,5 @@ class FocusTrapWithSvg {
224
260
`
225
261
} )
226
262
class FocusTrapWithoutFocusableElements {
227
- @ViewChild ( FocusTrapDirective ) focusTrapDirective : FocusTrapDirective ;
263
+ @ViewChild ( CdkTrapFocus ) focusTrapDirective : CdkTrapFocus ;
228
264
}
0 commit comments