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