当我尝试测试替换或推送时,我要么得到代表历史的空对象,要么当我使用模拟时,它永远不会被调用。目前我得到这个:无法监视替换属性,因为它不是函数;未定义给定,或者当我使用模拟时它不会被调用。
const submitHandler = async e => {
e.preventDefault();
const apiUrl = 'http://localhost:8080/auth/signup'
const signupData = {
email,
password,
matchPassword,
username
}
const responseData = await postData(apiUrl,signupData,'')
if (responseData === undefined) {
toggleValidationErrors([
{
param: 'server-error',
msg: `Server isn't availdable.Please try again later!`
}
]);
} else if (responseData.validationErrors) {
toggleValidationErrors(responseData.validationErrors);
} else {
history.replace(`/login`);
}
};
import React from 'react'
import {render,fireEvent, getByValue,waitForElement} from 'react-testing-library'
import { BrowserRouter } from 'react-router-dom';
import SignupForm from './SignupForm'
describe('<SignupForm />',() => {
const username = "testUser";
const email = "testEmail@email.com";
const password = 'testpass1012'
const matchPassword = 'testpass1012'
const history = { replace: jest.fn() };
const { container, getByPlaceholderText, getByValue,getByText } = render (<SignupForm />,{wrapper:BrowserRouter})
beforeEach(() => {
const submitButton = getByText('SIGN UP')
fireEvent.click(submitButton)
})
it('should call history.replace',() => {
const replaceSpy = jest.spyOn(history, 'replace');
expect(replaceSpy).toBeCalled()
replaceSpy.mockRestore()
})
})