module.exports.get = function(req, url, callback, errorCallback) {
https.get(getOptions(req, url), (response) => {
handleResponse(response, callback, errorCallback);
}).on('error', (e) => {
console.error('MYAPP-GET Request.', e);
if (errorCallback) {
errorCallback(e);
}
});
};
function handleResponse(response, callback, errorCallback) {
let rawData = '';
response.on('data', (chunk) => {
rawData += chunk;
});
response.on('end', () => {
if (response.statusCode >= 200 && response.statusCode < 300) {
callback(checkJSONResponse(rawData));
} else if (errorCallback) {
errorCallback(rawData);
}
});
}
function checkJSONResponse(rawData) {
if (typeof rawData === 'object') {
return rawData;
}
let data = {};
if (rawData.length > 0) {
try {
data = JSON.parse(rawData);
} catch (e) {
console.log('Response is not JSON.');
if (e) {
console.log(e);
}
data = {};
}
}
return data;
}
I am learning more about writing JUnit test cases for apps written in Javascript.
Below is what I have so far. I know it is not correct. Need help writing code to cover the above. If there are any good links to learn more about JUnit test cases please place them also in the reply. Thank You.
describe('>>> CRUD tests', () => {
it('+++ test > get method', () => {
const getSpy = jest.spyOn(crud, 'get');
const httpAPISpy = jest.spyOn(http, 'get');
getSpy.mockReturnValue(httpAPISpy.mockReturnValue());
expect(getSpy).toHaveBeenCalledTimes(1);
});
});
2
Answers