Почему mockAxios.post.mockImplementationOnce не возвращает никаких данных

Почему mockAxios.post.mockImplementationOnce не возвращает никаких данных? Я хотел бы видеть ошибки.

  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)
  })

Это мое состояние, так как оно есть в console.log.

{ showModal: true,
 name: '',
 username: '' }

На фронте ответ в event.response.data действительно показывает мне, что я хочу видеть то, что я ожидаю errors :["Name is required.", "Email is required."] но я не могу насмехаться над этим.

Если вам нужно увидеть полный контекст: https://github.com/freeCodeCamp/classroom-mode/blob/mock-axio/client/src/__test__/AddStudentForm.test.js

Достаточно интересно, когда у меня есть ожидание на

await addStudentForm()
  .find('button.submit')
  .simulate('click') 

Ожидаемое (addStudentForm(). Instance(). State.showModal).toBe(true) возвращает значение false.

1 ответ

Решение

Кажется, вы пропустили done (), и поэтому тест завершается раньше, чем возвращаются проверенные данные:

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 ->
    })


  })
Другие вопросы по тегам