Skip to content

Commit 742999f

Browse files
authored
Fix matchMedia not present in jest test (ant-design#7344)
close ant-design#6560 close ant-design#3308 close ant-design#7341
1 parent d84452e commit 742999f

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

components/carousel/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// matchMedia polyfill for
2-
// https://github.com/WickyNilliams/enquire.js/issues/82
1+
import React from 'react';
32
import debounce from 'lodash.debounce';
43

4+
// matchMedia polyfill for
5+
// https://github.com/WickyNilliams/enquire.js/issues/82
56
if (typeof window !== 'undefined') {
67
const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
78
return {
@@ -15,9 +16,11 @@ if (typeof window !== 'undefined') {
1516
};
1617
window.matchMedia = window.matchMedia || matchMediaPolyfill;
1718
}
18-
19-
import SlickCarousel from 'react-slick';
20-
import React from 'react';
19+
// Use require over import (will be lifted up)
20+
// make sure matchMedia polyfill run before require('react-slick')
21+
// Fix https://github.com/ant-design/ant-design/issues/6560
22+
// Fix https://github.com/ant-design/ant-design/issues/3308
23+
const SlickCarousel = require('react-slick').default;
2124

2225
export type CarouselEffect = 'scrollx' | 'fade';
2326
// Carousel

tests/setup.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ import { jsdom } from 'jsdom';
22

33
// fixed jsdom miss
44
if (typeof window !== 'undefined') {
5-
const matchMediaPolyfill = function matchMediaPolyfill() {
6-
return {
7-
matches: false,
8-
addListener() {
9-
},
10-
removeListener() {
11-
},
12-
};
13-
};
14-
window.matchMedia = window.matchMedia || matchMediaPolyfill;
15-
165
const documentHTML = '<!doctype html><html><body><div id="root"></div></body></html>';
176
global.document = jsdom(documentHTML);
187
global.window = document.parentWindow;

typings/custom-typings.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,6 @@ declare module "*.json" {
9090
export default value;
9191
}
9292

93-
declare module "prop-types"
93+
declare module "prop-types"
94+
95+
declare function require(name: string): any;

0 commit comments

Comments
 (0)