简体   繁体   English

酵素不浅渲染子

[英]Enzyme not shallow rendering child

I am trying to test that a child component exists by shallow-rendering it with Enzyme.我试图通过使用酶对其进行浅渲染来测试子组件是否存在。 However, it does not appear to be getting past the return of the component and instead shallow-renders <undefined /> .然而,它似乎并没有超过组件的返回,而是浅渲染<undefined />

MainComponentContainer.js主组件容器.js

import PropTypes from 'prop-types'
import React from 'react'
import createReactClass from 'create-react-class'
import ImmutablePropTypes from 'react-immutable-proptypes'
import MainComponent from './MainComponent'
import {addSelection} from '../../actions/betslip'
import {connect} from 'react-redux'

export const MainComponentContainter = createReactClass({
  displayName: 'MainComponentCont',
  propTypes: {
    displayMode: PropTypes.string,
    other: ImmutablePropTypes.map,
    addSelection: PropTypes.func,
    prices: ImmutablePropTypes.map,
    selections: ImmutablePropTypes.map,
  },

  render() {
    return (
      <div>
        {this.props.other.valueSeq().map(this.renderMain)}
      </div>
    )
  },

  renderMain(other) {
    const yesOutcome = other.get('markets').first().get('outcomes').first()
    const newPrice = this.props.prices.getIn([yesOutcome.get('id'), 'price'])

    if (newPrice) {
      return (
        <MainComponent key={other.get('id')}
                    ...some other props/>
      )
    }
    return null
  }
})

const mapStateToProps = () => {
  return (state) => {
    const displayMode = state.ui.get('displayMode')
    const selections = state.stp.get('selections')
    const prices = state.catalog.get('prices')
    const other = state.catalog.get('other')

    return {
      displayMode,
      other,
      prices,
      selections,
    }
  }
}

const mapDispatchToProps = {
  addSelection,
}

export default connect(mapStateToProps, mapDispatchToProps)(MainComponentContainer)

MainComponent.js主组件.js

This just basically returns another component to the above component.这基本上只是将另一个组件返回到上述组件。

return (
    <div style={[styles.container, styles.container[displayMode]]}>
      <div style={[styles.logo, styles.logo[displayMode]]}>
        {renderLogo(displayMode, quoteExtraLogo)}
        {displayMode === 'mobile' &&
          renderButton({displayMode, selected, suspended, clickHandler})}
      </div>
      <div style={[styles.content, styles.content[displayMode]]}>
        <h2 style={[styles.headline, styles.headline[displayMode]]}>
          {title}
        </h2>
        <div style={[styles.offer, styles.offer[displayMode]]}>
          <div style={[styles.details, styles.details[displayMode]]}>
            <p style={[styles.market, styles.market[displayMode]]}>
              {text}
            </p>
            <div>
              <p style={[styles.improvedOdds, styles.improvedOdds[displayMode]]}>
                <span style={styles.improvedOddsAt}>a</span> {newPrice}
              </p>
              <p style={[styles.previousOdds, styles.previousOdds[displayMode]]}>
                invece di{' '}
                <span className="strikethrough">
                  {oldPrice}
                </span>
              </p>
            </div>
          </div>
          {displayMode === 'desktop' &&
            renderButton({displayMode, selected, suspended, clickHandler})}
        </div>
      </div>
    </div>
  )

Test测试

describe.only('MainComponentContainer Component', () => {

  beforeEach(() => {
    sandbox = sinon.sandbox.create()
    addSelectionSpy = sinon.spy()
  })

  afterEach(() => {
    sandbox.restore()
  })

  function getOutput({
    displayMode = 'mobile',
    other = mockData,
    addSelection = spy,
    prices = pricesMock,
    selections = selectionsMock,
  } = {}) {

    return shallow(
      <MainComponentContainer
        displayMode = {displayMode}
        other = {mockData}
        addSelection = {addSelection}
        prices = {prices}
        selections = {selections}
      />
    )
  }

  it('should include a MainComponent component', () => {
    const pb = getOutput().find('MainComponent')
    expect(pb.length).to.equal(1)
  })

When doing the above test (should include a MainComponent component), I get the following error:进行上述测试时(应包含MainComponent组件),出现以下错误:

 AssertionError: expected 0 to equal 1 + expected - actual -0 +1

However I have logged out getOutput().debug() , and it returns <div><undefined /></div> .但是我已经注销了getOutput().debug() ,它返回<div><undefined /></div>

The shallow renderer is intentionally limited to operating on only the root component so as to make the test more isolated. shallow渲染器有意限制在仅对根组件进行操作,以便使测试更加隔离。 In the case of decorators or "wrapped" components like this, the wrapped component is not what we want to test.在装饰器或像这样“包装”的组件的情况下,包装的组件不是我们想要测试的。 Since MainComponentContainer is a HOC, you face this problem.由于MainComponentContainer是一个 HOC,你会面临这个问题。

There are two ways to get around this problem, either有两种方法可以解决这个问题,要么

First export the undecorated component首先导出未修饰的组件

export default connect(mapStateToProps, mapDispatchToProps)(MainComponentContainer)
export {MainComponentContainer as ComponentContainer};

and test like并测试像

return shallow(
  <ComponentContainer
    displayMode = {displayMode}
    other = {mockData}
    addSelection = {addSelection}
    prices = {prices}
    selections = {selections}
  />
)

or use .dive或使用.dive

  it('should include a MainComponent component', () => {
    const pb = getOutput().dive().find('MainComponent')
    expect(pb.length).to.equal(1)
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM