1+ import "@testing-library/jest-dom" ;
2+ import { render } from "@testing-library/react" ;
3+
4+ import { ImageColorSchemeSwitch , getLogoSrc } from "./ImageColorSchemeSwitch" ;
5+
6+ jest . mock ( "@mui/material" , ( ) => {
7+ return {
8+ useColorScheme : jest . fn ( ) . mockReturnValue ( { mode :"dark" } )
9+ } ;
10+ } )
11+
12+ describe ( "ImageColorSchemeSwitch" , ( ) => {
13+ const testVals = {
14+ src : "src/light" ,
15+ alt : "test-alt"
16+ } ;
17+
18+ function getRenderImg ( image : any ) {
19+ const { getByAltText} = render ( < ImageColorSchemeSwitch image = { { ...testVals , ...image } } /> ) ;
20+
21+ const img = getByAltText ( testVals . alt )
22+ expect ( img ) . toBeInTheDocument ( )
23+ return img
24+ }
25+
26+ it ( "should render without errors" , ( ) => {
27+ render ( < ImageColorSchemeSwitch image = { { ...testVals } } /> ) ;
28+ } ) ;
29+
30+ it ( "should have src and alt by default" , ( ) => {
31+ const img = getRenderImg ( { } )
32+
33+ expect ( img ) . toHaveAttribute ( "alt" , testVals . alt )
34+ expect ( img ) . toHaveAttribute ( "src" , testVals . src )
35+
36+ expect ( img ) . not . toHaveAttribute ( "width" )
37+ expect ( img ) . not . toHaveAttribute ( "height" )
38+ } ) ;
39+
40+ it ( "should have width 123" , ( ) => {
41+ const width = "123" ;
42+
43+ const img = getRenderImg ( { width} )
44+ expect ( img ) . toHaveAttribute ( "width" , width )
45+ expect ( img ) . not . toHaveAttribute ( "height" )
46+ } ) ;
47+
48+ it ( "should have width 123 and height 124" , ( ) => {
49+ const width = "123" ,
50+ height = "124" ;
51+
52+ const img = getRenderImg ( { width, height} )
53+
54+ expect ( img ) . toHaveAttribute ( "width" , width )
55+ expect ( img ) . toHaveAttribute ( "height" , height )
56+ } ) ;
57+
58+ it ( "should have alternate src" , ( ) => {
59+ const srcDark = "src/dark" ;
60+
61+ const img = getRenderImg ( { srcDark} )
62+
63+ expect ( img ) . toHaveAttribute ( "src" , srcDark )
64+ } ) ;
65+ } )
66+
67+ describe ( "getLogoSrc" , ( ) => {
68+ const srcLight = "src/light" ,
69+ srcDark = "src/dark" ;
70+
71+ it ( "should be null if no image" , ( ) => {
72+ // @ts -ignore: invalid input
73+ expect ( getLogoSrc ( null , "" ) ) . toStrictEqual ( undefined ) ;
74+ // @ts -ignore: invalid input, calm down ts
75+ expect ( getLogoSrc ( ) ) . toStrictEqual ( undefined ) ;
76+ } ) ;
77+
78+ it ( "should be srcLight if no srcDark" , ( ) => {
79+ expect ( getLogoSrc ( { src :srcLight , alt :"" } , "light" ) ) . toStrictEqual ( srcLight ) ;
80+ } ) ;
81+
82+ it ( "should be srcLight if mode is dark but no srcDark" , ( ) => {
83+ expect ( getLogoSrc ( { src :srcLight , alt :"" } , "dark" ) ) . toStrictEqual ( srcLight ) ;
84+ } ) ;
85+
86+ it ( "should be srcLight if srcDark but mode light" , ( ) => {
87+ expect ( getLogoSrc ( { src : srcLight , srcDark : srcDark , alt :"" } , "light" ) ) . toStrictEqual ( srcLight ) ;
88+ } ) ;
89+
90+ it ( "should be srcDark if mode dark" , ( ) => {
91+ expect ( getLogoSrc ( { src :"src/light" , srcDark :srcDark , alt :"" } , "dark" ) ) . toStrictEqual ( srcDark ) ;
92+ } ) ;
93+
94+ } )
0 commit comments