Skip to content

Commit 3a4d1e4

Browse files
committed
chore: improve the test by executeComponentTest
1 parent 2509a72 commit 3a4d1e4

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/tests/test-component.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import userEvent, { UserEvent } from '@testing-library/user-event'
2+
3+
export async function executeComponentTest<T>(renderComponent: () => T, assertion: (component: T, user: UserEvent) => Promise<unknown>) {
4+
const component = renderComponent()
5+
const user = userEvent.setup()
6+
7+
await assertion(component, user)
8+
}

tests/setup/clean-up-dom.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { cleanup } from '@/tests/testing-library'
2+
import { afterEach } from 'vitest'
3+
4+
afterEach(() => {
5+
cleanup()
6+
})

tests/theme.test.tsx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import { describe, it, expect, beforeAll } from 'vitest'
2-
import { render, waitFor, screen } from '../src/tests/testing-library'
2+
import { render, waitFor } from '../src/tests/testing-library'
33
import MatchMediaMock from '@/tests/mock-match-media'
44
import { afterEach } from 'node:test'
55
import { ThemeToggle } from '@/features/theme/components/theme-toggle'
6-
import userEvent from '@testing-library/user-event'
6+
import { executeComponentTest } from '@/tests/test-component'
77

88
describe('when using the default system light theme', () => {
9-
beforeAll(() => {
10-
render(<ThemeToggle />)
11-
})
12-
13-
it('the theme is set to light', async () => {
14-
await waitFor(() => expect(document.documentElement.classList.contains('light')).toBe(true))
9+
it('the theme is set to light', () => {
10+
return executeComponentTest(
11+
() => render(<ThemeToggle />),
12+
async () => {
13+
await waitFor(() => expect(document.documentElement.classList.contains('light')).toBe(true))
14+
}
15+
)
1516
})
17+
})
1618

17-
describe('when the theme is toggled to dark', () => {
18-
it('the theme is set to dark', async () => {
19-
userEvent.click(await screen.findByTestId('theme-toggle-menu'))
20-
userEvent.click(await screen.findByText('Dark'))
19+
describe('when the theme is toggled to dark', () => {
20+
it('the theme is set to dark', async () => {
21+
return executeComponentTest(
22+
() => render(<ThemeToggle />),
23+
async (component, user) => {
24+
user.click(await component.findByTestId('theme-toggle-menu'))
25+
user.click(await component.findByText('Dark'))
2126

22-
await waitFor(() => expect(document.documentElement.classList.contains('dark')).toBe(true))
23-
})
27+
await waitFor(() => expect(document.documentElement.classList.contains('dark')).toBe(true))
28+
}
29+
)
2430
})
2531
})
2632

@@ -30,11 +36,15 @@ describe('when using the default system dark theme', () => {
3036
beforeAll(() => {
3137
// Set system theme to dark
3238
matchMediaMock.useMediaQuery('(prefers-color-scheme: dark)')
33-
render(<ThemeToggle />)
3439
})
3540
afterEach(() => matchMediaMock.clear())
3641

37-
it('the theme is set to dark', async () => {
38-
await waitFor(() => expect(document.documentElement.classList.contains('dark')).toBe(true))
42+
it('the theme is set to dark', () => {
43+
return executeComponentTest(
44+
() => render(<ThemeToggle />),
45+
async () => {
46+
await waitFor(() => expect(document.documentElement.classList.contains('dark')).toBe(true))
47+
}
48+
)
3949
})
4050
})

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
test: {
1010
root: 'tests',
1111
environment: 'happy-dom',
12+
setupFiles: ['setup/clean-up-dom.ts'],
1213
},
1314

1415
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`

0 commit comments

Comments
 (0)