@@ -5,6 +5,7 @@ import { IgxFocusTrapDirective } from './focus-trap.directive';
55
66import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
77import { UIInteractions } from '../../test-utils/ui-interactions.spec' ;
8+ import { IgxTimePickerComponent } from '../../time-picker/time-picker.component' ;
89
910describe ( 'igxFocusTrap' , ( ) => {
1011 beforeEach ( waitForAsync ( ( ) => {
@@ -161,6 +162,39 @@ describe('igxFocusTrap', () => {
161162 fix . detectChanges ( ) ;
162163 expect ( document . activeElement ) . toEqual ( button . nativeElement ) ;
163164 } ) ) ;
165+
166+ it ( 'should focus only visible focusable elements on Tab key pressed' , ( ) => {
167+ const fix = TestBed . createComponent ( TrapFocusTestComponent ) ;
168+ fix . detectChanges ( ) ;
169+
170+ fix . componentInstance . showTimePicker = true ;
171+ fix . detectChanges ( ) ;
172+
173+ const focusTrap = fix . debugElement . query ( By . directive ( IgxFocusTrapDirective ) ) ;
174+ const buttons = fix . debugElement . queryAll ( By . css ( 'button' ) ) ;
175+ const inputs = fix . debugElement . queryAll ( By . css ( 'input' ) ) ;
176+ const timePickerInput = fix . debugElement . query ( By . css ( '.igx-input-group__input' ) ) ;
177+
178+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
179+ fix . detectChanges ( ) ;
180+ expect ( document . activeElement ) . toEqual ( inputs [ 0 ] . nativeElement ) ;
181+
182+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
183+ fix . detectChanges ( ) ;
184+ expect ( document . activeElement ) . toEqual ( inputs [ 1 ] . nativeElement ) ;
185+
186+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
187+ fix . detectChanges ( ) ;
188+ expect ( document . activeElement ) . toEqual ( timePickerInput . nativeElement ) ;
189+
190+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
191+ fix . detectChanges ( ) ;
192+ expect ( document . activeElement ) . toEqual ( buttons [ buttons . length - 1 ] . nativeElement ) ;
193+
194+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
195+ fix . detectChanges ( ) ;
196+ expect ( document . activeElement ) . toEqual ( inputs [ 0 ] . nativeElement ) ;
197+ } ) ;
164198} ) ;
165199
166200
@@ -177,14 +211,19 @@ describe('igxFocusTrap', () => {
177211 <input type="password" placeholder="Enter Password" name="psw">
178212 }
179213 <br>
214+ @if (showTimePicker) {
215+ <igx-time-picker> </igx-time-picker>
216+ }
217+ <br>
180218 @if (showButton) {
181219 <button>SIGN IN</button>
182220 }
183221 </div>` ,
184- imports : [ IgxFocusTrapDirective ]
222+ imports : [ IgxFocusTrapDirective , IgxTimePickerComponent ]
185223} )
186224class TrapFocusTestComponent {
187225 public showInput = true ;
188226 public showButton = true ;
189227 public focusTrap = true ;
228+ public showTimePicker = false ;
190229}
0 commit comments