@@ -6,76 +6,80 @@ import {
6
6
WHITE_ON_BLACK_CSS_CLASS ,
7
7
} from './high-contrast-mode-detector' ;
8
8
import { Platform } from '@angular/cdk/platform' ;
9
- import { inject } from '@angular/core/testing' ;
9
+ import { TestBed } from '@angular/core/testing' ;
10
+ import { Provider } from '@angular/core' ;
11
+ import { A11yModule } from '../a11y-module' ;
12
+ import { DOCUMENT } from '@angular/common' ;
10
13
11
14
describe ( 'HighContrastModeDetector' , ( ) => {
12
- let fakePlatform : Platform ;
15
+ function getDetector ( document : unknown , platform ?: Platform ) {
16
+ const providers : Provider [ ] = [ { provide : DOCUMENT , useValue : document } ] ;
13
17
14
- beforeEach ( inject ( [ Platform ] , ( p : Platform ) => {
15
- fakePlatform = p ;
16
- } ) ) ;
18
+ if ( platform ) {
19
+ providers . push ( { provide : Platform , useValue : platform } ) ;
20
+ }
21
+
22
+ TestBed . configureTestingModule ( { imports : [ A11yModule ] , providers} ) ;
23
+ return TestBed . inject ( HighContrastModeDetector ) ;
24
+ }
17
25
18
26
it ( 'should detect NONE for non-browser platforms' , ( ) => {
19
- fakePlatform . isBrowser = false ;
20
- const detector = new HighContrastModeDetector ( fakePlatform , { } ) ;
27
+ const detector = getDetector ( getFakeDocument ( '' ) , { isBrowser : false } as Platform ) ;
28
+
21
29
expect ( detector . getHighContrastMode ( ) )
22
30
. withContext ( 'Expected high-contrast mode `NONE` on non-browser platforms' )
23
31
. toBe ( HighContrastMode . NONE ) ;
24
32
} ) ;
25
33
26
34
it ( 'should not apply any css classes for non-browser platforms' , ( ) => {
27
- fakePlatform . isBrowser = false ;
28
35
const fakeDocument = getFakeDocument ( '' ) ;
29
- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
36
+ const detector = getDetector ( fakeDocument , { isBrowser : false } as Platform ) ;
30
37
detector . _applyBodyHighContrastModeCssClasses ( ) ;
31
38
expect ( fakeDocument . body . className )
32
39
. withContext ( 'Expected body not to have any CSS classes in non-browser platforms' )
33
40
. toBe ( '' ) ;
34
41
} ) ;
35
42
36
43
it ( 'should detect WHITE_ON_BLACK when backgrounds are coerced to black' , ( ) => {
37
- const detector = new HighContrastModeDetector ( fakePlatform , getFakeDocument ( 'rgb(0,0,0)' ) ) ;
44
+ const detector = getDetector ( getFakeDocument ( 'rgb(0,0,0)' ) ) ;
38
45
expect ( detector . getHighContrastMode ( ) )
39
46
. withContext ( 'Expected high-contrast mode `WHITE_ON_BLACK`' )
40
47
. toBe ( HighContrastMode . WHITE_ON_BLACK ) ;
41
48
} ) ;
42
49
43
50
it ( 'should detect BLACK_ON_WHITE when backgrounds are coerced to white ' , ( ) => {
44
- const detector = new HighContrastModeDetector (
45
- fakePlatform ,
46
- getFakeDocument ( 'rgb(255,255,255)' ) ,
47
- ) ;
51
+ const detector = getDetector ( getFakeDocument ( 'rgb(255,255,255)' ) ) ;
48
52
expect ( detector . getHighContrastMode ( ) )
49
53
. withContext ( 'Expected high-contrast mode `BLACK_ON_WHITE`' )
50
54
. toBe ( HighContrastMode . BLACK_ON_WHITE ) ;
51
55
} ) ;
52
56
53
57
it ( 'should detect NONE when backgrounds are not coerced ' , ( ) => {
54
- const detector = new HighContrastModeDetector ( fakePlatform , getFakeDocument ( 'rgb(1,2,3)' ) ) ;
58
+ const detector = getDetector ( getFakeDocument ( 'rgb(1,2,3)' ) ) ;
55
59
expect ( detector . getHighContrastMode ( ) )
56
60
. withContext ( 'Expected high-contrast mode `NONE`' )
57
61
. toBe ( HighContrastMode . NONE ) ;
58
62
} ) ;
59
63
60
64
it ( 'should apply css classes for BLACK_ON_WHITE high-contrast mode' , ( ) => {
61
65
const fakeDocument = getFakeDocument ( 'rgb(255,255,255)' ) ;
62
- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
66
+ const detector = getDetector ( fakeDocument ) ;
63
67
detector . _applyBodyHighContrastModeCssClasses ( ) ;
64
68
expect ( fakeDocument . body . classList ) . toContain ( HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS ) ;
65
69
expect ( fakeDocument . body . classList ) . toContain ( BLACK_ON_WHITE_CSS_CLASS ) ;
66
70
} ) ;
67
71
68
72
it ( 'should apply css classes for WHITE_ON_BLACK high-contrast mode' , ( ) => {
69
73
const fakeDocument = getFakeDocument ( 'rgb(0,0,0)' ) ;
70
- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
74
+ const detector = getDetector ( fakeDocument ) ;
71
75
detector . _applyBodyHighContrastModeCssClasses ( ) ;
72
76
expect ( fakeDocument . body . classList ) . toContain ( HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS ) ;
73
77
expect ( fakeDocument . body . classList ) . toContain ( WHITE_ON_BLACK_CSS_CLASS ) ;
74
78
} ) ;
75
79
76
80
it ( 'should not apply any css classes when backgrounds are not coerced' , ( ) => {
77
81
const fakeDocument = getFakeDocument ( '' ) ;
78
- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
82
+ const detector = getDetector ( fakeDocument ) ;
79
83
detector . _applyBodyHighContrastModeCssClasses ( ) ;
80
84
expect ( fakeDocument . body . className )
81
85
. withContext ( 'Expected body not to have any CSS classes in non-browser platforms' )
@@ -88,6 +92,7 @@ function getFakeDocument(fakeComputedBackgroundColor: string) {
88
92
return {
89
93
body : document . createElement ( 'body' ) ,
90
94
createElement : ( tag : string ) => document . createElement ( tag ) ,
95
+ querySelectorAll : ( selector : string ) => document . querySelectorAll ( selector ) ,
91
96
defaultView : {
92
97
getComputedStyle : ( ) => ( { backgroundColor : fakeComputedBackgroundColor } ) ,
93
98
} ,
0 commit comments