|
| 1 | +import {AfterViewInit, Component, ElementRef, ViewChild} from '@angular/core'; |
| 2 | +import {async, ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing'; |
| 3 | +import {A11yModule, ConfigurableFocusTrapFactory, ConfigurableFocusTrap} from '../index'; |
| 4 | + |
| 5 | + |
| 6 | +describe('ConfigurableFocusTrap', () => { |
| 7 | + |
| 8 | + beforeEach(async(() => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + imports: [A11yModule], |
| 11 | + declarations: [ |
| 12 | + NestedFocusTraps, |
| 13 | + SimpleFocusTrap, |
| 14 | + ], |
| 15 | + }); |
| 16 | + |
| 17 | + TestBed.compileComponents(); |
| 18 | + })); |
| 19 | + |
| 20 | + describe('with EventListenerFocusTrapInertStrategy', () => { |
| 21 | + |
| 22 | + it('refocuses the first FocusTrap element when focus moves outside the FocusTrap', |
| 23 | + fakeAsync(() => { |
| 24 | + const fixture = TestBed.createComponent(SimpleFocusTrap); |
| 25 | + const componentInstance = fixture.componentInstance; |
| 26 | + fixture.detectChanges(); |
| 27 | + |
| 28 | + expect(document.activeElement).toBe(document.body, 'Expected body to be focused'); |
| 29 | + |
| 30 | + componentInstance.focusTrap.focusFirstTabbableElementWhenReady(); |
| 31 | + |
| 32 | + expect(document.activeElement).toBe( |
| 33 | + componentInstance.firstFocusableElement.nativeElement, |
| 34 | + 'Expected first focusable element to be focused'); |
| 35 | + |
| 36 | + componentInstance.outsideFocusableElement.nativeElement.focus(); |
| 37 | + flush(); |
| 38 | + |
| 39 | + expect(document.activeElement).toBe( |
| 40 | + componentInstance.firstFocusableElement.nativeElement, |
| 41 | + 'Expected first focusable element to be focused'); |
| 42 | + })); |
| 43 | + |
| 44 | + it('does not intercept focus when focus moves to another element in the FocusTrap', |
| 45 | + fakeAsync(() => { |
| 46 | + const fixture = TestBed.createComponent(SimpleFocusTrap); |
| 47 | + const componentInstance = fixture.componentInstance; |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(document.activeElement).toBe(document.body, 'Expected body to be focused'); |
| 51 | + |
| 52 | + componentInstance.focusTrap.focusFirstTabbableElementWhenReady(); |
| 53 | + |
| 54 | + expect(document.activeElement).toBe( |
| 55 | + componentInstance.firstFocusableElement.nativeElement, |
| 56 | + 'Expected first focusable element to be focused'); |
| 57 | + |
| 58 | + componentInstance.secondFocusableElement.nativeElement.focus(); |
| 59 | + flush(); |
| 60 | + |
| 61 | + expect(document.activeElement).toBe( |
| 62 | + componentInstance.secondFocusableElement.nativeElement, |
| 63 | + 'Expected second focusable element to be focused'); |
| 64 | + })); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('with nested FocusTraps', () => { |
| 68 | + it('traps focus in the most recently enabled FocusTrap', |
| 69 | + fakeAsync(() => { |
| 70 | + const fixture = TestBed.createComponent(NestedFocusTraps); |
| 71 | + const componentInstance = fixture.componentInstance; |
| 72 | + fixture.detectChanges(); |
| 73 | + |
| 74 | + componentInstance.outerFocusTrap.enabled = true; |
| 75 | + componentInstance.innerFocusTrap.enabled = true; |
| 76 | + |
| 77 | + componentInstance.outsideFocusableElement.nativeElement.focus(); |
| 78 | + flush(); |
| 79 | + expect(document.activeElement).toBe( |
| 80 | + componentInstance.firstFocusableInnerElement.nativeElement, |
| 81 | + 'Expected first focusable inner element to be focused'); |
| 82 | + |
| 83 | + componentInstance.firstFocusableOuterElement.nativeElement.focus(); |
| 84 | + flush(); |
| 85 | + |
| 86 | + expect(document.activeElement).toBe( |
| 87 | + componentInstance.firstFocusableInnerElement.nativeElement, |
| 88 | + 'Expected first focusable inner element to be focused'); |
| 89 | + })); |
| 90 | + |
| 91 | + it(`traps focus in the second most recently enabled FocusTrap when |
| 92 | + the active FocusTrap is disabled`, fakeAsync(() => { |
| 93 | + const fixture = TestBed.createComponent(NestedFocusTraps); |
| 94 | + const componentInstance = fixture.componentInstance; |
| 95 | + fixture.detectChanges(); |
| 96 | + |
| 97 | + componentInstance.outerFocusTrap.enabled = true; |
| 98 | + componentInstance.innerFocusTrap.enabled = true; |
| 99 | + componentInstance.innerFocusTrap.enabled = false; |
| 100 | + |
| 101 | + componentInstance.outsideFocusableElement.nativeElement.focus(); |
| 102 | + flush(); |
| 103 | + expect(document.activeElement).toBe( |
| 104 | + componentInstance.firstFocusableOuterElement.nativeElement, |
| 105 | + 'Expected first focusable outer element to be focused'); |
| 106 | + |
| 107 | + componentInstance.secondFocusableInnerElement.nativeElement.focus(); |
| 108 | + flush(); |
| 109 | + |
| 110 | + expect(document.activeElement).toBe( |
| 111 | + componentInstance.secondFocusableInnerElement.nativeElement, |
| 112 | + 'Expected second focusable inner element to be focused'); |
| 113 | + })); |
| 114 | + |
| 115 | + it('stops trapping focus when all FocusTraps are disabled', |
| 116 | + fakeAsync(() => { |
| 117 | + const fixture = TestBed.createComponent(NestedFocusTraps); |
| 118 | + const componentInstance = fixture.componentInstance; |
| 119 | + fixture.detectChanges(); |
| 120 | + |
| 121 | + componentInstance.outerFocusTrap.enabled = true; |
| 122 | + componentInstance.innerFocusTrap.enabled = true; |
| 123 | + componentInstance.outerFocusTrap.enabled = false; |
| 124 | + componentInstance.innerFocusTrap.enabled = false; |
| 125 | + |
| 126 | + componentInstance.outsideFocusableElement.nativeElement.focus(); |
| 127 | + flush(); |
| 128 | + expect(document.activeElement).toBe( |
| 129 | + componentInstance.outsideFocusableElement.nativeElement, |
| 130 | + 'Expected outside element to be focused'); |
| 131 | + })); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | + |
| 136 | +@Component({ |
| 137 | + template: ` |
| 138 | + <textarea #outsideFocusable></textarea> |
| 139 | + <div #focusTrapElement> |
| 140 | + <input #firstFocusable> |
| 141 | + <button #secondFocusable>SAVE</button> |
| 142 | + </div> |
| 143 | + ` |
| 144 | +}) |
| 145 | +class SimpleFocusTrap implements AfterViewInit { |
| 146 | + @ViewChild('focusTrapElement') focusTrapElement!: ElementRef; |
| 147 | + @ViewChild('outsideFocusable') outsideFocusableElement!: ElementRef; |
| 148 | + @ViewChild('firstFocusable') firstFocusableElement!: ElementRef; |
| 149 | + @ViewChild('secondFocusable') secondFocusableElement!: ElementRef; |
| 150 | + |
| 151 | + focusTrap: ConfigurableFocusTrap; |
| 152 | + |
| 153 | + constructor(private _focusTrapFactory: ConfigurableFocusTrapFactory) { |
| 154 | + } |
| 155 | + |
| 156 | + ngAfterViewInit() { |
| 157 | + this.focusTrap = this._focusTrapFactory.create(this.focusTrapElement.nativeElement); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +@Component({ |
| 162 | + template: ` |
| 163 | + <a #outsideFocusable href="www.google.com">link</a> |
| 164 | + <div #outerFocusTrapElement> |
| 165 | + <textarea #firstFocusableOuter></textarea> |
| 166 | + <div #innerFocusTrapElement> |
| 167 | + <input #firstFocusableInner> |
| 168 | + <button #secondFocusableInner>SAVE</button> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + ` |
| 172 | +}) |
| 173 | +class NestedFocusTraps implements AfterViewInit { |
| 174 | + @ViewChild('outerFocusTrapElement') outerFocusTrapElement!: ElementRef; |
| 175 | + @ViewChild('innerFocusTrapElement') innerFocusTrapElement!: ElementRef; |
| 176 | + @ViewChild('outsideFocusable') outsideFocusableElement!: ElementRef; |
| 177 | + @ViewChild('firstFocusableOuter') firstFocusableOuterElement!: ElementRef; |
| 178 | + @ViewChild('firstFocusableInner') firstFocusableInnerElement!: ElementRef; |
| 179 | + @ViewChild('secondFocusableInner') secondFocusableInnerElement!: ElementRef; |
| 180 | + |
| 181 | + outerFocusTrap: ConfigurableFocusTrap; |
| 182 | + innerFocusTrap: ConfigurableFocusTrap; |
| 183 | + |
| 184 | + constructor(private _focusTrapFactory: ConfigurableFocusTrapFactory) { |
| 185 | + } |
| 186 | + |
| 187 | + ngAfterViewInit() { |
| 188 | + this.outerFocusTrap = this._focusTrapFactory.create(this.outerFocusTrapElement.nativeElement); |
| 189 | + this.outerFocusTrap.enabled = false; |
| 190 | + this.innerFocusTrap = this._focusTrapFactory.create(this.innerFocusTrapElement.nativeElement); |
| 191 | + this.innerFocusTrap.enabled = false; |
| 192 | + } |
| 193 | +} |
0 commit comments