-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetter.test.ts
163 lines (131 loc) · 4.59 KB
/
getter.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fs from "fs";
import OpenWeatherAPI from "../src";
// ! Remeber to specify key in key.txt file
const key = fs.readFileSync("./test/key.txt").toString().trim();
describe("Getting tests:", () => {
const weather = new OpenWeatherAPI({ key });
it("gets location", async () => {
weather.setLocationByCoordinates(40.71, -74);
let location = await weather.getLocation();
expect(location!.name).toMatch(/new york/i);
});
it("gets all locations", async () => {
let locations = await weather.getAllLocations("Lousã");
expect(locations.length).toBeGreaterThan(0);
});
it("gets current", async () => {
weather.setLocationByName("warsaw");
let current = await weather.getCurrent();
expect(typeof current.weather.temp.cur).toBe("number");
expect(current.weather.temp.min).toBeLessThanOrEqual(current.weather.temp.cur);
expect(current.weather.temp.max).toBeGreaterThanOrEqual(current.weather.temp.cur);
});
it("gets forecast", async () => {
let forecast = await weather.getForecast(20);
expect(forecast).toBeInstanceOf(Array);
if (!forecast.length) {
console.log("\t\x1b[31mEmpty forecast: ", forecast);
} else {
expect(forecast.length).toBe(20);
expect(typeof forecast[Math.floor(Math.random() * 17)].weather.rain).toBe(
"number"
);
}
});
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);
} else {
expect(minutely.length).toBe(48);
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);
} else {
expect(hourly.length).toBe(10);
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);
} else {
expect(daily.length).toBe(3);
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(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(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");
});
it("gets history", async () => {
weather.setLocationByCoordinates(49.84, 24.03);
let date = new Date().getTime() - 900000;
let history = await weather.getHistory(date);
expect(Math.round(date / 1000)).toBe(history.dtRaw);
});
it("gets current air pollution", async () => {
let pollution = await weather.getCurrentAirPollution({
locationName: "Paris",
});
expect(
Object.values(pollution.components).every((v) => typeof v === "number")
).toBeTruthy();
});
it("gets forecasted air pollution", async () => {
let pollution = await weather.getForecastedAirPollution(10, {
locationName: "Chicago",
});
expect(pollution.length).toBe(10);
expect(
Object.values(pollution[Math.floor(Math.random() * 9)].components).every(
(v) => typeof v === "number"
)
).toBeTruthy();
});
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(
(v) => typeof v === "number"
)
).toBeTruthy();
});
});