简体   繁体   English

开始使用Enzyme和Jest测试React组件

[英]Getting started testing React components with Enzyme and Jest

Unfortunately, where I am working, we do not do unit tests on React Components. 不幸的是,在我工作的地方,我们不对React Components进行单元测试。 As I am fairly new to the industry as well, I have no experience whatsoever writing unit tests for software. 由于我对这个行业也很陌生,所以我没有经验为软件编写单元测试。 This puts me in an odd predicament when trying to learn on my own, as the examples I have seen online are either poorly explained, or just not meant for beginners. 这让我在尝试独立学习时陷入了一种奇怪的困境,因为我在网上看到的例子要么解释得不好,要么就不适合初学者。 I recently checked out testing React Components with Enzyme and Jest, and thought the combination looked promising. 我最近检查了用酶和Jest测试React组件,并认为这种组合看起来很有希望。

My goal here is this: I would like to find out the correct way to test if React props are working correctly all the way from the parent to the child component. 我的目标是:我想找出正确的方法来测试React道具是否从父组件到子组件一直正常工作。 I am using Jest and Enzyme, so the solution should use best practices with these two modules. 我正在使用Jest和Enzyme,因此解决方案应该使用这两个模块的最佳实践。

For simplicity's sake, I wanted to pose this question around two example components. 为简单起见,我想围绕两个示例组件提出这个问题。 Instead of names you would never see in the real world, let's pretend that these components when used in tandem, will produce a list of users to the screen. 而不是在现实世界中你永远看不到的名字,让我们假装这些组件在串联使用时会产生一个用户列表到屏幕上。 The components will be named: 组件将命名为:

  1. UserList (The Parent) UserList(父母)
  2. UserListItem (The Child) UserListItem(孩子)

Here is the UserList React component. 这是UserList React组件。 To hopefully try to simplify this example, I just put the data that's going to be passed to the child in local state. 为了有希望尝试简化这个例子,我只是将要传递给子节点的数据放在本地状态。 Let's assume that this data will always be an array of objects. 我们假设这个数据总是一个对象数组。

import React, { Component } from 'react'
import UserListItem from './user-list-item'

export default class UserList extends Component {
  constructor(props) {
    super(props)

    this.state = {
      users: [
        {
          userName: 'max',
          age: 24
        },
        {
          userName: 'susan',
          age: 28
        }
      ]
    }
  }

  renderUsers = (list) => {
    return this.state.users.map(user => <UserListItem key={user.userName} user={user} />)
  }

  render() {
    return (
      <ul>
        {this.renderUsers()}
      </ul>
    )
  }
}

Now, here is the child component, UserListItem 现在,这是子组件UserListItem

import React, { Component } from 'react'

const UserListItem = ({ user }) => {
  return (
    <li>
      <div>
        User: {user.userName}
      </div>
      <div>
        Age: {user.age}
      </div>
    </li>
  )
}

export default UserListItem

From reading various tutorials online, I see that shallow rendering via Enzyme is the preferred way to go. 通过在线阅读各种教程,我发现通过Enzyme的浅层渲染是首选方式。 I already understand how that works, but what I am really looking for is an end to end and thorough test of the props. 我已经明白了它是如何工作的,但我真正想要的是对道具进行端到端的彻底测试。

Also, is it enough to just check if a React component is being passed a specific prop name? 此外,仅仅检查React组件是否正在传递特定的道具名称是否足够? Or do we also need to create some type of fake array with dummy data in it to give to the child component? 或者我们是否还需要创建一些带有虚拟数据的伪数组来提供给子组件?

For your current component I'd write a test that makes sure the elements on the state are being rendered correctly, for example: 对于您当前的组件,我会编写一个测试,以确保正确呈现状态中的元素,例如:

it('should render two UserListItems', () => {
  const cmp = shallow(<UserList />);

  expect(cmp.find(UserList).length).toBe(2);
})

I'd also test if the data sent to the children is correct. 我还会测试发送给孩子的数据是否正确。 In this case the users state of UserList should show up in the props of each child UserListItem . 在这种情况下, UserListusers状态应该显示在每个子UserListItem的props中。

it('should send the correct data', () => {
  const user = { userName: 'test', age: 22 };
  const cmp = shallow(<UserList />);
  cmp.setState({ users: [user] });

  expect(cmp.find(UserListItem).props().user).toBe(user);
})

For the UserListItem component, I'd tests that the name and age are being rendered correctly. 对于UserListItem组件,我将测试正确呈现名称和年龄。

it('should render name and age', () => {
  const user = { userName: 'test', age: 22 };
  const cmp = shallow(<UserListItem user={user} />);


  expect(cmp.find('div[children="User: test"]').length).toBe(1);
  expect(cmp.find('div[children="Age: 22"]').length).toBe(1);
})

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

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