【发布时间】:2018-10-28 06:52:13
【问题描述】:
我想弄清楚为什么我的测试无法运行。我有一个 React 功能组件,其中包含一个样式组件,该组件使用自定义方法生成一个媒体查询,该查询被导入为一个 css 帮助文件。我收到了TypeError: Cannot read property 'medium' of undefined。这是我的代码。
css/index.js
export const media = (function(){
const sizes = {
large: 996,
medium: 767,
small: 656,
};
return Object.keys(sizes).reduce((acc, label) => {
acc[label] = (...args) => css`
@media (min-width: ${sizes[label] / 16}em) {
${css(...args)}
}
`;
return acc;
}, {});
})();
我的组件
import { media } from 'css';
const MyComponent = ({ icon, title, description }) => (
<MyComponentContainer>
...
</MyComponentContainer>
);
export default MyComponent;
const MyComponentContainer = styled.article`
max-width: 300px;
width: 100%;
border: 1px solid #000;
${media.medium`
padding: 0 25px;
`}
${media.large`
max-width: 350px;
`}
`;
我的测试文件
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from 'components/MyComponent';
describe('<MyComponent />', () => {
it('should match the snapshot', () => {
const snap = shallow(<MyComponent />);
expect(snap).toMatchSnapshot();
});
});
我是反应测试的新手,并不真正了解如何模拟该功能或告诉 jest 忽略它。
我正在使用带有所需酶适配器和 jest-styled-components 测试插件的 react 16+。
【问题讨论】:
标签: javascript reactjs sinon enzyme styled-components