I have a function:
funtion foo() {
Spinner.setVisible(true);
await ApiService.doApiCall();
Spinner.setVisible(false);
}
I'd like to unit test the order of function calls, to make sure the spinner is being shown before the API call and hidden afterwards.
I use Jest. I tried this:
import 'jest-extended';
describe('foo', () => {
it('should make spinner visible before the api call', async () => {
const setVisibleSpy = jest.spyOn(Spinner, 'setVisible').mockImplementation(() => true);
const doApiCallSpy = jest.spyOn(ApiService, 'doApiCall').mockImplementation(() => Promise.resolve(true));
expect(setVisibleSpy).toHaveBeenCalledBefore(doApiCallSpy ); // <-- THIS WORKS!!!
})
it('should make spinner hidden afterthe api call', async () => {
const setVisibleSpy = jest.spyOn(Spinner, 'setVisible').mockImplementation(() => true);
const doApiCallSpy = jest.spyOn(ApiService, 'doApiCall').mockImplementation(() => Promise.resolve(true));
expect(setVisibleSpy).toHaveBeenCalledAfter(doApiCallSpy ); // <-- THIS DOESN'T WORK!!!
})
})
As I understand, the check after doesn't work, because the very first call of the setVisible()
function actually happen before the doApiCall()
.
How can I check that the second call of the setVisible()
function happened after the doApiCall()
call?