Why doesn't the mockAxios.post.mockImplementationOnce return any data? I would like to see the errors.
it('should show errors when submitting returns a 422 response', () => {
mockAxios.post.mockImplementationOnce(() =>
Promise.resolve({
data: { errors: ['Name is required.', 'Email is required.'] },
status: 422,
})
)
addStudentForm()
.find('button.open-modal')
.simulate('click')
addStudentForm()
.find('button.submit')
.simulate('click')
expect(addStudentForm().instance().state.showModal).toBe(true)
console.log(addStudentForm().instance().state)
})
This is my state as it's there in the console.log.
{ showModal: true,
name: '',
username: '' }
On the frontend the response in event.response.data
does show me what I want to see what I expect errors :["Name is required.", "Email is required."]
but I cannot seem to mock it.
If you needed to see full context: https://github.com/freeCodeCamp/classroom-mode/blob/mock-axio/client/src/test/AddStudentForm.test.js
Interesting enough when I have the await on the
await addStudentForm()
.find('button.submit')
.simulate('click')
The expect(addStudentForm().instance().state.showModal).toBe(true) returns false.
You seems to miss done(), and that's why test is finished earlier then mocked data returned:
it('should show errors when submitting returns a 422 response', done // < --HERE ->
=> {
mockAxios.post.mockImplementationOnce(() => {
Promise.resolve({
data: { errors: ['Name is required.', 'Email is required.'] },
status: 422,
});
addStudentForm()
.find('button.open-modal')
.simulate('click')
addStudentForm()
.find('button.submit')
.simulate('click')
expect(addStudentForm().instance().state.showModal).toBe(true)
console.log(addStudentForm().instance().state);
done(); // <- HERE ->
})
})