Newer
Older
import { FluidType, TimeStep } from 'enums'
import { DateTime } from 'luxon'
import { Dataload } from 'models'
import { graphData } from 'tests/__mocks__/chartData.mock'
getLagDays,
isLastDateReached,
isLastPeriodReached,
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
} from './date'
describe('date utils', () => {
describe('compareDates test', () => {
const datePrevious = DateTime.fromObject({ year: 2020, month: 11, day: 1 })
const dateLatter = DateTime.fromObject({ year: 2020, month: 11, day: 2 })
it('should return -1 if date is previous that the second one', () => {
const result = compareDates(datePrevious, dateLatter)
expect(result).toBe(-1)
})
it('should return 1 if the date is latter that the second one', () => {
const result = compareDates(dateLatter, datePrevious)
expect(result).toBe(1)
})
})
describe('isLastDateReached test', () => {
describe('case HALF_AN_Hour', () => {
const timeStep = TimeStep.HALF_AN_HOUR
it('should return true if date is latter that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ days: 1 }),
timeStep
)
expect(result).toBe(true)
})
it('should return false if date is previous that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ days: 1 }),
timeStep
)
expect(result).toBe(false)
})
})
describe('case WEEK', () => {
const timeStep = TimeStep.WEEK
it('should return true if date is latter that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ days: 1 }),
timeStep
)
expect(result).toBe(true)
})
it('should return false if date is previous that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ days: 1 }),
timeStep
)
expect(result).toBe(false)
})
})
describe('case DAY', () => {
const timeStep = TimeStep.DAY
it('should return true if date is latter that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ days: 1 }),
timeStep
)
expect(result).toBe(true)
})
it('should return false if date is previous that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ days: 1 }),
timeStep
)
expect(result).toBe(false)
})
})
describe('case MONTH', () => {
const timeStep = TimeStep.MONTH
it('should return true if date is latter that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ months: 1 }),
timeStep
)
expect(result).toBe(true)
})
it('should return false if date is previous that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ months: 1 }),
timeStep
)
expect(result).toBe(false)
})
})
describe('case YEAR', () => {
const timeStep = TimeStep.YEAR
it('should return true if date is latter that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ years: 1 }),
timeStep
)
expect(result).toBe(true)
})
it('should return false if date is previous that now', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ years: 1 }),
timeStep
)
expect(result).toBe(false)
})
})
describe('default', () => {
it('should return false for default case', () => {
const result = isLastDateReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ years: 1 }),
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
)
expect(result).toBe(false)
})
})
})
describe('IsLastPeriodReached test', () => {
describe('case HALF_AN_HOUR', () => {
const timeStep = TimeStep.HALF_AN_HOUR
it('should return false when date is previous to now + 1 day', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ days: 1 }),
timeStep
)
expect(result).toBe(false)
})
it('should return true when date is latter to date + 1 day', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ days: 1 }),
timeStep
)
expect(result).toBe(true)
})
})
describe('case WEEK', () => {
const timeStep = TimeStep.WEEK
it('should return false when date is previous to week + 1 day', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ week: 1 }),
timeStep
)
expect(result).toBe(false)
})
it('should return true when date is latter to week + 1 day', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ week: 1 }),
timeStep
)
expect(result).toBe(true)
})
})
describe('case DAY', () => {
const timeStep = TimeStep.DAY
it('should return false when date is previous to now + 1 month', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ months: 1 }),
timeStep
)
expect(result).toBe(false)
})
it('should return true when date is latter to date + 1 month', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ months: 1 }),
timeStep
)
expect(result).toBe(true)
})
})
describe('case MONTH', () => {
const timeStep = TimeStep.MONTH
it('should return false when date is previous to now + 1 year', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ years: 1 }),
timeStep
)
expect(result).toBe(false)
})
it('should return true when date is latter to date + 1 year', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ years: 1 }),
timeStep
)
expect(result).toBe(true)
})
})
describe('case YEAR', () => {
const timeStep = TimeStep.YEAR
it('should return false when date is previous to now + 5 year', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ years: 5 }),
timeStep
)
expect(result).toBe(false)
})
it('should return true when date is latter to date + 5 year', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.plus({ years: 5 }),
timeStep
)
expect(result).toBe(true)
})
})
describe('default', () => {
it('should return false', () => {
const result = isLastPeriodReached(
DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.minus({ years: 1 }),
)
expect(result).toBe(false)
})
})
})
describe('getLagDays test', () => {
it('should return 3 when there is only WATER fluid type', () => {
const result = getLagDays([FluidType.WATER])
expect(result).toBe(3)
})
it('should return 3 when there are many fluid type including WATER', () => {
const result = getLagDays([FluidType.ELECTRICITY, FluidType.WATER])
expect(result).toBe(3)
})
it('should return 2 hen there is only GAS Fluid Type', () => {
const result = getLagDays([FluidType.GAS])
expect(result).toBe(2)
})
it('should return 2 when there are many fluid type including GAS and excluding WATER', () => {
const result = getLagDays([FluidType.ELECTRICITY, FluidType.GAS])
expect(result).toBe(2)
})
it('should return when there is only ELECTRICITY Fluid Type', () => {
const result = getLagDays([FluidType.ELECTRICITY])
expect(result).toBe(1)
})
})
describe('convertDateToShortDateString test', () => {
const actualData: Dataload[] = graphData.actualData
it('should return an empty string when the TimePeriod is unknown', () => {
const result = convertDateToShortDateString(null, 0 as TimeStep)
expect(result).toBe('')
})
describe('should return the correct short date string', () => {
it('case HALF_AN_HOUR', () => {
const result = convertDateToShortDateString(
actualData,
TimeStep.HALF_AN_HOUR
)
expect(result).toBe('bilan du jeudi 01 octobre')
})
it('case WEEK', () => {
const result = convertDateToShortDateString(actualData, TimeStep.WEEK)
expect(result).toBe('bilan du 01/10 au 03/10')
})
it('case DAY', () => {
const result = convertDateToShortDateString(actualData, TimeStep.DAY)
expect(result).toBe('bilan d’octobre')
})
it('case MONTH', () => {
const result = convertDateToShortDateString(actualData, TimeStep.MONTH)
expect(result).toBe('bilan de l’année 2020')
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
})
})
})
describe('convertDateToMonthYearString test', () => {
it('should return the name of the month and the year', () => {
const date = DateTime.fromISO('2020-11-29T23:59:59.999Z', {
zone: 'utc',
})
const result = convertDateToMonthYearString(date)
expect(result).toBe('novembre 2020')
})
})
describe('getActualAnalysisDate test', () => {
it('should return the 3rd of this month if actual day >= 3', () => {
const now = DateTime.local().setZone('utc', {
keepLocalTime: true,
})
jest
.spyOn(DateTime, 'local')
.mockReturnValueOnce(now.set({ day: 3, month: 11, year: 2020 }))
const mockDate = now.startOf('day').set({ day: 3, month: 11, year: 2020 })
const result = getActualAnalysisDate()
expect(result).toEqual(mockDate)
})
it('should return the 3rd of previous month if actual day < 3', () => {
const now = DateTime.local().setZone('utc', {
keepLocalTime: true,
})
jest
.spyOn(DateTime, 'local')
.mockReturnValueOnce(now.set({ day: 2, month: 11, year: 2020 }))
const mockDate = now.startOf('day').set({ day: 3, month: 10, year: 2020 })
const result = getActualAnalysisDate()
expect(result).toEqual(mockDate)
})
})
})