forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-dates.test.js
More file actions
95 lines (93 loc) · 2.58 KB
/
load-dates.test.js
File metadata and controls
95 lines (93 loc) · 2.58 KB
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
const moment = require('moment')
const { stringify } = require('jest-matcher-utils')
const { loadDates } = require('../load-dates')
describe('given the date range function', () => {
let consoleOutput = []
const mockedWarn = (output) => consoleOutput.push(output)
beforeEach(() => {
console.warn = mockedWarn
consoleOutput = []
})
describe('given a successful call', () => {
it('returns the twelve months', () => {
const dateLoader = loadDates(moment)
const dates = dateLoader({
startDate: '2020-01-01',
})
const expectedDates = [
{
startDate: '2020-01-01',
},
{
startDate: '2020-02-01',
},
{
startDate: '2020-03-01',
},
{
startDate: '2020-04-01',
},
{
startDate: '2020-05-01',
},
{
startDate: '2020-06-01',
},
{
startDate: '2020-07-01',
},
{
startDate: '2020-08-01',
},
{
startDate: '2020-09-01',
},
{
startDate: '2020-10-01',
},
{
startDate: '2020-11-01',
},
{
startDate: '2020-12-01',
},
{
startDate: '2021-01-01',
},
]
expect(dates).toEqual(expectedDates)
})
})
describe('given an unsuccessful return', () => {
describe('startDate is not a string', () => {
const dateLoader = loadDates(moment)
;[123, {}, [], null, undefined, true].forEach((invalidInput) => {
it(`throws an error when generating dates ${stringify(
invalidInput,
)}`, () => {
expect(() =>
dateLoader({ startDate: invalidInput, endDate: '' }),
).toThrow(
new Error('Start date is not a valid string. Please try again.'),
)
expect(consoleOutput).toEqual([
`Error: startDate for dateRange must be of type string, instead: startDate: ${typeof invalidInput}`,
])
})
})
})
describe('startDate does not match format of YYYY-MM-DD', () => {
it('throws an error', () => {
const dateLoader = loadDates(moment)
expect(() => dateLoader({ startDate: 'string' })).toThrow(
new Error(
'Start date is not a valid format, please conform to YYYY-MM-DD. Please try again.',
),
)
expect(consoleOutput).toEqual([
`Error: startDate for dateRange must conform to format: YYYY-MM-DD, instead: startDate: string`,
])
})
})
})
})