diff --git a/components/_util/responsiveObserve.ts b/components/_util/responsiveObserve.ts index 06e2ee2483..e017c802a7 100644 --- a/components/_util/responsiveObserve.ts +++ b/components/_util/responsiveObserve.ts @@ -77,6 +77,8 @@ const responsiveObserve = { mql, listener, }; + + listener(mql); }); }, }; diff --git a/components/descriptions/__tests__/index.test.js b/components/descriptions/__tests__/index.test.js index 6e745708e9..96cfb3e630 100644 --- a/components/descriptions/__tests__/index.test.js +++ b/components/descriptions/__tests__/index.test.js @@ -10,15 +10,6 @@ describe('Descriptions', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); - jest.spyOn(window, 'matchMedia').mockImplementationOnce(query => ({ - addListener: (listener) => { - if (query === '(max-width: 575px)') { - listener({ matches: true }); - } - }, - removeListener: jest.fn(), - })); - afterEach(() => { MockDate.reset(); errorSpy.mockReset(); diff --git a/components/grid/__tests__/index.test.js b/components/grid/__tests__/index.test.js index fe64b37d29..fedb70ccf3 100644 --- a/components/grid/__tests__/index.test.js +++ b/components/grid/__tests__/index.test.js @@ -3,15 +3,6 @@ import { render, mount } from 'enzyme'; import { Col, Row } from '..'; import mountTest from '../../../tests/shared/mountTest'; -jest.spyOn(window, 'matchMedia').mockImplementationOnce(query => ({ - addListener: (listener) => { - if (query === '(max-width: 575px)') { - listener({ matches: true }); - } - }, - removeListener: jest.fn(), -})); - describe('Grid', () => { mountTest(Row); mountTest(Col); diff --git a/tests/setup.js b/tests/setup.js index 8fddc467a3..fc447196d0 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -12,10 +12,10 @@ if (typeof window !== 'undefined') { }; global.window.scrollTo = () => {}; // ref: https://github.com/ant-design/ant-design/issues/18774 - if (!global.window.matchMedia) { + if (!window.matchMedia) { Object.defineProperty(global.window, 'matchMedia', { - value: jest.fn(() => ({ - matches: true, + value: jest.fn(query => ({ + matches: query.includes('max-width'), addListener: () => {}, removeListener: () => {}, })),