Skip to content

Commit 2f695c4

Browse files
authored
test(cdk/a11y): use DI to construct modality checker (#23030)
Currently the `InputModalityChecker` tests construct the object manually which is brittle. These changes switch using DI to construct it automatically.
1 parent cfa9a89 commit 2f695c4

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/cdk/a11y/input-modality/input-modality-detector.spec.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {A, ALT, B, C, CONTROL, MAC_META, META, SHIFT} from '@angular/cdk/keycodes';
22
import {Platform} from '@angular/cdk/platform';
3-
import {NgZone, PLATFORM_ID} from '@angular/core';
43

54
import {
65
createMouseEvent,
@@ -10,26 +9,36 @@ import {
109
dispatchEvent,
1110
createTouchEvent,
1211
} from '@angular/cdk/testing/private';
13-
import {fakeAsync, inject, tick} from '@angular/core/testing';
14-
import {InputModality, InputModalityDetector, TOUCH_BUFFER_MS} from './input-modality-detector';
12+
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
13+
import {
14+
InputModality,
15+
InputModalityDetector,
16+
InputModalityDetectorOptions,
17+
INPUT_MODALITY_DETECTOR_OPTIONS,
18+
TOUCH_BUFFER_MS,
19+
} from './input-modality-detector';
1520

1621
describe('InputModalityDetector', () => {
17-
let platform: Platform;
18-
let ngZone: NgZone;
1922
let detector: InputModalityDetector;
2023

21-
beforeEach(inject([PLATFORM_ID], (platformId: Object) => {
22-
platform = new Platform(platformId);
23-
ngZone = new NgZone({});
24-
}));
24+
function setupTest(isBrowser = true, options: InputModalityDetectorOptions = {}) {
25+
TestBed.configureTestingModule({
26+
providers: [
27+
{provide: Platform, useValue: {isBrowser}},
28+
{provide: INPUT_MODALITY_DETECTOR_OPTIONS, useValue: options},
29+
]
30+
});
31+
32+
detector = TestBed.inject(InputModalityDetector);
33+
}
2534

2635
afterEach(() => {
2736
detector?.ngOnDestroy();
37+
detector = undefined!;
2838
});
2939

3040
it('should do nothing on non-browser platforms', () => {
31-
platform.isBrowser = false;
32-
detector = new InputModalityDetector(platform, ngZone, document);
41+
setupTest(false);
3342
expect(detector.mostRecentModality).toBe(null);
3443

3544
dispatchKeyboardEvent(document, 'keydown');
@@ -43,25 +52,25 @@ describe('InputModalityDetector', () => {
4352
});
4453

4554
it('should detect keyboard input modality', () => {
46-
detector = new InputModalityDetector(platform, ngZone, document);
55+
setupTest();
4756
dispatchKeyboardEvent(document, 'keydown');
4857
expect(detector.mostRecentModality).toBe('keyboard');
4958
});
5059

5160
it('should detect mouse input modality', () => {
52-
detector = new InputModalityDetector(platform, ngZone, document);
61+
setupTest();
5362
dispatchMouseEvent(document, 'mousedown');
5463
expect(detector.mostRecentModality).toBe('mouse');
5564
});
5665

5766
it('should detect touch input modality', () => {
58-
detector = new InputModalityDetector(platform, ngZone, document);
67+
setupTest();
5968
dispatchTouchEvent(document, 'touchstart');
6069
expect(detector.mostRecentModality).toBe('touch');
6170
});
6271

6372
it('should detect changes in input modality', () => {
64-
detector = new InputModalityDetector(platform, ngZone, document);
73+
setupTest();
6574

6675
dispatchKeyboardEvent(document, 'keydown');
6776
expect(detector.mostRecentModality).toBe('keyboard');
@@ -77,9 +86,9 @@ describe('InputModalityDetector', () => {
7786
});
7887

7988
it('should emit when input modalities are detected', () => {
80-
detector = new InputModalityDetector(platform, ngZone, document);
89+
setupTest();
8190
const emitted: InputModality[] = [];
82-
detector.modalityDetected.subscribe((modality: InputModality) => {
91+
detector.modalityDetected.subscribe(modality => {
8392
emitted.push(modality);
8493
});
8594

@@ -102,9 +111,9 @@ describe('InputModalityDetector', () => {
102111
});
103112

104113
it('should emit changes in input modality', () => {
105-
detector = new InputModalityDetector(platform, ngZone, document);
114+
setupTest();
106115
const emitted: InputModality[] = [];
107-
detector.modalityChanged.subscribe((modality: InputModality) => {
116+
detector.modalityChanged.subscribe(modality => {
108117
emitted.push(modality);
109118
});
110119

@@ -130,7 +139,7 @@ describe('InputModalityDetector', () => {
130139
});
131140

132141
it('should detect fake screen reader mouse events as keyboard input modality', () => {
133-
detector = new InputModalityDetector(platform, ngZone, document);
142+
setupTest();
134143

135144
// Create a fake screen-reader mouse event.
136145
const event = createMouseEvent('mousedown');
@@ -141,7 +150,7 @@ describe('InputModalityDetector', () => {
141150
});
142151

143152
it('should detect fake screen reader touch events as keyboard input modality', () => {
144-
detector = new InputModalityDetector(platform, ngZone, document);
153+
setupTest();
145154

146155
// Create a fake screen-reader touch event.
147156
const event = createTouchEvent('touchstart');
@@ -152,7 +161,7 @@ describe('InputModalityDetector', () => {
152161
});
153162

154163
it('should ignore certain modifier keys by default', () => {
155-
detector = new InputModalityDetector(platform, ngZone, document);
164+
setupTest();
156165

157166
dispatchKeyboardEvent(document, 'keydown', ALT);
158167
dispatchKeyboardEvent(document, 'keydown', CONTROL);
@@ -164,13 +173,13 @@ describe('InputModalityDetector', () => {
164173
});
165174

166175
it('should not ignore modifier keys if specified', () => {
167-
detector = new InputModalityDetector(platform, ngZone, document, {ignoreKeys: []});
176+
setupTest(true, {ignoreKeys: []});
168177
dispatchKeyboardEvent(document, 'keydown', CONTROL);
169178
expect(detector.mostRecentModality).toBe('keyboard');
170179
});
171180

172181
it('should ignore keys if specified', () => {
173-
detector = new InputModalityDetector(platform, ngZone, document, {ignoreKeys: [A, B, C]});
182+
setupTest(true, {ignoreKeys: [A, B, C]});
174183

175184
dispatchKeyboardEvent(document, 'keydown', A);
176185
dispatchKeyboardEvent(document, 'keydown', B);
@@ -180,7 +189,7 @@ describe('InputModalityDetector', () => {
180189
});
181190

182191
it('should ignore mouse events that occur too closely after a touch event', fakeAsync(() => {
183-
detector = new InputModalityDetector(platform, ngZone, document);
192+
setupTest();
184193

185194
dispatchTouchEvent(document, 'touchstart');
186195
dispatchMouseEvent(document, 'mousedown');

0 commit comments

Comments
 (0)