Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-pens-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Set media width globally to `md` breakpoint by default for tests
34 changes: 1 addition & 33 deletions polaris-react/src/components/AlphaCard/tests/AlphaCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import React from 'react';
import {mountWithApp} from 'tests/utilities';
import {matchMedia} from '@shopify/jest-dom-mocks';
import {
BreakpointsTokenName,
breakpoints,
getMediaConditions,
} from '@shopify/polaris-tokens';
import {setMediaWidth} from 'tests/utilities/breakpoints';

import {AlphaCard} from '..';

const mediaConditions = getMediaConditions(breakpoints);

const heading = <p>Online store dashboard</p>;
const subheading = <p>View a summary of your online store performance</p>;

describe('AlphaCard', () => {
beforeEach(() => {
matchMedia.mock();
});

afterEach(() => {
matchMedia.restore();
});

it('renders children', () => {
const alphaCard = mountWithApp(
<AlphaCard>
Expand Down Expand Up @@ -53,20 +38,3 @@ describe('AlphaCard', () => {
});
});
});

function setMediaWidth(breakpointsTokenName: BreakpointsTokenName) {
const aliasDirectionConditions = Object.values(
mediaConditions[breakpointsTokenName],
);

jest.spyOn(window, 'matchMedia').mockImplementation((query) => ({
matches: aliasDirectionConditions.includes(query),
media: '',
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
}));
}
13 changes: 13 additions & 0 deletions polaris-react/tests/setup/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import {destroyAll} from '@shopify/react-testing';
import {matchMedia} from '@shopify/jest-dom-mocks';
import '@shopify/react-testing/matchers';
import './matchers';
import {setMediaWidth} from 'tests/utilities/breakpoints';

// eslint-disable-next-line jest/require-top-level-describe
beforeAll(() => {
matchMedia.mock();
setMediaWidth('breakpoints-md');
});

// eslint-disable-next-line jest/require-top-level-describe
afterAll(() => {
matchMedia.restore();
});

// eslint-disable-next-line jest/require-top-level-describe
afterEach(() => {
Expand Down
24 changes: 24 additions & 0 deletions polaris-react/tests/utilities/breakpoints.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
BreakpointsTokenName,
breakpoints,
getMediaConditions,
} from '@shopify/polaris-tokens';

const mediaConditions = getMediaConditions(breakpoints);

export function setMediaWidth(breakpointsTokenName: BreakpointsTokenName) {
const aliasDirectionConditions = Object.values(
mediaConditions[breakpointsTokenName],
);

jest.spyOn(window, 'matchMedia').mockImplementation((query) => ({
matches: aliasDirectionConditions.includes(query),
media: '',
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
}));
}