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