-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from wenfeng-xing/use-is-dark-mode
chore: add the useIsDarkMode
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/ui-kit-components/src/utils/useIsDarkMode.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import { renderHook, RenderResult } from '@testing-library/react-hooks'; | ||
import { createTheme, ThemeProvider, Palette } from '@mui/material'; | ||
import UseIsDarkMode from './useIsDarkMode'; | ||
|
||
describe('Test UseIsDarkMode', () => { | ||
describe('Given the UseIsDarkMode hook', () => { | ||
describe('When the theme is dark', () => { | ||
let res: RenderResult<boolean>; | ||
beforeEach(() => { | ||
const theme = createTheme({ | ||
palette: { | ||
mode: 'dark', | ||
} as Palette, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactElement }) => ( | ||
<ThemeProvider theme={theme}>{children}</ThemeProvider> | ||
); | ||
res = renderHook(() => UseIsDarkMode(), { wrapper }).result; | ||
}); | ||
|
||
it('Then should get the true value', () => { | ||
expect(res.current).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('When the theme is light', () => { | ||
let res: RenderResult<boolean>; | ||
beforeEach(() => { | ||
const theme = createTheme({ | ||
palette: { | ||
mode: 'light', | ||
} as Palette, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactElement }) => ( | ||
<ThemeProvider theme={theme}>{children}</ThemeProvider> | ||
); | ||
res = renderHook(() => UseIsDarkMode(), { wrapper }).result; | ||
}); | ||
|
||
it('Then should get the false value', () => { | ||
expect(res.current).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useTheme } from '@mui/material'; | ||
|
||
const UseIsDarkMode = () => { | ||
const { palette } = useTheme(); | ||
|
||
return palette.mode === 'dark'; | ||
}; | ||
|
||
export default UseIsDarkMode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters