我使用react
与styled-components
和jest
enzyme
为我的测试。我遇到了麻烦,因为theme
从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
属性。我如何将主题传递给我的组件?
你找到任何解决问题了吗? – rels
你曾经解决过这个问题吗? – DavidWorldpeace
嗨,很抱歉,很长的延迟!与此同时,一些公用事业公司推出了带有风格的组件。 他们推荐的主题只是将它作为道具传递。你也可以创建一个围绕“浅”方法的封装来自动执行。 ([Source](https://github.com/styled-components/jest-styled-components#theming)) – Dispix