用Jest进行快照测试,子组件如何从父组件调用函数?

uga

我有一个用于解雇培训的基本应用程序,其中包括两个组件应用程序和用户以及一个针对用户的快照测试文件。

该测试目前通过了,但是我想测试更新父级状态的方法,但是我不知道如何进行操作,请帮助。

应用程序组件

import React from 'react'
import './App.css'
import data from './data/users-data.json'
import User from './components/User/User'

export default class App extends React.Component {
  constructor() {
    super()
    this.state = {users: data}
    this.clickFollowHandler = this.clickFollowHandler.bind(this)
    this.clickStarHandler = this.clickStarHandler.bind(this)
  }

  clickFollowHandler(id) {
    this.setState(prevState => {
        const updatedUsers = prevState.users.map(user => {
            if (user.id === id) {
              user.isFollowed === 'active' ? user.isFollowed = 'idle' : user.isFollowed = 'active'
            }
            return user
        })
        return {
            users: updatedUsers
        }
    })
  }

  clickStarHandler(id) {
    this.setState(prevState => {
        const updatedUsers = prevState.users.map(user => {
            if (user.id === id) {
              user.isStared === 'active' ? user.isStared = 'idle' : user.isStared = 'active'
            }
            return user
        })
        return {
            users: updatedUsers
        }
    })
  }

  render() {
    return (
      <div>
        {this.state.users.map(u => {
          return (
            <User 
              key={u.id}
              id={u.id}
              name={u.name}
              date={u.date}
              readingTime={u.readingTime}
              isStared={u.isStared}
              isFollowed={u.isFollowed}
              image={u.image}
              handleFollowClick={this.clickFollowHandler}
              handleStarClick={this.clickStarHandler}
            />
          )
        })}
      </div>
    )
  }
}

用户组件

import React from 'react'
import classes from './User.module.css'
import myImage from '../../assets/images/avatar.png'
import PropTypes from 'prop-types'


const User = props => {

    return(
      <div className={classes.User} key={props.id}>
        <div className={classes.name}>name: {props.name}</div>
        <button onClick={() => props.handleFollowClick(props.id)}>
          {props.isFollowed === 'active' ? 'Unfollow' : 'Follow'}
        </button>
        <input 
          className={classes.hvrIconPop}
          checked={props.isStared === 'active' ? true : false} 
          onChange={() => props.handleStarClick(props.id)}
          type='checkbox' 
        />
        <div>date: {props.date}</div>
        <div>reading time: {props.readingTime}</div>
        <img src={myImage} alt={props.name} />
      </div>
    )

}

User.propTypes = {
  handleFollowClick: PropTypes.func.isRequired,
  handleStarClick: PropTypes.func.isRequired,
}

export default User

User.test.js

import React from 'react';
import renderer from 'react-test-renderer';

import User from './User';

const users = [{
  "id": "5d552d0058f193f2795fc814",
  "isFollowed": "active",
  "isStared": "idle",
  "image": "./assets/images/avata.png",
  "readingTime": 20,
  "name": "Walton Morton",
  "date": "Aug 9"
}];

it('renders correctly when there are no users', () => {
  const tree = renderer.create(<User 
    users={[]} 
    handleFollowClick={() => 'test'}
    handleStarClick={() => {}} />).toJSON();
  expect(tree).toMatchSnapshot();
});

it('renders correctly when there is one user', () => {

  const tree = renderer.create(<User users={users}
    handleFollowClick={() => 'test'}
    handleStarClick={() => {}}
     />).toJSON();
  expect(tree).toMatchSnapshot();
 });
里克登哈恩

您通过User组件propjest.fn()向其组件传递了一个模拟函数(,然后模拟触发父操作的所有操作(上的事件或上的事件),并测试是否调用了相应的模拟函数。handleFollowClickhandleStarClickclick<button />change<input />

我个人总是将Enzyme用于此类事情,但是react-test-renderer根据以下答案,我认为这是可以使用

const mockFollowClick = jest.fn();
const mockStarClick = jest.fn();

const tree = renderer.create(<User
    {...users[0]}
    handleFollowClick={mockFollowClick}
    handleStarClick={mockStarClick}
/>)

const button = tree.root.findByType('button');
const input = tree.root.findByType('input');

button.props.onClick();
expect(mockFollowClick).toHaveBeenCalled();

input.props.onChange();
expect(mockStarClick).toHaveBeenCalled();

您甚至可以检查是否使用正确的用户ID调用了它:

button.props.onClick();
expect(mockFollowClick).toHaveBeenCalledWith("5d552d0058f193f2795fc814");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从父组件调用React / Typescript子组件函数?

使用 Jest 对 ChartJS 组件进行快照测试

在Angular 1.5中从父组件调用子组件函数

angular 4-从父组件html调用子组件函数

如何在React中从父组件调用子组件的方法

如何从父组件调用子组件中的方法?

如何使用foreach从父组件调用子组件方法

如何从父组件调用子组件中的方法?

角路由器如何从父组件调用子组件

react:从父组件调用子组件的方法

从父组件执行子(组件)函数

从父元素单击时在导入的子组件中调用函数?

React Jest匹配快照,使用子组件测试组件时崩溃

ReactJS。如何从父组件调用组件的方法

如何从父组件调用`this.props.children`函数?

由于lodash,用Vest中的Jest测试失败了子组件

快照测试的工作方式以及在React组件的Jest Snapshot测试中toMatchSnapshot()函数做什么?

React.js:从父组件传递给子组件的函数没有被调用

如何模拟正在用Jest测试的React组件中进行的API调用

从父类调用子组件方法-Angular

如何用类型和基本的React函数组件的Jest和Typescript进行测试

如何在无状态组件中执行函数以在 Jest 和 Enzyme 中进行测试

如何在Reactjs中从父组件的事件中调用子组件的方法

如何在Angular 6中从父组件调用子组件的方法

如何在蚂蚁设计框架中从父组件调用子组件中表单的onFinish方法

如何在 React Native 中的 Tab View 回滚时从父组件调用子组件方法?

Angular2:如何从父组件到子组件进行通信?

在 React 中调用从父组件传递到子组件的方法

在嵌套的反应导航组件上使用Jest进行快照测试-对象键更改,因此快照匹配失败