I am trying to mock the ajax but it's not working I have tried various thing but it doesn't help .
the code scenario is like. FileName: test.js
import {ajax} from 'jquery';
const someFunction = param => new Promise((resolve) => {
ajax({
method: 'GET',
cache: false,
url: `someurl/url`,
success: (response) => {
resolve(response);
},
error: () => resolve(null),
});
});
export {someFunction}
and its calling in another function like this
testCall() {
// some codes
someFunction(param).then((response) => {
// codes
})
}
I have tried to mock like these 3 ways
spyOn($, 'ajax').and.callFake( () =>{
var d = $.Deferred();
d.resolve({});
return d.promise();
});
--------------
spyOn($, 'ajax').and.callFake( (promise) => {
var d = $.Deferred();
d.reject(promise.success());
return d.promise();
});
-------------
const testing = new Promise((resolve) => {
spyOn(test, 'someFunction').and.callFake(() => { // here test is import form test.js file
resolve();
return fakeResponse;
});
})
Any help would be appreciated.