使用带有酶的jest-styled-components'toHaveStyleRule进行测试时出错

时间:2017-06-27 18:58:47

标签: javascript unit-testing jestjs enzyme styled-components

我在我正在测试的组件中使用样式化组件,它看起来像这样:

export const StyledCheckboxGroup = styled(CheckboxGroup)`
> .ant-checkbox-group-item {
display: ${props => (props.inline ? 'inline-block' : 'block')};
}
`

我有一个看起来像这样的测试:

it('renders an inline widget with the appropriate style rules', () => {
  const options = {
    enumOptions: [
      {
        value: 'one',
        label: 'One'
      },
      {
        label: 'Two',
        value: 'two'
      }
    ],
    inline: true
  }

  const wrapper = mount(<Checkboxes options={options} />)
  const item = wrapper.find(StyledCheckboxGroup)
  const subject = item.find('.ant-checkbox-group-item')
  expect(subject).toHaveStyleRule('display', 'inline-block')
})

我希望能够使用酶的匹配来匹配我的造型规则。我有subject的两个结果,断言的输出看起来是这样的:

"Expected [object Object] to be a component from react-test-renderer, or a mounted enzyme component."

"But had an error, TypeError: Cannot read property '_renderedComponent' of undefined"

深入研究来源我发现:

const renderedComponent = received.node._reactInternalInstance._renderedComponent

是错误的位置,它可以在组件上找到_reactInternalInstance但不能找到_renderedComponent

0 个答案:

没有答案