微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的测试会抛出警告,无法在现有状态转换期间更新开玩笑

如何解决为什么我的测试会抛出警告,无法在现有状态转换期间更新开玩笑

我正在尝试使用 Jest 和 React-Testing-Library 测试我的组件的功能。我的组件使用 React-Hook-Form 来处理表单提交。但是,即使测试通过,我还是收到此警告?

警告 -

console.error
    Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.

测试 -

it('Api is called when valid form data is submitted',async () => {
        const apiMock = _testHelper.setUpPostMock(postResponse);
        const { getByText,getByLabelText } = _testHelper.renderWithRedux(
            <CreateProfileForm isAuthenticated={true} user={{ email: 'test@email.com' }}></CreateProfileForm>,);

        const firstNameInput = getByLabelText('First Name');
        const surnameInput = getByLabelText('Surname');
        const submit = getByText('Submit');

        await waitFor(async () => {
            fireEvent.input(firstNameInput,{ target: { value: 'John' } });
            fireEvent.input(surnameInput,{ target: { value: 'Doe' } });
            fireEvent.click(submit);
        });
        expect(apiMock).toHaveBeenCalled();
    });

组件 -

import React from 'react';
import './CreateProfileForm.css';
import { useForm } from 'react-hook-form';
import Loader from 'react-loader-spinner';
import { RootStore } from '../../State/Store';
import { usedispatch,useSelector } from 'react-redux';
import { createuser } from '../../State/User/UserActions/UserActions';
import { useHistory } from 'react-router-dom';
import { CreateProfileFormProps } from '../../Models/PropTypes';

export default function CreateProfileForm({ isAuthenticated,user }: CreateProfileFormProps) {
    const userState = useSelector((state: RootStore) => state.user);

    const { register,handleSubmit,errors } = useForm();
    const dispatch = usedispatch();
    const history = useHistory();

    const onSubmit = (data: any): any => {
        dispatch(createuser(data,user.email));
    };

    if (userState.isUserCreated) {
        history.push('/profile');
    }

    if (isAuthenticated) {
        return (
            <div>
                {userState.isApiBeingCalled ? (
                    <Loader type="oval" color="black" data-testid="spinner"></Loader>
                ) : (
                    <form onSubmit={handleSubmit(onSubmit)}>
                        <div className="form-group">
                            <label htmlFor="email">Email</label>
                            <input
                                name="email"
                                type="email"
                                className="form-control"
                                id="email"
                                value={user.email}
                                disabled
                            ></input>
                            <small>This information was given to us from your authentication provider!</small>
                        </div>
                        <div className="form-group">
                            <label htmlFor="first-name">First Name</label>
                            <input
                                name="firstName"
                                type="text"
                                className="form-control"
                                id="first-name"
                                ref={register({ required: 'First Name cannot be empty' })}
                            ></input>
                            {errors.firstName && <small className="form-error-text">{errors.firstName.message}</small>}
                        </div>
                        <div className="form-group">
                            <label htmlFor="surname">Surname</label>
                            <input
                                name="surname"
                                type="text"
                                className="form-control"
                                id="surname"
                                ref={register({ required: 'Surname cannot be empty' })}
                            ></input>
                            {errors.surname && <small className="form-error-text">{errors.surname.message}</small>}
                        </div>
                        <button className="btn btn-primary" type="submit">
                            Submit
                        </button>
                    </form>
                )}
            </div>
        );
    } else {
        return <div></div>;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?