Im trying to test an error state which get updated when no foundEnterprise match is found:
async function onSubmit (data) {
try {
const enterprisesData = await fetchData()
const userCredential = await createUserWithEmailAndPassword(
auth,
data.email,
data.password
);
const business = userCredential.user;
const foundEnterprise = enterprisesData && enterprisesData.filter(item => item.email === data.email && item.companyID === data.companyID)
if (foundEnterprise.length !== 0){
await setDoc(doc(db, "businesses", business.uid), {
businessName: data.businessName,
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
location: data.location,
postcode: data.postcode,
phoneNumber: data.phoneNumber,
business_type: data.business_type,
role: 'business',
createdAt: new Date()
});
console.log("Business user created and stored in Firestore");
navigate("/signin");
} else{
setSignUpError('No record of your company has been found. Try again.')
}
} catch (error) {
console.error("Error during signup:", error.message);
}
};
im doing this in my test file:
jest.mock('firebase/app', () => {
return {
auth: jest.fn(),
};
});
jest.mock("firebase/auth", () => ({
getAuth: () => ({}),
createUserWithEmailAndPassword: jest.fn().mockResolvedValue({ user: { uid: "123", email:'[email protected]', password:'Password123' }}),
}));
jest.mock("../firebase.js");
jest.mock("../utilities/fetchData", () => ({ fetchData: jest.fn() }));
import "@testing-library/jest-dom";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MemoryRouter } from "react-router-dom";
import { fetchData } from "../utilities/fetchData";
import BusinessSignup from "../Business/BusinessSignUp";
async function fillInForm(){
await userEvent.type(screen.getByPlaceholderText(/business name/i), "test");
await userEvent.type(screen.getByPlaceholderText(/company id/i), "RF238E2");
await userEvent.type(screen.getByPlaceholderText(/first name/i), "test");
await userEvent.type(screen.getByPlaceholderText(/last name/i), "test");
await userEvent.type(screen.getByPlaceholderText(/email address/i), "[email protected]");
await userEvent.type(screen.getByPlaceholderText(/business address/i), "Green street");
await userEvent.type(screen.getByPlaceholderText(/postcode/i), "E1 38CD");
await userEvent.type(screen.getByPlaceholderText(/phone number/i), "07889546333");
await userEvent.type(screen.getByPlaceholderText(/^password$/i), "Password123");
await userEvent.type(screen.getByPlaceholderText(/^confirm password$/i), "Password123");
await userEvent.selectOptions(screen.getByRole('combobox'), "Services");
}
async function submit(){
await userEvent.click(screen.getByRole('button', { name: /submit/i }));
}
describe('Sign up form for enterprises', ()=>{
it(' signUpError state change', async()=>{
fetchData.mockImplementation(async()=> {
return [
{email: "[email protected]", companyID: "YI294P7"}
]
})
render(
<MemoryRouter>
<BusinessSignup />
</MemoryRouter>);
await fillInForm();
await submit();
await waitFor(()=>{
const errorNode = screen.findByTestId('signup-error');
expect(fetchData).toHaveBeenCalled();
expect(errorNode).toHaveTextContent('No record of your company has been found. Try again.');
})
})
})
I am trying to simulate a mismatch between data retrieved from the API (fetchData) and user inputs so that signUpError state gets updated... The test fails and I get I get error: Cannot read properties of undefined (reading 'user')
I am very new to JEST, please help. Thank you!
I tried creating firebase/auth mock in many ways but I just keep getting the same error...