Skip to content

Commit 6ac900b

Browse files
Merge pull request #229 from commitd/stuarthendren/issue228
fix: allows tests to run without jest fix for media query
2 parents 7347c81 + 2c394af commit 6ac900b

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

src/components/Text/Text.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const Monospace = forwardRef<HTMLPreElement, MonospaceProps>(
173173
({ inline = false, ...props }, forwardedRef) => {
174174
return (
175175
<Text
176-
as={inline ? 'pre' : 'span'}
176+
as={inline ? 'span' : 'pre'}
177177
className={MONOSPACE_CLASS_NAME}
178178
font="monospace"
179179
{...props}

src/components/ThemeProvider/ThemeController.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ export const ThemeController: React.FC<ThemeControllerProps> = ({
8484
}
8585

8686
window
87-
.matchMedia('(prefers-color-scheme: dark)')
87+
?.matchMedia?.('(prefers-color-scheme: dark)')
8888
.addEventListener('change', (e) => {
8989
setThemeValues(e.matches ? 'dark' : 'light')
9090
})
9191

9292
window
93-
.matchMedia('(prefers-color-scheme: light)')
93+
?.matchMedia?.('(prefers-color-scheme: light)')
9494
.addEventListener('change', (e) => {
9595
setThemeValues(e.matches ? 'light' : 'dark')
9696
})

src/components/ThemeProvider/ThemeProvider.test.tsx

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,94 @@
11
import React from 'react'
2+
import { act, renderDark, renderLight, renderPlain, screen } from 'test-utils'
23
import { ThemeProvider } from '.'
34
import {
4-
UtilityUseThemeResolve,
55
UtilityUseThemeController,
6+
UtilityUseThemeResolve,
67
} from './ThemeProvider.stories'
7-
import { renderPlain, renderLight, renderDark } from 'test-utils'
8+
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
let addEventListener: jest.Mock
11+
12+
beforeEach(() => {
13+
addEventListener = jest.fn()
14+
15+
// Official way to supply missing window method https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
16+
Object.defineProperty(window, 'matchMedia', {
17+
writable: true,
18+
value: jest.fn().mockImplementation((query: string) => ({
19+
matches: false,
20+
media: query,
21+
onchange: null,
22+
addListener: jest.fn(), // Deprecated
23+
removeListener: jest.fn(), // Deprecated
24+
addEventListener,
25+
removeEventListener: jest.fn(),
26+
dispatchEvent: jest.fn(),
27+
})),
28+
})
29+
})
830

931
it('renders light without error', () => {
1032
const { asFragment } = renderPlain(<ThemeProvider />)
1133
expect(asFragment()).toBeDefined()
34+
expect(addEventListener).toHaveBeenCalledTimes(4)
1235
})
1336

1437
it('renders dark without error', () => {
1538
const { asFragment } = renderPlain(<ThemeProvider choice="dark" />)
1639
expect(asFragment()).toBeDefined()
40+
expect(addEventListener).toHaveBeenCalledTimes(4)
1741
})
1842

19-
it('renders resolutions within light theme', () => {
20-
const { asFragment } = renderLight(<UtilityUseThemeResolve />)
21-
expect(asFragment()).toBeDefined()
43+
it('changing system/window theme changes to dark sets the theme', async () => {
44+
renderPlain(
45+
<ThemeProvider>
46+
<UtilityUseThemeResolve />
47+
</ThemeProvider>
48+
)
49+
50+
act(() => {
51+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
52+
const listener = addEventListener.mock.calls[0][1] as (e: {
53+
matches: string
54+
}) => void
55+
listener({ matches: 'dark' })
56+
})
57+
58+
expect(await screen.findByText('#000000')).toBeInTheDocument()
2259
})
2360

24-
it('renders resolutions within dark theme', () => {
25-
const { asFragment } = renderDark(<UtilityUseThemeResolve />)
26-
expect(asFragment()).toBeDefined()
61+
it('changing system/window theme to light sets the theme', async () => {
62+
renderPlain(
63+
<ThemeProvider>
64+
<UtilityUseThemeResolve />
65+
</ThemeProvider>
66+
)
67+
act(() => {
68+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
69+
const listener = addEventListener.mock.calls[1][1] as (e: {
70+
matches: string
71+
}) => void
72+
listener({ matches: 'light' })
73+
})
74+
75+
expect(await screen.findByText('#f7f7f7')).toBeInTheDocument()
76+
})
77+
78+
it('changing system/window when theme selected does not change the theme', () => {
79+
renderPlain(
80+
<ThemeProvider choice="light">
81+
<UtilityUseThemeResolve />
82+
</ThemeProvider>
83+
)
84+
act(() => {
85+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
86+
const listener = addEventListener.mock.calls[0][1] as (e: {
87+
matches: string
88+
}) => void
89+
listener({ matches: 'dark' })
90+
})
91+
expect(screen.queryByText('#000000')).not.toBeInTheDocument()
2792
})
2893

2994
it('renders switch within light theme', () => {

src/utils/test-utils.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ import ResizeObserver from 'resize-observer-polyfill'
1919
// This is used in some components.
2020
global.ResizeObserver = ResizeObserver
2121

22-
// Official way to supply missing window method https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
23-
Object.defineProperty(window, 'matchMedia', {
24-
writable: true,
25-
value: jest.fn().mockImplementation((query: string) => ({
26-
matches: false,
27-
media: query,
28-
onchange: null,
29-
addListener: jest.fn(), // Deprecated
30-
removeListener: jest.fn(), // Deprecated
31-
addEventListener: jest.fn(),
32-
removeEventListener: jest.fn(),
33-
dispatchEvent: jest.fn(),
34-
})),
35-
})
36-
3722
const LightTheme: React.FC = ({ children }) => (
3823
<ThemeProvider choice="light">{children}</ThemeProvider>
3924
)

0 commit comments

Comments
 (0)