-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather.test.js
312 lines (272 loc) · 10.1 KB
/
Weather.test.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const Weather = require('./Weather');
const { testdata } = require('./testdata');
const ForecastArray = require('./ForecastArray');
const fetchedData = `
<?xml version='1.0' encoding='ISO-8859-1'?>
<siteData>
<license>text</license>
<dateTime></dateTime>
<dateTime></dateTime>
<one attr1='foo' attr2='bar'>baz</one>
<two>bing</two>
<two>bang</two>
<three><four></four></three>
<currentConditions>
<foo>bar</foo>
</currentConditions>
<forecastGroup>
<regionalNormals>
<textSummary>text</textSummary>
</regionalNormals>
<forecast>
<period textForecastName="Today">Wednesday</period>
<textSummary>text</textSummary>
</forecast>
<forecast>
<period textForecastName="Tomorrow">Thursday</period>
<textSummary>text</textSummary>
</forecast>
</forecastGroup>
<hourlyForecastGroup>
<hourlyForecast dateTimeUTC="202101280300">
<condition>Rain</condition>
</hourlyForecast>
<hourlyForecast dateTimeUTC="202101280400">
<condition>Sunny</condition>
</hourlyForecast>
</hourlyForecastGroup>
</siteData>
`;
const forecastMap = new Map();
forecastMap.set('wednesday', { textSummary: 'text' });
forecastMap.set('thursday', { textSummary: 'text' });
forecastMap.set('202101280300', { condition: 'Rain' });
forecastMap.set('202101280400', { condition: 'Sunny' });
describe('Weather', () => {
describe('constructor(fetchedXMLWeatherData)', () => {
it('stores the original fetched weather data', () => {
const weather = new Weather(testdata);
expect(weather._originalXML).toStrictEqual(testdata);
});
it('converts, parses, and stores weather data as an object', () => {
const expected = {
one: { attr1: 'foo', attr2: 'bar', value: 'baz' },
two: [ 'bing', 'bang' ],
three: { four: null },
regionalNormals: { textSummary: 'text' },
forecast: forecastMap
};
const weather = new Weather(fetchedData);
expect(weather._data).toMatchObject(expected);
});
it('throws an informative Error if fetched data is falsy', () => {
const attempt = () => {
const weather = new Weather();
};
expect(attempt).toThrow(TypeError);
expect(attempt).toThrow(/constructor/i);
});
it('throws an informative Error if fetched data is not a string', () => {
const numberAttempt = () => {
const weather = new Weather(99);
};
const arrayAttempt = () => {
const weather = new Weather([ 9, 9 ]);
};
const objectAttempt = () => {
const weather = new Weather({ '9': 9 });
};
expect(numberAttempt).toThrow(TypeError);
expect(numberAttempt).toThrow(/string/i);
expect(arrayAttempt).toThrow(/string/i);
expect(objectAttempt).toThrow(/string/i);
});
});
describe('get all()', () => {
it('returns all data parsed', () => {
const expected = {
one: { attr1: 'foo', attr2: 'bar', value: 'baz' },
two: [ 'bing', 'bang' ],
three: { four: null },
regionalNormals: { textSummary: 'text' },
forecast: forecastMap
};
const weather = new Weather(fetchedData);
expect(weather.all).toMatchObject(expected);
});
});
describe('get raw()', () => {
it('returns the original XML data', () => {
const weather = new Weather(testdata);
expect(weather.raw).toStrictEqual(testdata);
});
});
describe('get current()', () => {
it('returns all current weather conditions', () => {
const weather = new Weather(fetchedData);
const expected = { foo: 'bar' };
expect(weather.current).toMatchObject(expected);
});
});
describe('get weekly()', () => {
it('returns a ForecastArray of the weekly weather data', () => {
const weather = new Weather(testdata);
const result = weather.weekly;
expect(result).toBeInstanceOf(ForecastArray);
expect(result.length).toStrictEqual(13);
result.forEach((forecast) => {
expect(forecast).toHaveProperty('day');
});
});
});
describe('get hourly()', () => {
it('returns a ForecastArray of the hourly weather data', () => {
const weather = new Weather(testdata);
const result = weather.hourly;
expect(result).toBeInstanceOf(ForecastArray);
expect(result.length).toStrictEqual(24);
result.forEach((forecast) => {
expect(forecast).toHaveProperty('hour');
});
});
});
describe('get date()', () => {
it('returns a Date object of the current date and time', () => {
const weather = new Weather(testdata);
const expected = new Date(Date.UTC(2021, 0, 27, 15, 0));
expect(weather.date).toMatchObject(expected);
});
});
describe('forecast(date)', () => {
describe('when passed a string', () => {
it('returns all forecast data for a given day of the week', () => {
const weather = new Weather(testdata);
const results = weather.forecast('FrIdaY');
const expected = {
textSummary: 'A mix of sun and cloud. High minus 7.',
cloudPrecip: { textSummary: 'A mix of sun and cloud.' },
abbreviatedForecast: {
iconCode: { format: 'gif', value: '02' },
pop: { units: '%' },
textSummary: 'A mix of sun and cloud'
},
temperatures: {
textSummary: 'High minus 7.',
temperature: { unitType: 'metric', units: 'C', class: 'high', value: '-7' }
},
winds: null,
humidex: null,
precipitation: { textSummary: null, precipType: { start: '', end: '' } },
relativeHumidity: { units: '%', value: '60' }
};
expect(results).toMatchObject(expected);
});
});
describe('when passed a Date object within the hourly forecast', () => {
it('returns all forecast data for the given hour', () => {
const weather = new Weather(testdata);
const date = new Date(2021, 0, 27, 15, 12);
const results = weather.forecast(date);
const expected = {
condition: 'Cloudy',
iconCode: { format: 'png', value: '10' },
temperature: { unitType: 'metric', units: 'C', value: '-4' },
lop: { category: 'Low', units: '%', value: '10' },
windChill: { unitType: 'metric', value: '-10' },
humidex: { unitType: 'metric' },
wind: {
speed: { unitType: 'metric', units: 'km/h', value: '20' },
direction: { windDirFull: 'Northwest', value: 'NW' },
gust: { unitType: 'metric', units: 'km/h' }
}
};
expect(results).toMatchObject(expected);
});
});
describe('when passed a Date object outside hourly but within weekly forecast', () => {
it('returns all forecast data for the day/night', () => {
const weather = new Weather(testdata);
const dateDaytime = new Date(2021, 0, 30, 12, 12);
const dateNighttime = new Date(2021, 0, 30, 23, 12);
const expectedDaytime = {
textSummary: 'A mix of sun and cloud. High minus 6.',
cloudPrecip: { textSummary: 'A mix of sun and cloud.' },
abbreviatedForecast: {
iconCode: { format: 'gif', value: '02' },
pop: { units: '%' },
textSummary: 'A mix of sun and cloud'
},
temperatures: {
textSummary: 'High minus 6.',
temperature: { unitType: 'metric', units: 'C', class: 'high', value: '-6' }
},
winds: null,
humidex: null,
precipitation: { textSummary: null, precipType: { start: '', end: '' } },
relativeHumidity: { units: '%', value: '55' }
};
const expectedNighttime = {
textSummary: 'Cloudy. Low minus 9.',
cloudPrecip: { textSummary: 'Cloudy.' },
abbreviatedForecast: {
iconCode: { format: 'gif', value: '10' },
pop: { units: '%' },
textSummary: 'Cloudy'
},
temperatures: {
textSummary: 'Low minus 9.',
temperature: { unitType: 'metric', units: 'C', class: 'low', value: '-9' }
},
winds: null,
humidex: null,
precipitation: { textSummary: null, precipType: { start: '', end: '' } },
relativeHumidity: { units: '%', value: '80' }
};
const resultsDaytime = weather.forecast(dateDaytime);
const resultsNighttime = weather.forecast(dateNighttime);
expect(resultsDaytime).toMatchObject(expectedDaytime);
expect(resultsNighttime).toMatchObject(expectedNighttime);
});
});
describe('when passed a date outside the range of the data', () => {
it('returns undefined', () => {
const weather = new Weather(testdata);
const date = new Date(2022, 0, 1, 1, 1);
expect(weather.forecast(date)).toBeUndefined();
});
});
describe('when passed something other than a string or Date object', () => {
it('throws an Error', () => {
const weather = new Weather(testdata);
const numberAttempt = () => {
weather.forecast(99);
};
const arrayAttempt = () => {
weather.forecast([ 9, 9 ]);
};
const objectAttempt = () => {
weather.forecast({ '9': 9 });
};
expect(numberAttempt).toThrow();
expect(arrayAttempt).toThrow();
expect(objectAttempt).toThrow();
});
});
});
describe('_makeUTCTimestamp(date)', () => {
it('returns the corresponding UTC Timestamp of the given Date', () =>{
const weather = new Weather(testdata);
const date = new Date(Date.UTC(2011, 1, 1, 1, 1));
const expected = '201102010100';
expect(weather._makeUTCTimestamp(date)).toStrictEqual(expected);
});
});
describe('_getWeekDay(date)', () => {
it('returns the expanded weekday name of the date', () => {
const weather = new Weather(testdata);
const date = new Date(Date.UTC(2011, 1, 1, 1, 1));
const expected = 'tuesday';
expect(weather._getWeekDay(date)).toStrictEqual(expected);
});
});
});