I'm writing a number of unit tests on the API layer of an application and I'm having trouble using jest to mock the various endpoints. I don't have any experience in this and many of the articles out there, aren't helping my specific scenario
I have specific module setup to which makes the requests using Axios. This is the specific call I need to mock:
export async function ajax<T>(options: AxiosRequestConfig): Promise<T> {
options.baseURL = ApiConfig.currentContext.ApiPath;
options.headers = options.headers || {};
options.headers['Accept-Language'] = Locale.resolveCulture();
options.withCredentials = true;
try {
const response = await AXIOS_ADAPTER(options);
return response.data;
}
catch (error) {
throw new ApiError(error);
}
}
And here is what my current test looks like:
import * as http from '../src/http';
import { account } from '../src/api/account';
describe('Account Credit Cards Test Suite', () => {
it('should get all saved credit cards in an account', async () => {
const spy = jest.spyOn(http, 'ajax');
const getAll = await account.creditCards.getAll();
expect(spy).toHaveBeenCalled();
expect(getAll).toBe(true);
});
});
This is an initial test to see if I could even get it to work, but unfortunately the resolveCulture call relys on the window object, since it's for a web application.
My question is how can I mock the ajax call so I can see what params get passed to and from it, in order to assert the request are behaving as expected.
Aucun commentaire:
Enregistrer un commentaire