Skip to content

Commit

Permalink
tests(all): 100% Code Coverage!
Browse files Browse the repository at this point in the history
  • Loading branch information
zskelton committed Feb 7, 2022
1 parent bbe6dfd commit f350cd2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
70 changes: 67 additions & 3 deletions all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,66 @@ const utils = require('./utils/index.cjs');
const weatherApiKey = require('./secret/config');

describe("Utility Tests:", () => {
it('Lets get 100%...', () => {
weatherData.wind.deg = 22;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 66;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 112;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 156;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 202;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 246;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 292;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 336;
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.wind.deg = 339;
weatherData.weather[0].description = "";
expect(utils.reportWeather(weatherData)).toBeTruthy;expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "clear sky";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "few clouds";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "scattered clouds";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "broken clouds";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "shower rain";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "rain";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "thunderstorms";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "snow";
expect(utils.reportWeather(weatherData)).toBeTruthy;
weatherData.weather[0].description = "mist";
expect(utils.reportWeather(weatherData)).toBeTruthy;
});

it('Does it Die?', () => {
// Clean Exit
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
utils.killMe();
expect(mockExit).toHaveBeenCalled();
// Fake Error
let error = {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'api.openweathermap.org'
};
utils.killMe(error);
expect(mockExit).toHaveBeenCalled();
// Fake non-network Error
error = "Random Error";
utils.killMe(error);
expect(mockExit).toHaveBeenCalled();
})

jest.mock('dns');
Expand All @@ -16,10 +72,18 @@ describe("Utility Tests:", () => {
jest.mock('http');
const locationUrl = 'http://ipinfo.io';
it('Does Data Pull?', async () => {
await expect(utils.getData(locationUrl)).resolves.toBeTruthy();
await expect(utils.getData(locationUrl)).resolves.toBeTruthy();
});

const weatherData = {
it('Do errors go to catch?', () => {
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
utils.checkConnection();
expect(mockExit).toHaveBeenCalled();
utils.getData();
expect(mockExit).toHaveBeenCalled();
});

let weatherData = {
coord: { lon: -95.9378, lat: 41.2586 },
weather: [ { id: 800, main: 'Clear', description: 'clear sky', icon: '01d' } ],
base: 'stations',
Expand Down Expand Up @@ -51,7 +115,7 @@ describe("Utility Tests:", () => {
it('Does it Show Data?', async () => {
const res = utils.reportWeather(weatherData);
expect(res).toBeTruthy();
})
});
});

// Can it process the data?
Expand Down
2 changes: 0 additions & 2 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ async function main() {
const weatherData = await utils.getData(weatherApiUrl+locData.city);
if(!weatherData) { console.warn("Error: Failed to get weather."); utils.killMe(); };

console.log(weatherData);

// Print Status
utils.reportWeather(weatherData);

Expand Down
38 changes: 27 additions & 11 deletions utils/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,40 @@ const colors = require('./../ansi_colors');

/* KILL PROCESS FUNCTION */
function killMe (error) {
error ? console.error(`${colors.red}Error: ${colors.white}${error.code} @ ${error.hostname}.${colors.reset}`) : null;
process.exit(error ? 1 : 0);
if (error) {
// Display Error Message
if (error.code && error.hostname) {
console.log(`${colors.red}Error: ${colors.white}${error.code} @ ${error.hostname}.${colors.reset}`);
} else {
console.log(`${colors.red}Error: ${colors.white}${error}${colors.reset}`);
}
process.exit(1);
}
// Clean Exit
process.exit(0);
}

/* CHECK CONNECTION FUNCTION */
const checkConnection = async(sitelist) => {
sitelist.forEach((site) => {
dns.lookup(site, err => {
if (err) { killMe(err); }
});
});
try {
sitelist.forEach((site) => {
dns.lookup(site, () => {});
});

return true;
return true;
} catch (e) {
killMe(e);
}
}

/* GET DATA FUNCTION */
async function getData(url) {
// Get Response
try {
// Catch Bad Data Format
if (!url) { throw new Error('No URL'); }

// Setup Calls
const res = await new Promise(resolve => {
http.get(url, resolve);
});
Expand Down Expand Up @@ -66,19 +81,20 @@ function reportWeather (data) {
if (d < 293) { return '◀'; } // u25c0
if (d < 337) { return '◤'; } // u25e4
if (d >= 338) { return '▲'; } // u25b2
return '';
}

// Get Weather Symbol
const getWeatherTypeSymbol = (desc) => {
switch(desc) {
case "clear sky":
return '☀️ Clear Skies'; // u2600
case "few clouds" | "scattered clouds":
case "few clouds":
case "scattered clouds":
return '☁️ Few Clouds'; // u2601
case "broken clouds":
return '⛅ Broken Clouds'; // u26c5
case "shower rain" | "rain":
case "shower rain":
case "rain":
return '☂ Rain'; // u2602
case "thunderstorms":
return '⚡️ Thunderstorms'; // u26a1
Expand Down

1 comment on commit f350cd2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 100% 165/165
🟢 Branches 100% 50/50
🟢 Functions 100% 11/11
🟢 Lines 100% 165/165

Test suite run success

7 tests passing in 1 suite.

Report generated by 🧪jest coverage report action from f350cd2

Please sign in to comment.