|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { IgxFocusTrapDirective, IgxFocusTrapModule } from './focus-trap.directive'; |
| 5 | + |
| 6 | +import { configureTestSuite } from '../../test-utils/configure-suite'; |
| 7 | +import { IgxCheckboxModule } from '../../checkbox/checkbox.component'; |
| 8 | +import { IgxDatePickerModule } from '../../date-picker/public_api'; |
| 9 | +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
| 10 | +import { UIInteractions } from '../../test-utils/ui-interactions.spec'; |
| 11 | + |
| 12 | +describe('igxFocusTrap', () => { |
| 13 | + configureTestSuite(); |
| 14 | + beforeAll(waitForAsync(() => { |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + declarations: [ |
| 17 | + TrapFocusTestComponent |
| 18 | + ], |
| 19 | + imports: [IgxFocusTrapModule, IgxCheckboxModule, IgxDatePickerModule, NoopAnimationsModule] |
| 20 | + }).compileComponents(); |
| 21 | + })); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + UIInteractions.clearOverlay(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should focus focusable elements on Tab key pressed', () => { |
| 28 | + const fix = TestBed.createComponent(TrapFocusTestComponent); |
| 29 | + fix.detectChanges(); |
| 30 | + |
| 31 | + const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective)); |
| 32 | + const button = fix.debugElement.query(By.css('button')); |
| 33 | + const inputs = fix.debugElement.queryAll(By.css('input')); |
| 34 | + |
| 35 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 36 | + fix.detectChanges(); |
| 37 | + expect(document.activeElement).toEqual(inputs[0].nativeElement); |
| 38 | + |
| 39 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 40 | + fix.detectChanges(); |
| 41 | + expect(document.activeElement).toEqual(inputs[1].nativeElement); |
| 42 | + |
| 43 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 44 | + fix.detectChanges(); |
| 45 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 46 | + |
| 47 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 48 | + fix.detectChanges(); |
| 49 | + expect(document.activeElement).toEqual(inputs[0].nativeElement); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should focus focusable elements in reversed order on Shift + Tab key pressed', () => { |
| 53 | + const fix = TestBed.createComponent(TrapFocusTestComponent); |
| 54 | + fix.detectChanges(); |
| 55 | + |
| 56 | + const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective)); |
| 57 | + const button = fix.debugElement.query(By.css('button')); |
| 58 | + const inputs = fix.debugElement.queryAll(By.css('input')); |
| 59 | + |
| 60 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 61 | + fix.detectChanges(); |
| 62 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 63 | + |
| 64 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 65 | + fix.detectChanges(); |
| 66 | + expect(document.activeElement).toEqual(inputs[1].nativeElement); |
| 67 | + |
| 68 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 69 | + fix.detectChanges(); |
| 70 | + expect(document.activeElement).toEqual(inputs[0].nativeElement); |
| 71 | + |
| 72 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 73 | + fix.detectChanges(); |
| 74 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should trap focus on element when there is only one focusable element', () => { |
| 78 | + const fix = TestBed.createComponent(TrapFocusTestComponent); |
| 79 | + fix.detectChanges(); |
| 80 | + |
| 81 | + fix.componentInstance.showInput = false; |
| 82 | + fix.detectChanges(); |
| 83 | + |
| 84 | + const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective)); |
| 85 | + const button = fix.debugElement.query(By.css('button')); |
| 86 | + |
| 87 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 88 | + fix.detectChanges(); |
| 89 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 90 | + |
| 91 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 92 | + fix.detectChanges(); |
| 93 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 94 | + |
| 95 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 96 | + fix.detectChanges(); |
| 97 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should trap focus on element with non-focusable elements', fakeAsync(() => { |
| 101 | + const fix = TestBed.createComponent(TrapFocusTestComponent); |
| 102 | + fix.detectChanges(); |
| 103 | + |
| 104 | + fix.componentInstance.showInput = false; |
| 105 | + fix.componentInstance.showButton = false; |
| 106 | + fix.detectChanges(); |
| 107 | + |
| 108 | + const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective)); |
| 109 | + |
| 110 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 111 | + tick(); |
| 112 | + fix.detectChanges(); |
| 113 | + expect(document.activeElement).toEqual(focusTrap.nativeElement); |
| 114 | + |
| 115 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 116 | + tick(); |
| 117 | + fix.detectChanges(); |
| 118 | + expect(document.activeElement).toEqual(focusTrap.nativeElement); |
| 119 | + |
| 120 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 121 | + tick(); |
| 122 | + fix.detectChanges(); |
| 123 | + expect(document.activeElement).toEqual(focusTrap.nativeElement); |
| 124 | + })); |
| 125 | + |
| 126 | + it('should be able to set focusTrap dynamically', fakeAsync(() => { |
| 127 | + const fix = TestBed.createComponent(TrapFocusTestComponent); |
| 128 | + fix.detectChanges(); |
| 129 | + |
| 130 | + const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective)); |
| 131 | + const button = fix.debugElement.query(By.css('button')); |
| 132 | + const inputs = fix.debugElement.queryAll(By.css('input')); |
| 133 | + |
| 134 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 135 | + fix.detectChanges(); |
| 136 | + expect(document.activeElement).toEqual(inputs[0].nativeElement); |
| 137 | + |
| 138 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 139 | + fix.detectChanges(); |
| 140 | + expect(document.activeElement).toEqual(inputs[1].nativeElement); |
| 141 | + |
| 142 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 143 | + fix.detectChanges(); |
| 144 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 145 | + |
| 146 | + button.nativeElement.blur(); |
| 147 | + fix.detectChanges(); |
| 148 | + |
| 149 | + fix.componentInstance.focusTrap = false; |
| 150 | + fix.detectChanges(); |
| 151 | + |
| 152 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 153 | + fix.detectChanges(); |
| 154 | + expect(document.activeElement).not.toEqual(inputs[0].nativeElement); |
| 155 | + |
| 156 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 157 | + fix.detectChanges(); |
| 158 | + expect(document.activeElement).not.toEqual(inputs[1].nativeElement); |
| 159 | + |
| 160 | + fix.componentInstance.focusTrap = true; |
| 161 | + fix.detectChanges(); |
| 162 | + |
| 163 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap); |
| 164 | + fix.detectChanges(); |
| 165 | + expect(document.activeElement).toEqual(inputs[0].nativeElement); |
| 166 | + |
| 167 | + UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap, false, true); |
| 168 | + fix.detectChanges(); |
| 169 | + expect(document.activeElement).toEqual(button.nativeElement); |
| 170 | + })); |
| 171 | +}); |
| 172 | + |
| 173 | + |
| 174 | +@Component({ |
| 175 | + template: `<div #wrapper [igxFocusTrap]="focusTrap" tabindex="0"> |
| 176 | + <label for="uname"><b>Username</b></label><br> |
| 177 | + <input type="text" *ngIf="showInput" placeholder="Enter Username" name="uname"><br> |
| 178 | + <label for="psw"><b>Password</b></label><br> |
| 179 | + <input type="password" *ngIf="showInput" placeholder="Enter Password" name="psw"><br> |
| 180 | + <button *ngIf="showButton">SIGN IN</button> |
| 181 | + </div>` }) |
| 182 | +class TrapFocusTestComponent { |
| 183 | + public showInput = true; |
| 184 | + public showButton = true; |
| 185 | + public focusTrap = true; |
| 186 | +} |
0 commit comments