React&Jest,如何测试更改状态和检查另一个组件

Leo*_*ban 22 javascript testing reactjs jestjs

反应 - 测试实用程序文档

我有一个Login组件,如果为true ,将显示Notification组件this.state.error.

我现在正在写一个Jest测试来测试它.

import React from 'react'
import ReactTestUtils from 'react-dom/test-utils';
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import Login from './Login'
import Notification from '../common/Notification'

describe('<Login /> component', () => {
    it('should render', () => {
        const loginComponent = shallow(<Login />);
        const tree = toJson(loginComponent);
        expect(tree).toMatchSnapshot();
    });

    it('should contains the words "Forgot Password"', () => {
        const loginComponent = shallow(<Login />);
        expect(loginComponent.contains('Forgot Password')).toBe(true);
    });

    // This test fails
    it('should render the Notification component if state.error is true', () => {
        const loginComponent = ReactTestUtils.renderIntoDocument(
            <Login />
        );

        const notificationComponent = ReactTestUtils.renderIntoDocument(
            <Notification />
        );

        loginComponent.setState({
            error: true
        }, expect(ReactTestUtils.isDOMComponent(notificationComponent)).toBe(true));
    });
});
Run Code Online (Sandbox Code Playgroud)

然而目前测试失败了,我不知道为什么

在此输入图像描述

在我的代码的最后一部分,我也试过这个无济于事

loginComponent.setState({
        error: true
    }, expect(ReactTestUtils. isElement(notificationComponent)).toBe(true));
Run Code Online (Sandbox Code Playgroud)

https://facebook.github.io/react/docs/test-utils.html

我的Login组件的render()

render() {
    const usernameError = this.state.username.error;
    const error = this.state.error;
    const errorMsg = this.state.errorMsg;

    return (
        <div className="app-bg">
            { error &&
                <Notification message={ errorMsg } closeMsg={ this.closeMessage }/>
            }
            <section id="auth-section">
                <header>
                    <img src="static/imgs/logo.png"/>
                    <h1>tagline</h1>
                </header>
Run Code Online (Sandbox Code Playgroud)

在将state.error设置为true之后,还尝试使用此方法测试Notification组件

it('should render the Notification component if state.error is true', () => {
    const loginComponent = ReactTestUtils.renderIntoDocument(
        <Login />
    );

    const notificationComponent = ReactTestUtils.renderIntoDocument(
        <Notification />
    );

    // loginComponent.setState({
    //  error: true
    // }, expect(ReactTestUtils.isDOMComponent(notificationComponent)).toBe(true));

    const checkForNotification = () => {
        const login = shallow(<Login />);
        expect(login.find(Notification).length).toBe(1);
    };

    loginComponent.setState({
        error: true
    }, checkForNotification());
});
Run Code Online (Sandbox Code Playgroud)

但那次测试也失败了.

也试过了 const login = mount(<Login />);


其他人使用Jest和React Test Utilities遇到问题?

Leo*_*ban 45

弄清楚了!不需要React Test Utilities

it('should render the Notification component if state.error is true', () => {
    const loginComponent = shallow(<Login />);
    loginComponent.setState({ error: true });
    expect(loginComponent.find(Notification).length).toBe(1);
});
Run Code Online (Sandbox Code Playgroud)

这将在Login组件中将error的状态设置为true ,然后检查Login组件是否包含Notification组件.

  • 更新 useState 状态怎么样?我真的需要那个。 (4认同)