Skip to content

Commit ea2a45b

Browse files
Add setMediaWidth breakpoints test utility (#7348)
### WHY are these changes introduced? #7345 Refactors the `AccountConnection` component to use the new layout primitives including `AlphaCard`. `AlphaCard` uses `useBreakpoints()` which required setting up a mock to set media width for the tests. It appears setting the media width in the tests becomes necessary for any parent components using a component that uses `useBreakpoints()`. ### WHAT is this pull request doing? This adds a `setMediaWidth` test utility which can be used in tests for any components that require mocked media widths in their tests. Co-authored-by: Aaron Casanova <[email protected]>
1 parent 56c82ee commit ea2a45b

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

.changeset/perfect-pens-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': patch
3+
---
4+
5+
Added `setMediaWidth` breakpoints test utility

polaris-react/src/components/AlphaCard/tests/AlphaCard.test.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import React from 'react';
22
import {mountWithApp} from 'tests/utilities';
33
import {matchMedia} from '@shopify/jest-dom-mocks';
4-
import {
5-
BreakpointsTokenName,
6-
breakpoints,
7-
getMediaConditions,
8-
} from '@shopify/polaris-tokens';
4+
import {setMediaWidth} from 'tests/utilities/breakpoints';
95

106
import {AlphaCard} from '..';
117

12-
const mediaConditions = getMediaConditions(breakpoints);
13-
148
const heading = <p>Online store dashboard</p>;
159
const subheading = <p>View a summary of your online store performance</p>;
1610

@@ -53,20 +47,3 @@ describe('AlphaCard', () => {
5347
});
5448
});
5549
});
56-
57-
function setMediaWidth(breakpointsTokenName: BreakpointsTokenName) {
58-
const aliasDirectionConditions = Object.values(
59-
mediaConditions[breakpointsTokenName],
60-
);
61-
62-
jest.spyOn(window, 'matchMedia').mockImplementation((query) => ({
63-
matches: aliasDirectionConditions.includes(query),
64-
media: '',
65-
onchange: null,
66-
addListener: jest.fn(),
67-
removeListener: jest.fn(),
68-
addEventListener: jest.fn(),
69-
removeEventListener: jest.fn(),
70-
dispatchEvent: jest.fn(),
71-
}));
72-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
BreakpointsTokenName,
3+
breakpoints,
4+
getMediaConditions,
5+
} from '@shopify/polaris-tokens';
6+
7+
const mediaConditions = getMediaConditions(breakpoints);
8+
9+
export function setMediaWidth(breakpointsTokenName: BreakpointsTokenName) {
10+
const aliasDirectionConditions = Object.values(
11+
mediaConditions[breakpointsTokenName],
12+
);
13+
14+
jest.spyOn(window, 'matchMedia').mockImplementation((query) => ({
15+
matches: aliasDirectionConditions.includes(query),
16+
media: '',
17+
onchange: null,
18+
addListener: jest.fn(),
19+
removeListener: jest.fn(),
20+
addEventListener: jest.fn(),
21+
removeEventListener: jest.fn(),
22+
dispatchEvent: jest.fn(),
23+
}));
24+
}

0 commit comments

Comments
 (0)