1+ import "@testing-library/jest-dom" ;
2+ import { render , fireEvent } from "@testing-library/react" ;
3+
4+ import { ColourSchemeButton } from "./ColourSchemeButton" ;
5+ import { ColourSchemes } from "../utils/globals" ;
6+
7+ let mockSetColorScheme = jest . fn ( )
8+ jest . mock ( "@mui/material" , ( ) => {
9+ return {
10+ ...jest . requireActual ( "@mui/material" ) ,
11+ useColorScheme : jest . fn ( ) . mockReturnValue ( {
12+ colorScheme : jest . requireActual ( "../utils/globals" ) . ColourSchemes . Dark ,
13+ setColorScheme : ( scheme : ColourSchemes ) => mockSetColorScheme ( scheme )
14+ } )
15+ } ;
16+ } )
17+
18+ describe ( "ColourSchemeButton" , ( ) => {
19+
20+ it ( "should render without errors" , ( ) => {
21+ render ( < ColourSchemeButton /> ) ;
22+ } ) ;
23+
24+ it ( "should show dark icon and button" , ( ) => {
25+ const { getByTestId, getByRole} = render ( < ColourSchemeButton /> ) ;
26+
27+ const button = getByRole ( "button" )
28+ expect ( button ) . toBeInTheDocument ( )
29+
30+ const icon = getByTestId ( "BedtimeIcon" )
31+ expect ( icon ) . toBeInTheDocument ( )
32+ } ) ;
33+
34+ it ( "should change colour scheme on click" , ( ) => {
35+ const { getByRole} = render ( < ColourSchemeButton /> ) ;
36+
37+ const button = getByRole ( "button" )
38+ fireEvent . click ( button ) ;
39+
40+ expect ( mockSetColorScheme ) . toHaveBeenCalledWith ( ColourSchemes . Light )
41+ } ) ;
42+
43+ it ( "should call local onclick when button clicked" , ( ) => {
44+ const mockOnClick = jest . fn ( ) ;
45+ const { getByRole} = render ( < ColourSchemeButton onClick = { mockOnClick } /> ) ;
46+
47+ const button = getByRole ( "button" )
48+ fireEvent . click ( button ) ;
49+
50+ expect ( mockOnClick ) . toHaveBeenCalled ( )
51+ } ) ;
52+ } )
0 commit comments