-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
focus-trap.spec.ts
201 lines (161 loc) · 6.18 KB
/
focus-trap.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {Component, ViewChild} from '@angular/core';
import {FocusTrapFactory, FocusTrapDirective, FocusTrap} from './focus-trap';
import {InteractivityChecker} from './interactivity-checker';
import {Platform} from '../platform/platform';
describe('FocusTrap', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
FocusTrapDirective,
FocusTrapWithBindings,
SimpleFocusTrap,
FocusTrapTargets,
FocusTrapWithSvg,
],
providers: [InteractivityChecker, Platform, FocusTrapFactory]
});
TestBed.compileComponents();
}));
describe('with default element', () => {
let fixture: ComponentFixture<SimpleFocusTrap>;
let focusTrapInstance: FocusTrap;
beforeEach(() => {
fixture = TestBed.createComponent(SimpleFocusTrap);
fixture.detectChanges();
focusTrapInstance = fixture.componentInstance.focusTrapDirective.focusTrap;
});
it('wrap focus from end to start', () => {
// Because we can't mimic a real tab press focus change in a unit test, just call the
// focus event handler directly.
focusTrapInstance.focusFirstTabbableElement();
expect(document.activeElement.nodeName.toLowerCase())
.toBe('input', 'Expected input element to be focused');
});
it('should wrap focus from start to end', () => {
// Because we can't mimic a real tab press focus change in a unit test, just call the
// focus event handler directly.
focusTrapInstance.focusLastTabbableElement();
// In iOS button elements are never tabbable, so the last element will be the input.
let lastElement = new Platform().IOS ? 'input' : 'button';
expect(document.activeElement.nodeName.toLowerCase())
.toBe(lastElement, `Expected ${lastElement} element to be focused`);
});
it('should be enabled by default', () => {
expect(focusTrapInstance.enabled).toBe(true);
});
});
describe('with bindings', () => {
let fixture: ComponentFixture<FocusTrapWithBindings>;
let focusTrapInstance: FocusTrap;
beforeEach(() => {
fixture = TestBed.createComponent(FocusTrapWithBindings);
fixture.detectChanges();
focusTrapInstance = fixture.componentInstance.focusTrapDirective.focusTrap;
});
it('should clean up its anchor sibling elements on destroy', () => {
const rootElement = fixture.debugElement.nativeElement as HTMLElement;
expect(rootElement.querySelectorAll('div.cdk-visually-hidden').length).toBe(2);
fixture.componentInstance.renderFocusTrap = false;
fixture.detectChanges();
expect(rootElement.querySelectorAll('div.cdk-visually-hidden').length).toBe(0);
});
it('should set the appropriate tabindex on the anchors, based on the disabled state', () => {
const anchors = Array.from(
fixture.debugElement.nativeElement.querySelectorAll('div.cdk-visually-hidden')
) as HTMLElement[];
expect(anchors.every(current => current.getAttribute('tabindex') === '0')).toBe(true);
fixture.componentInstance.isFocusTrapEnabled = false;
fixture.detectChanges();
expect(anchors.every(current => current.getAttribute('tabindex') === '-1')).toBe(true);
});
});
describe('with focus targets', () => {
let fixture: ComponentFixture<FocusTrapTargets>;
let focusTrapInstance: FocusTrap;
beforeEach(() => {
fixture = TestBed.createComponent(FocusTrapTargets);
fixture.detectChanges();
focusTrapInstance = fixture.componentInstance.focusTrapDirective.focusTrap;
});
it('should be able to set initial focus target', () => {
// Because we can't mimic a real tab press focus change in a unit test, just call the
// focus event handler directly.
focusTrapInstance.focusInitialElement();
expect(document.activeElement.id).toBe('middle');
});
it('should be able to prioritize the first focus target', () => {
// Because we can't mimic a real tab press focus change in a unit test, just call the
// focus event handler directly.
focusTrapInstance.focusFirstTabbableElement();
expect(document.activeElement.id).toBe('first');
});
it('should be able to prioritize the last focus target', () => {
// Because we can't mimic a real tab press focus change in a unit test, just call the
// focus event handler directly.
focusTrapInstance.focusLastTabbableElement();
expect(document.activeElement.id).toBe('last');
});
});
describe('special cases', () => {
it('should not throw when it has a SVG child', () => {
let fixture = TestBed.createComponent(FocusTrapWithSvg);
fixture.detectChanges();
let focusTrapInstance = fixture.componentInstance.focusTrapDirective.focusTrap;
expect(() => focusTrapInstance.focusFirstTabbableElement()).not.toThrow();
expect(() => focusTrapInstance.focusLastTabbableElement()).not.toThrow();
});
});
});
@Component({
template: `
<div cdkTrapFocus>
<input>
<button>SAVE</button>
</div>
`
})
class SimpleFocusTrap {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
}
@Component({
template: `
<div *ngIf="renderFocusTrap" [cdkTrapFocus]="isFocusTrapEnabled">
<input>
<button>SAVE</button>
</div>
`
})
class FocusTrapWithBindings {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
renderFocusTrap = true;
isFocusTrapEnabled = true;
}
@Component({
template: `
<div cdkTrapFocus>
<input>
<button>before</button>
<button id="first" cdk-focus-region-start></button>
<button id="middle" cdk-focus-initial></button>
<button id="last" cdk-focus-region-end></button>
<button>after</button>
<input>
</div>
`
})
class FocusTrapTargets {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
}
@Component({
template: `
<div cdkTrapFocus>
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100"/>
</svg>
</div>
`
})
class FocusTrapWithSvg {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
}