【发布时间】:2018-05-04 05:25:53
【问题描述】:
我指的是这个github project,我正在尝试编写一个KeyPad.js组件的简单测试。
我已经看到了关于这个问题的问题,一个建议的解决方案是将主题作为道具传递给组件。此解决方案不适用于酶。
在我的情况下,问题是子组件通过 ThemeProvider 接收主题并且能够使测试工作我需要向所有组件添加主题道具。
例子:
const tree = renderer.create(
<KeyPad
theme={theme}
cancel={()=> true}
confirm={()=> true}
validation={()=> true}
keyValid={()=>true} />
).toJSON();
expect(tree).toMatchSnapshot();
KeyPad 的渲染方法会这样变化,到处都是主题道具
render() {
let { displayRule, validation, label, confirm, cancel, theme, keyValid, dateFormat } = this.props
return (
<Container theme={theme}>
<Content theme={theme}>
<Header theme={theme}>
<CancelButton theme={theme} onClick={cancel}>
<MdCancel />
</CancelButton>
<Label>{label}</Label>
<ConfirmButton theme={theme} onClick={() => confirm(this.state.input)} disabled={!validation(this.state.input)}>
<MdCheck />
</ConfirmButton>
</Header>
<Display
theme={theme}
value={this.state.input}
displayRule={displayRule}
dateFormat={dateFormat}
cancel={this.cancelLastInsert} />
<Keys>
{[7, 8, 9, 4, 5, 6, 1, 2, 3, '-', 0, '.'].map( key => (
<Button
theme={theme}
key={`button-${key}`}
theme={theme}
click={(key) => this.handleClick(key) }
value={key}
disabled={!keyValid(this.state.input, key, dateFormat)} />
))}
</Keys>
</Content>
</Container>
)
}
我不喜欢这个解决方案。有人可以帮我解决这个问题吗?
谢谢
【问题讨论】:
标签: javascript reactjs jestjs styled-components