1+ import React from 'react' ;
2+ import { mount } from 'enzyme' ;
3+ import { act } from 'react-dom/test-utils' ;
4+ import List from '../src' ;
5+
6+ describe ( 'List.dark' , ( ) => {
7+ const originalMatchMedia = window . matchMedia ;
8+
9+ afterEach ( ( ) => {
10+ window . matchMedia = originalMatchMedia ;
11+ } ) ;
12+
13+ const mockMatchMedia = ( matches ) => {
14+ Object . defineProperty ( window , 'matchMedia' , {
15+ writable : true ,
16+ value : jest . fn ( ) . mockImplementation ( query => ( {
17+ matches,
18+ media : query ,
19+ onchange : null ,
20+ addListener : jest . fn ( ) , // deprecated
21+ removeListener : jest . fn ( ) , // deprecated
22+ addEventListener : jest . fn ( ) ,
23+ removeEventListener : jest . fn ( ) ,
24+ dispatchEvent : jest . fn ( ) ,
25+ } ) ) ,
26+ } ) ;
27+ } ;
28+
29+ it ( 'should render dark scrollbar' , ( ) => {
30+ mockMatchMedia ( true ) ;
31+
32+ const wrapper = mount (
33+ < List data = { [ 1 , 2 , 3 ] } height = { 10 } itemHeight = { 5 } itemKey = { ( item ) => item } >
34+ { ( item ) => < div > { item } </ div > }
35+ </ List >
36+ ) ;
37+
38+ const thumb = wrapper . find ( '.rc-virtual-list-scrollbar-thumb' ) ;
39+ expect ( thumb . props ( ) . style . background ) . toBe ( 'rgba(255, 255, 255, 0.5)' ) ;
40+ } ) ;
41+
42+ it ( 'should render light scrollbar' , ( ) => {
43+ mockMatchMedia ( false ) ;
44+
45+ const wrapper = mount (
46+ < List data = { [ 1 , 2 , 3 ] } height = { 10 } itemHeight = { 5 } itemKey = { ( item ) => item } >
47+ { ( item ) => < div > { item } </ div > }
48+ </ List >
49+ ) ;
50+
51+ const thumb = wrapper . find ( '.rc-virtual-list-scrollbar-thumb' ) ;
52+ expect ( thumb . props ( ) . style . background ) . toBe ( 'rgba(0, 0, 0, 0.5)' ) ;
53+ } ) ;
54+
55+ it ( 'should update on theme change' , ( ) => {
56+ let listener ;
57+ Object . defineProperty ( window , 'matchMedia' , {
58+ writable : true ,
59+ value : jest . fn ( ) . mockImplementation ( ( ) => ( {
60+ matches : false ,
61+ addEventListener : ( type , cb ) => {
62+ if ( type === 'change' ) {
63+ listener = cb ;
64+ }
65+ } ,
66+ removeEventListener : ( ) => { } ,
67+ } ) ) ,
68+ } ) ;
69+
70+ const wrapper = mount (
71+ < List data = { [ 1 , 2 , 3 ] } height = { 10 } itemHeight = { 5 } itemKey = { ( item ) => item } >
72+ { ( item ) => < div > { item } </ div > }
73+ </ List >
74+ ) ;
75+
76+ // Initial: light
77+ expect ( wrapper . find ( '.rc-virtual-list-scrollbar-thumb' ) . props ( ) . style . background ) . toBe (
78+ 'rgba(0, 0, 0, 0.5)' ,
79+ ) ;
80+
81+ // Change to dark
82+ act ( ( ) => {
83+ listener ( { matches : true } ) ;
84+ } ) ;
85+ wrapper . update ( ) ;
86+
87+ expect ( wrapper . find ( '.rc-virtual-list-scrollbar-thumb' ) . props ( ) . style . background ) . toBe (
88+ 'rgba(255, 255, 255, 0.5)' ,
89+ ) ;
90+
91+ // Change back to light
92+ act ( ( ) => {
93+ listener ( { matches : false } ) ;
94+ } ) ;
95+ wrapper . update ( ) ;
96+
97+ expect ( wrapper . find ( '.rc-virtual-list-scrollbar-thumb' ) . props ( ) . style . background ) . toBe (
98+ 'rgba(0, 0, 0, 0.5)' ,
99+ ) ;
100+ } ) ;
101+ } ) ;
0 commit comments