【问题标题】:Jest with styled components themeProvider使用样式化组件主题提供者开玩笑
【发布时间】: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


    【解决方案1】:

    我只需解构回调的props 参数并为theme 添加一个默认值。这样,您可能从theme 访问的任何道具都不会与Cannot read property ... of undefined 一起抛出,而是将返回undefined。 它会打乱你的风格,但我认为你在单元测试中不会太在意。

    ...
    color: ${({ theme = {} }) => theme.background};
    ...
    

    【讨论】:

      【解决方案2】:

      据我所知,这是完全合理的。 库中的每个组件都有一个主题道具,因此您可以像这样设置它。

      幸运的是它不使用上下文,否则你会离家更远。为什么不使用上下文:https://reactjs.org/docs/context.html

      如果你不使用道具,你最终会将组件耦合到另一个外部库。例如:Redux 或 mobx 商店。这意味着它不再是真正的组件

      虽然看起来很糟糕。如果您真的想制作一个单独的组件,那么这是要走的路。

      【讨论】:

      • 您是否建议在所有组件中使用上下文而不是 ThemeProvider?我不明白为什么与 StyledComponent 耦合会使组件不再正确。样式可以被覆盖。
      • @Pietro 不建议按照您的方式保留它。如果您希望 &lt;KeyPad/&gt; 成为真正的反应组件,这是最好的解决方案
      猜你喜欢
      • 2021-09-04
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2021-07-17
      • 2019-02-16
      相关资源
      最近更新 更多