In VueJs project I have list of api request methods in apiList.ts file with below code. Also I am trying to add the unit test case for getUsers, addUser, updateUser methods but unable to mock the http methods(get/post/put). Could someone help to add the test case for this below code.
File: apiList.ts
import Vue from 'vue';
export const API_LISTS = {
getUsers: () => {
const apiUrl = `/get/users`;
return Vue.prototype.$http.get(apiUrl);
},
addUser: (requestData: any) => {
let apiUrl = `/new/user`;
return Vue.prototype.$http.post(apiUrl, requestData);
},
updateUser: (requestData: any) => {
const url = `/update/user`;
return Vue.prototype.$http.put(url, requestData);
},
}
File: apiLists.spec.ts
import { assert } from 'chai';
import {API_LISTS } from 'apiList';
describe.only('getUsers', () => {
console.log("---------------------------------step-1");
let axiosGetStub: any;
const responseData = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }];
beforeEach(() => {
console.log("---------------------------------step-2");
axiosGetStub = sinon.stub(Vue.prototype.$http, 'get').callsFake(() => Promise.resolve({ status: 200, data: responseData }));
console.log("---------------------------------step-3");
});
afterEach(() => {
console.log("---------------------------------step-4");
axiosGetStub.restore();
});
it('should call $http.get with the correct endpoint', async () => {
console.log("---------------------------------step-5");
await API_LISTS.getUsers();
expect(axiosGetStub.calledOnce).equal(true);
// expect(axiosGetStub.calledWith('/users')).equal(true);
});
it('should return the response data', async () => {
console.log("---------------------------------step-6");
const result = await API_LISTS.getUsers();
console.log("---------------------------------step-7");
// expect(result).equal(responseData);
});
});
Output: I am getting the logs step1, step2, step4 but step3, step5, step6 and step7 not getting.