Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit e172b69

Browse files
authored
Test: Add unit tests for DarkModeToggle component (#2850)
1 parent 6e5c2c8 commit e172b69

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

firebase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,4 @@
472472
"type": "301"
473473
}
474474
]
475-
}
475+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`DarkModeToggle Component render dark mode toggle 1`] = `
4+
<div>
5+
<button
6+
class="darkModeToggle"
7+
type="button"
8+
>
9+
<span
10+
class="sr-only"
11+
>
12+
Toggle Dark Mode
13+
</span>
14+
<svg
15+
aria-hidden="true"
16+
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium light-mode-only css-i4bv87-MuiSvgIcon-root"
17+
data-testid="ModeNightIcon"
18+
focusable="false"
19+
viewBox="0 0 24 24"
20+
>
21+
<path
22+
d="M9.5 2c-1.82 0-3.53.5-5 1.35 2.99 1.73 5 4.95 5 8.65s-2.01 6.92-5 8.65c1.47.85 3.18 1.35 5 1.35 5.52 0 10-4.48 10-10S15.02 2 9.5 2z"
23+
/>
24+
</svg>
25+
<svg
26+
aria-hidden="true"
27+
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium dark-mode-only css-i4bv87-MuiSvgIcon-root"
28+
data-testid="BrightnessMediumIcon"
29+
focusable="false"
30+
viewBox="0 0 24 24"
31+
>
32+
<path
33+
d="M20 15.31 23.31 12 20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69zM12 18V6c3.31 0 6 2.69 6 6s-2.69 6-6 6z"
34+
/>
35+
</svg>
36+
</button>
37+
</div>
38+
`;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import DarkModeToggle from '../index';
5+
6+
let mockCurrentTheme = '';
7+
const mockToggleTheme = jest.fn().mockImplementation((newTheme: string) => {
8+
mockCurrentTheme = newTheme;
9+
});
10+
11+
// mock dark mode module for controlling dark mode HOC behavior
12+
jest.mock('@skagami/gatsby-plugin-dark-mode', () => ({
13+
__esModule: true,
14+
useTheme: () => [mockCurrentTheme, mockToggleTheme],
15+
}));
16+
17+
describe('DarkModeToggle Component', () => {
18+
it('render dark mode toggle', () => {
19+
const { container } = render(<DarkModeToggle />);
20+
expect(container).toMatchSnapshot();
21+
});
22+
23+
it('switches dark theme to light theme', async () => {
24+
mockCurrentTheme = 'dark';
25+
render(<DarkModeToggle />);
26+
const toggle = await screen.findByText('Toggle Dark Mode');
27+
await userEvent.click(toggle);
28+
expect(mockCurrentTheme).toBe('light');
29+
});
30+
31+
it('switches light theme to dark theme', async () => {
32+
mockCurrentTheme = 'light';
33+
render(<DarkModeToggle />);
34+
const toggle = await screen.findByText('Toggle Dark Mode');
35+
await userEvent.click(toggle);
36+
expect(mockCurrentTheme).toBe('dark');
37+
});
38+
});

0 commit comments

Comments
 (0)