@@ -6,76 +6,80 @@ import {
66 WHITE_ON_BLACK_CSS_CLASS ,
77} from './high-contrast-mode-detector' ;
88import { 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' ;
1013
1114describe ( 'HighContrastModeDetector' , ( ) => {
12- let fakePlatform : Platform ;
15+ function getDetector ( document : unknown , platform ?: Platform ) {
16+ const providers : Provider [ ] = [ { provide : DOCUMENT , useValue : document } ] ;
1317
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+ }
1725
1826 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+
2129 expect ( detector . getHighContrastMode ( ) )
2230 . withContext ( 'Expected high-contrast mode `NONE` on non-browser platforms' )
2331 . toBe ( HighContrastMode . NONE ) ;
2432 } ) ;
2533
2634 it ( 'should not apply any css classes for non-browser platforms' , ( ) => {
27- fakePlatform . isBrowser = false ;
2835 const fakeDocument = getFakeDocument ( '' ) ;
29- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
36+ const detector = getDetector ( fakeDocument , { isBrowser : false } as Platform ) ;
3037 detector . _applyBodyHighContrastModeCssClasses ( ) ;
3138 expect ( fakeDocument . body . className )
3239 . withContext ( 'Expected body not to have any CSS classes in non-browser platforms' )
3340 . toBe ( '' ) ;
3441 } ) ;
3542
3643 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)' ) ) ;
3845 expect ( detector . getHighContrastMode ( ) )
3946 . withContext ( 'Expected high-contrast mode `WHITE_ON_BLACK`' )
4047 . toBe ( HighContrastMode . WHITE_ON_BLACK ) ;
4148 } ) ;
4249
4350 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)' ) ) ;
4852 expect ( detector . getHighContrastMode ( ) )
4953 . withContext ( 'Expected high-contrast mode `BLACK_ON_WHITE`' )
5054 . toBe ( HighContrastMode . BLACK_ON_WHITE ) ;
5155 } ) ;
5256
5357 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)' ) ) ;
5559 expect ( detector . getHighContrastMode ( ) )
5660 . withContext ( 'Expected high-contrast mode `NONE`' )
5761 . toBe ( HighContrastMode . NONE ) ;
5862 } ) ;
5963
6064 it ( 'should apply css classes for BLACK_ON_WHITE high-contrast mode' , ( ) => {
6165 const fakeDocument = getFakeDocument ( 'rgb(255,255,255)' ) ;
62- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
66+ const detector = getDetector ( fakeDocument ) ;
6367 detector . _applyBodyHighContrastModeCssClasses ( ) ;
6468 expect ( fakeDocument . body . classList ) . toContain ( HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS ) ;
6569 expect ( fakeDocument . body . classList ) . toContain ( BLACK_ON_WHITE_CSS_CLASS ) ;
6670 } ) ;
6771
6872 it ( 'should apply css classes for WHITE_ON_BLACK high-contrast mode' , ( ) => {
6973 const fakeDocument = getFakeDocument ( 'rgb(0,0,0)' ) ;
70- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
74+ const detector = getDetector ( fakeDocument ) ;
7175 detector . _applyBodyHighContrastModeCssClasses ( ) ;
7276 expect ( fakeDocument . body . classList ) . toContain ( HIGH_CONTRAST_MODE_ACTIVE_CSS_CLASS ) ;
7377 expect ( fakeDocument . body . classList ) . toContain ( WHITE_ON_BLACK_CSS_CLASS ) ;
7478 } ) ;
7579
7680 it ( 'should not apply any css classes when backgrounds are not coerced' , ( ) => {
7781 const fakeDocument = getFakeDocument ( '' ) ;
78- const detector = new HighContrastModeDetector ( fakePlatform , fakeDocument ) ;
82+ const detector = getDetector ( fakeDocument ) ;
7983 detector . _applyBodyHighContrastModeCssClasses ( ) ;
8084 expect ( fakeDocument . body . className )
8185 . withContext ( 'Expected body not to have any CSS classes in non-browser platforms' )
@@ -88,6 +92,7 @@ function getFakeDocument(fakeComputedBackgroundColor: string) {
8892 return {
8993 body : document . createElement ( 'body' ) ,
9094 createElement : ( tag : string ) => document . createElement ( tag ) ,
95+ querySelectorAll : ( selector : string ) => document . querySelectorAll ( selector ) ,
9196 defaultView : {
9297 getComputedStyle : ( ) => ( { backgroundColor : fakeComputedBackgroundColor } ) ,
9398 } ,
0 commit comments