generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add set method to useThemeMode hooks
- Loading branch information
Showing
9 changed files
with
147 additions
and
49 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
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,26 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useContext } from 'react'; | ||
import { vi } from 'vitest'; | ||
|
||
import { ThemeModeContext } from './ThemeModeContext'; | ||
|
||
describe('ThemeModeContext', () => { | ||
it('should have default values', () => { | ||
const { result } = renderHook(() => useContext(ThemeModeContext)); | ||
expect(result.current.appearance).toEqual('light'); | ||
expect(result.current.isDarkMode).toEqual(false); | ||
expect(result.current.themeMode).toEqual('light'); | ||
expect(result.current.browserPrefers).toEqual('light'); | ||
expect(result.current.setAppearance).toBeDefined(); | ||
expect(result.current.setThemeMode).toBeDefined(); | ||
}); | ||
|
||
it('should return false when window is undefined', () => { | ||
const matchMedia = vi.fn(); | ||
Object.defineProperty(window, 'matchMedia', { value: matchMedia }); | ||
|
||
const { result } = renderHook(() => useContext(ThemeModeContext)); | ||
expect(result.current.browserPrefers).toEqual('light'); | ||
expect(matchMedia).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
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,93 @@ | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
|
||
import { ThemeModeContext } from '@/context'; | ||
import { useTheme } from '@/functions'; | ||
import { useThemeMode } from '@/hooks'; | ||
import { ThemeContextState } from '@/types'; | ||
import ThemeSwitcher, { ThemeSwitcherProps } from './ThemeSwitcher'; | ||
|
||
const mockUseTheme = { | ||
appearance: 'light', | ||
themeMode: 'light', | ||
} as ThemeContextState; | ||
|
||
const MockedChild = () => { | ||
const theme = useTheme(); | ||
const { setThemeMode, setAppearance } = useThemeMode(); | ||
return ( | ||
<div style={{ background: theme.colorBgContainer }}> | ||
Mocked Child | ||
<div | ||
onClick={() => { | ||
setAppearance('dark'); | ||
}} | ||
> | ||
switch-dark | ||
</div> | ||
<div | ||
onClick={() => { | ||
setThemeMode('dark'); | ||
}} | ||
> | ||
theme-mode-dark | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const Component = (props: Partial<ThemeSwitcherProps>) => ( | ||
<ThemeModeContext.Provider value={mockUseTheme}> | ||
<ThemeSwitcher {...props} useTheme={useTheme}> | ||
<MockedChild /> | ||
</ThemeSwitcher> | ||
</ThemeModeContext.Provider> | ||
); | ||
|
||
describe('<ThemeSwitcher />', () => { | ||
it('should render the child component', () => { | ||
const { getByText } = render(<Component />); | ||
expect(getByText('Mocked Child')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render with default appearance', () => { | ||
const { container } = render(<Component />); | ||
expect(container.firstChild).toHaveStyle('background-color: #fff'); | ||
}); | ||
|
||
it.skip('should render with dark appearance', () => { | ||
const { container } = render(<Component appearance={'dark'} />); | ||
|
||
expect(container.firstChild).toHaveStyle('background-color: #000'); | ||
}); | ||
|
||
it('should render with light theme mode', () => { | ||
const { container } = render(<Component themeMode={'light'} />); | ||
|
||
expect(container.firstChild).toHaveStyle('background-color: #fff'); | ||
}); | ||
|
||
it.skip('should render with dark theme mode', () => { | ||
const { container } = render(<Component themeMode={'dark'} />); | ||
|
||
expect(container.firstChild).toHaveStyle('background-color: #000'); | ||
}); | ||
|
||
it('should call onAppearanceChange when appearance is changed', () => { | ||
const onAppearanceChange = vi.fn(); | ||
const { getByText } = render(<Component onAppearanceChange={onAppearanceChange} />); | ||
|
||
const switchElement = getByText('switch-dark'); | ||
fireEvent.click(switchElement); | ||
expect(onAppearanceChange).toHaveBeenCalledWith('dark'); | ||
}); | ||
|
||
it('should call onThemeModeChange when theme mode is changed', () => { | ||
const onThemeModeChange = vi.fn(); | ||
const { getByText } = render(<Component onThemeModeChange={onThemeModeChange} />); | ||
|
||
const radioElement = getByText('theme-mode-dark'); | ||
fireEvent.click(radioElement); | ||
expect(onThemeModeChange).toHaveBeenCalledWith('dark'); | ||
}); | ||
}); |
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
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
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
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
This file was deleted.
Oops, something went wrong.