I recently came across a great solution on How to make an AJAX call without jQuery? that I wanted to implement. However, I found it challenging to mock the Promise, the AJAX call, or both in my testing.
My initial attempt with jasmine-ajax didn't work out as expected due to a reported bug. It also appeared that this tool is designed for the browser environment only.
Additionally, I tried to mock the XMLHttpRequest object, but unfortunately, I couldn't get it to work successfully.
At this point, I'm unsure about the alternatives available to me:
function get(url) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open('GET', url);
req.onload = () => req.status === 200 ? resolve(req.response) : reject(Error(req.statusText));
req.onerror = (e) => reject(Error(`Network Error: ${e}`));
req.send();
});
}