Skip to content

Commit

Permalink
✅ improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loloToster committed Dec 14, 2022
1 parent d33aa98 commit 2471420
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 105 deletions.
125 changes: 43 additions & 82 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import OpenWeatherAPI, { Language, Unit } from "../src";
// ! Remeber to specify key in key.txt file
const key = fs.readFileSync("./test/key.txt").toString().trim();

const weather = new OpenWeatherAPI({
key: key,
locationName: "Hong Kong",
});

describe("Error tests:", function () {
describe("Error tests:", () => {
const emptyValues = ["", 0, null, undefined, false, NaN];

const weather = new OpenWeatherAPI({
key, locationName: "Hong Kong"
});

it("handles invalid key", async () => {
try {
await weather.getCurrent({ key: "ptero" });
Expand All @@ -21,112 +20,74 @@ describe("Error tests:", function () {
}
});

it("handles wrong coordinates", async () => {
try {
weather.setLocationByCoordinates("-200" as unknown as number, 78);
} catch (err: any) {
expect(
err.message.toLowerCase().includes("invalid coordinates")
).toBeTruthy();
}
it("handles wrong coordinates", () => {
expect(
() => weather.setLocationByCoordinates("-200" as any, 78)
).toThrow(/invalid coordinates/i);
});

it("handles wrong location name", async () => {
try {
await weather.getCurrent({ locationName: "ptero" });
} catch (err: any) {
expect(err.message.toLowerCase().includes("ptero")).toBeTruthy();
}
await expect(
weather.getCurrent({ locationName: "ptero" })
).rejects.toThrow(/ptero/i);
});

it("handles wrong language", async () => {
try {
weather.setLanguage("ptero" as Language);
} catch (err: any) {
expect(err.message.toLowerCase().includes("ptero")).toBeTruthy();
}
it("handles wrong language", () => {
expect(
() => weather.setLanguage("ptero" as Language)
).toThrow(/ptero/i);
});

it("handles wrong unit", async () => {
try {
weather.setUnits("ptero" as Unit);
} catch (err: any) {
expect(err.message.toLowerCase().includes("ptero")).toBeTruthy();
}
it("handles wrong unit", () => {
expect(
() => weather.setUnits("ptero" as Unit)
).toThrow(/ptero/i);
});

it("handles unknown parameter", async () => {
try {
await weather.getCurrent({ ptero: "" } as any);
} catch (err: any) {
expect(err.message.toLowerCase().includes("ptero")).toBeTruthy();
}
await expect(
weather.getCurrent({ ptero: "" } as any)
).rejects.toThrow(/ptero/i);
});

it("handles wrong type of option argument", async () => {
try {
await weather.getCurrent("ptero" as {});
} catch (err: any) {
expect(err.message.toLowerCase().includes("provide {}")).toBeTruthy();
}
await expect(
weather.getCurrent("ptero" as {})
).rejects.toThrow(/provide {}/i);
});

it("handles empty location name", async () => {
it("handles empty location name", () => {
emptyValues.forEach((element) => {
try {
weather.setLocationByName(element as string);
expect(false).toBe(true); // TODO: fail test
} catch (err: any) {
expect(err.message.toLowerCase().includes("empty")).toBeTruthy();
}
expect(
() => weather.setLocationByName(element as string)
).toThrow(/empty/i);
});
});

it("handles empty key", async () => {
it("handles empty key", () => {
emptyValues.forEach((element) => {
try {
weather.setKey(element as string);
expect(false).toBe(true); // TODO: fail test
} catch (err: any) {
expect(err.message.toLowerCase().includes("empty")).toBeTruthy();
}
expect(
() => weather.setKey(element as string)
).toThrow(/empty/i);
});
});

it("handles future in history", async () => {
try {
await weather.getHistory(new Date().getTime() + 900000);
} catch (err: any) {
expect(
err.message.toLowerCase().includes("requested time is in the future")
).toBeTruthy();
return;
}
expect(false).toBe(true); // TODO: fail test
await expect(
weather.getHistory(new Date().getTime() + 900000)
).rejects.toThrow(/requested time is in the future/i);
});

it("handles not within 5 days in history", async () => {
try {
await weather.getHistory(new Date().getTime() - 6 * 24 * 60 * 60 * 1000);
} catch (err: any) {
expect(
err.message
.toLowerCase()
.includes("requested time is out of allowed range of 5 days back")
).toBeTruthy();
return;
}
expect(false).toBe(true); // TODO: fail test
await expect(
weather.getHistory(new Date().getTime() - 6 * 24 * 60 * 60 * 1000)
).rejects.toThrow(/requested time is out of allowed range of 5 days back/i);
});

it("handles no time in history", async () => {
try {
await expect(
// @ts-ignore
await weather.getHistory();
} catch (err: any) {
expect(err.message.toLowerCase().includes("provide time")).toBeTruthy();
return;
}
expect(false).toBe(true); // TODO: fail test
weather.getHistory()
).rejects.toThrow(/provide time/i);
});
});
49 changes: 30 additions & 19 deletions test/getter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import OpenWeatherAPI from "../src";
// ! Remeber to specify key in key.txt file
const key = fs.readFileSync("./test/key.txt").toString().trim();

const weather = new OpenWeatherAPI({
key: key,
});
describe("Getting tests:", () => {
const weather = new OpenWeatherAPI({ key });

describe("Getting tests:", function () {
it("gets location", async () => {
weather.setLocationByCoordinates(40.71, -74);
let location = await weather.getLocation();
expect(location!.name.toLowerCase().includes("new york")).toBeTruthy();
expect(location!.name).toMatch(/new york/i);
});

it("gets all locations", async () => {
Expand All @@ -29,59 +27,69 @@ describe("Getting tests:", function () {
it("gets minutely", async () => {
weather.setLocationByCoordinates(40.71, -74);
let minutely = await weather.getMinutelyForecast(48);

expect(minutely).toBeInstanceOf(Array);

if (!minutely.length) {
console.log("\t\x1b[31mEmpty minutely: ", minutely);
expect(typeof minutely).toBe("object");
} else {
expect(minutely.length).toBe(48);
expect(typeof minutely[Math.floor(Math.random() * 40)].weather.rain).toBe(
"number"
);
expect(
typeof minutely[Math.floor(Math.random() * 40)].weather.rain
).toBe("number");
}
});

it("gets hourly", async () => {
weather.setLocationByZipCode("E14,GB");
let hourly = await weather.getHourlyForecast(10);

expect(hourly).toBeInstanceOf(Array);

if (!hourly.length) {
console.log("\t\x1b[31mEmpty hourly: ", hourly);
expect(typeof hourly).toBe("object");
} else {
expect(hourly.length).toBe(10);
expect(typeof hourly[Math.floor(Math.random() * 8)].weather.rain).toBe(
"number"
);
expect(
typeof hourly[Math.floor(Math.random() * 8)].weather.rain
).toBe("number");
}
});

it("gets daily", async () => {
weather.setLocationByCoordinates(10, -40);
let daily = await weather.getDailyForecast(3);

expect(daily).toBeInstanceOf(Array);

if (!daily.length) {
console.log("\t\x1b[31mEmpty daily: ", daily);
expect(typeof daily).toBe("object");
} else {
expect(daily.length).toBe(3);
expect(typeof daily[Math.floor(Math.random() * 2)].weather.rain).toBe(
"number"
);
expect(
typeof daily[Math.floor(Math.random() * 2)].weather.rain
).toBe("number");
}
});

it("gets alerts", async () => {
weather.setLocationByName("Giza");
let alerts = await weather.getAlerts();
expect(typeof alerts).toBe("object");
expect(alerts).toBeInstanceOf(Array);
});

it("gets everything", async () => {
weather.setLocationByCoordinates(0, 0);
let everything = await weather.getEverything();

expect(typeof everything.current.weather.temp.cur).toBe("number");
expect(typeof everything.minutely).toBe("object");

expect(everything.minutely).toBeInstanceOf(Array);

expect(
typeof everything.hourly[Math.floor(Math.random() * 20)].weather.rain
).toBe("number");

expect(
typeof everything.daily[Math.floor(Math.random() * 5)].weather.rain
).toBe("number");
Expand All @@ -108,6 +116,7 @@ describe("Getting tests:", function () {
let pollution = await weather.getForecastedAirPollution(10, {
locationName: "Chicago",
});

expect(pollution.length).toBe(10);
expect(
Object.values(pollution[Math.floor(Math.random() * 9)].components).every(
Expand All @@ -119,11 +128,13 @@ describe("Getting tests:", function () {
it("gets historical air pollution", async () => {
let currentDate = new Date();
let dateFrom12HoursAgo = new Date().setHours(currentDate.getHours() - 12);

let pollution = await weather.getHistoryAirPollution(
dateFrom12HoursAgo,
currentDate,
{ coordinates: { lat: 10, lon: 10 } }
);

expect(pollution.length).toBe(12);
expect(
Object.values(pollution[Math.floor(Math.random() * 10)].components).every(
Expand Down
7 changes: 3 additions & 4 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import OpenWeatherAPI from "../src";
// ! Remeber to specify key in key.txt file
const key = fs.readFileSync("./test/key.txt").toString().trim();

const weather = new OpenWeatherAPI();
describe("Options tests:", () => {
const weather = new OpenWeatherAPI();

describe("Options tests:", function () {
it("handles key in options", async () => {
await weather.getCurrent({
locationName: "Ottawa",
key: key,
locationName: "Ottawa", key
});
});

Expand Down

0 comments on commit 2471420

Please sign in to comment.