Skip to content

Commit d71e0f6

Browse files
committed
add matchMedia mock to test-helpers
1 parent ef9c8ec commit d71e0f6

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

packages/react/src/UnderlineNav/UnderlineNav.test.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ import {
1515
import {UnderlineNav} from '.'
1616
import {checkExports, checkStoriesForAxeViolations} from '../utils/testing'
1717

18-
// window.matchMedia() is not implemented by JSDOM so we have to create a mock:
19-
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
20-
Object.defineProperty(window, 'matchMedia', {
21-
writable: true,
22-
value: jest.fn().mockImplementation(query => ({
23-
matches: false,
24-
media: query,
25-
onchange: null,
26-
addListener: jest.fn(), // deprecated
27-
removeListener: jest.fn(), // deprecated
28-
addEventListener: jest.fn(),
29-
removeEventListener: jest.fn(),
30-
dispatchEvent: jest.fn(),
31-
})),
32-
})
33-
3418
const ResponsiveUnderlineNav = ({
3519
selectedItemText = 'Code',
3620
loadingCounters = false,

packages/react/src/__tests__/ThemeProvider.test.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import 'jest-styled-components'
44
import React from 'react'
55
import {Text, ThemeProvider, useColorSchemeVar, useTheme} from '..'
66

7-
// window.matchMedia() is not implemented by JSDOM so we have to create a mock:
8-
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
9-
Object.defineProperty(window, 'matchMedia', {
10-
writable: true,
11-
value: jest.fn().mockImplementation(query => ({
12-
matches: false,
13-
media: query,
14-
onchange: null,
15-
addListener: jest.fn(), // deprecated
16-
removeListener: jest.fn(), // deprecated
17-
addEventListener: jest.fn(),
18-
removeEventListener: jest.fn(),
19-
dispatchEvent: jest.fn(),
20-
})),
21-
})
22-
237
const exampleTheme = {
248
colors: {
259
text: '#f00',

packages/react/src/utils/test-helpers.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,20 @@ if (typeof document !== 'undefined') {
4444
if (global.Element.prototype.scrollIntoView === undefined) {
4545
global.Element.prototype.scrollIntoView = jest.fn()
4646
}
47+
48+
// window.matchMedia() is not implemented by JSDOM so we have to create a mock:
49+
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
50+
// eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope
51+
Object.defineProperty(window, 'matchMedia', {
52+
writable: true,
53+
value: jest.fn().mockImplementation(query => ({
54+
matches: false,
55+
media: query,
56+
onchange: null,
57+
addListener: jest.fn(), // deprecated
58+
removeListener: jest.fn(), // deprecated
59+
addEventListener: jest.fn(),
60+
removeEventListener: jest.fn(),
61+
dispatchEvent: jest.fn(),
62+
})),
63+
})

0 commit comments

Comments
 (0)