我正在使用react
with styled-components
,并jest
enzyme
用于我的测试。我在测试一个由于theme
from而不断出错的主题组件时遇到了麻烦styled-components
。
我的组件是:
const Wrapper = styled.header`
position: fixed;
top: 0;
left: 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
height: 64px;
background-color: ${({ theme }) => theme.colors.main.default};
color: ${({ theme }) => theme.colors.main.text};
`
我的测试是:
it('should render a <header> tag', () => {
const renderedComponent = shallow(<Wrapper />)
expect(renderedComponent.type()).toEqual('header')
})
开玩笑给了我这个错误:
<Wrapper /> › should render a <header> tag
TypeError: Cannot read property 'main' of undefined
at Object.<anonymous>._styledComponents2.default.header.theme (app/containers/AppBar/Wrapper.js:13:182)
基本上,它会引发错误,因为theme
它是未定义的,因此它无法读取其中的colors
属性。如何将主题传递给我的组件?