When i try to test replace or push i either get empthy object that represents history or when i use mock it is never being called. Currently i get this : Cannot spy the replace property because it is not a function; undefined given instead or when i use mock it dosnt get called.
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()
})
})