React / enzyme:如何测试子组件?

时间:2017-08-09 18:45:20

标签: javascript reactjs unit-testing enzyme

这就是我的 Messenger 组件的样子。如您所见,主要组件和列表组件。主要组件默认导出。 有了这一切,我的应用程序中的所有内容都按预期工作。

/imports/ui/components/messenger.jsx

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Container, Segment, Loader, Header } from 'semantic-ui-react'

class Messenger extends Component {
  static get propTypes () {
    return {
      data: PropTypes.array,
      articleId: PropTypes.string,
      isLoading: PropTypes.bool
    }
  }

  render () {
    const { data, articleId, isLoading } = this.props

    if (isLoading) { return (<Loader active inverted size='massive' className='animated fadeIn' />) }
    if (articleId) { return (<MessengerList data={data} articleId={articleId} />) }

    return (
      <Container>
        <Segment id='' className='m-b-1'>
          <Header as='h1'>Title</Header>
          <MessengerList data={data} />
        </Segment>
      </Container>
    )
  }
}

class MessengerList extends Component {
  /* ... */
}

export default Messenger

现在我想用酶对主要成分进行一些单元测试。我就是这样做的,但最后一次测试失败了,因为MessengerList没有定义。那应该怎么做呢。

import React from 'react'
import { expect } from 'meteor/practicalmeteor:chai'
import { shallow } from 'enzyme'

import { Container, Loader } from 'semantic-ui-react'
import Messenger from '/imports/ui/components/messenger.jsx'

describe('<Messenger />', () => {
  const defaultProps = {
    data: [],
    articleId: '',
    isLoading: true
  }

  it('should show <Loader /> while loading data', () => {
    const wrapper = shallow(<Messenger {...defaultProps} />);
    expect(wrapper.exists()).to.be.true
    expect(wrapper.find(Loader).length).to.equal(1)
  })
  it('should show <Container /> data has been loaded', () => {
    defaultProps.isLoading = false
    const wrapper = shallow(<Messenger {...defaultProps} />);
    expect(wrapper.find(Container).length).to.equal(1)
    expect(wrapper.find(Loader).exists()).to.be.false
  })
  it('should show <MessengerList /> if articleID is given', () => {
    defaultProps.articleID = 'dummy'
    defaultProps.isLoading = false
    const wrapper = shallow(<Messenger {...defaultProps} />);
    expect(wrapper.find(MessengerList).length).to.equal(1)
    expect(wrapper.find(Loader).exists()).to.be.false
  })
})

我不想更改export default Messenger

1 个答案:

答案 0 :(得分:4)

导出您的MessengerList类....

export class MessengerList extends Component {
  /* ... */
}

然后在测试中做....

import React from 'react'
import { expect } from 'meteor/practicalmeteor:chai'
import { shallow } from 'enzyme'

import { Container, Loader } from 'semantic-ui-react'
import Messenger, { MessengerList } from '/imports/ui/components/messenger.jsx';

describe('<Messenger />', () => {
  let wrapper;
  const defaultProps = {
    data: [],
    articleId: '',
    isLoading: true
  }

  beforeEach(() => {
    // render the component once up here in this block. It runs before each test.
    wrapper = shallow(<Messenger {...defaultProps} />);
  });

  it('should show <Loader /> while loading data', () => {
    expect(wrapper.exists()).to.be.true
    expect(wrapper.find(Loader).length).to.equal(1)
  });

  it('should show <Container /> data has been loaded', () => {
    defaultProps.isLoading = false

    expect(wrapper.find(Container).length).to.equal(1)
    expect(wrapper.find(Loader).exists()).to.be.false
  });

  it('should show <MessengerList /> if articleID is given', () => {
    defaultProps.articleID = 'dummy'
    defaultProps.isLoading = false

    expect(wrapper.find(MessengerList).length).to.equal(1);
    expect(wrapper.find(Loader).exists()).to.be.false
  });
});

<强>更新

理想情况下,您应该声明首先修改道具......

...
describe('and the data has loaded', () => {
  beforeEach(() => {
    defaultProps.isLoading = false;
  });

  it('should show <Container /> component', () => {
    expect(wrapper.find(Container).length).to.equal(1)
    expect(wrapper.find(Loader).exists()).to.be.false
  });
});
...