score:0

Accepted answer

Reference: Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise

Wrap your expect statement in setImmediate

setImmediate(() => {
    expect(spotifyMock.getUserInfo).toHaveBeenCalledTimes(1);
})

score:10

You should wrap your render-affecting calls in an async act() function as per Dan Abramov's blog post like so:

  it('calls spotify api on click', async () => {
    await act(async () => {
      wrapper = mount(<User />);
    });
    expect(spotifyMock.getUserInfo).not.toHaveBeenCalled();

    await act(async () => {
      wrapper
        .find('button')
        .last()
        .simulate('click');
    });

    wrapper.update();
    expect(spotifyMock.getUserInfo).toHaveBeenCalledTimes(1);
  });

Related Query

More Query from same tag