-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.spec.js
174 lines (156 loc) · 6.21 KB
/
index.spec.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
const cronBomb = require('./index');
describe('explode()', () => {
it("accepts data as a single object", () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const data = {
title: 'Lord Of The Fries',
cron: '10 0 * * 1-5', // Every weekday at 11am
};
const expected = [
{ title: 'Lord Of The Fries', cron: '2020-01-01T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-02T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-03T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-06T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-07T00:10:00.000Z' }
];
const received = cronBomb.explode(data, {start, end, utc:true});
expect(received).toEqual(expected);
});
it('accepts data as an array of objects', () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const data = [
{
title: 'Lord Of The Fries',
cron: '10 0 * * 1-5', // Every weekday at 11am
},
{
title: 'Shift Eatery',
cron: '10 0 * * 1-5', // Every weekday at 11am
},
];
const expected = [
{ title: 'Lord Of The Fries', cron: '2020-01-01T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-02T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-03T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-06T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-07T00:10:00.000Z' },
{ title: 'Shift Eatery', cron: '2020-01-01T00:10:00.000Z' },
{ title: 'Shift Eatery', cron: '2020-01-02T00:10:00.000Z' },
{ title: 'Shift Eatery', cron: '2020-01-03T00:10:00.000Z' },
{ title: 'Shift Eatery', cron: '2020-01-06T00:10:00.000Z' },
{ title: 'Shift Eatery', cron: '2020-01-07T00:10:00.000Z' }
];
const received = cronBomb.explode(data, {start, end, utc:true});
expect(received).toEqual(expected);
});
it("fails if the data object doesn't have a 'cron' field", () => {
const data = {
'nocronfield': '1 1 * * 0',
}
expect(() => {
cronBomb.explode(data, {});
}).toThrow(ReferenceError);
});
it("fails if the the cron field isn't valid cron syntax", () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const data = {
title: 'Lord Of The Fries',
cron: '8', // crontabs expect the first entry to be a number from 0-7
};
// Technically, this should throw a RangeError, but the error is thrown by
// the cronparser library, not cronbomb, so I don't have much control over
// it.
expect(() => {
cronBomb.explode(data, {start, end});
}).toThrow(Error);
});
it("checks the correct field if $field is passed in", () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const field = 'blah';
const data = {
title: 'Lord Of The Fries',
[field]: '10 0 * * 1-5', // Every weekday at 11am
};
const expected = [
{ title: 'Lord Of The Fries', [field]: '2020-01-01T00:10:00.000Z' },
{ title: 'Lord Of The Fries', [field]: '2020-01-02T00:10:00.000Z' },
{ title: 'Lord Of The Fries', [field]: '2020-01-03T00:10:00.000Z' },
{ title: 'Lord Of The Fries', [field]: '2020-01-06T00:10:00.000Z' },
{ title: 'Lord Of The Fries', [field]: '2020-01-07T00:10:00.000Z' }
];
const received = cronBomb.explode(data, {start, end, field, utc:true});
expect(received).toEqual(expected);
});
it('fails if start is later than end', () => {
const start = new Date('08 January 2020 00:00 UTC');
const end = new Date('01 January 2020 00:00 UTC');
const data = {
title: 'Lord Of The Fries',
cron: '10 0 * * 1-5', // Every weekday at 11am
};
expect(() => {
cronBomb.explode(data, {start, end});
}).toThrow(RangeError);
});
it('removes excluded dates from the returned array', () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const data = {
title: 'Lord Of The Fries',
cron: '10 0 * * 1-5', // Every weekday at 11am
};
const exclude = [
'2020-01-02T00:10:00.000Z',
'2020-01-03T00:10:00.000Z',
'2020-01-06T00:10:00.000Z',
'2020-01-07T00:10:00.000Z'
];
const expected = [
{ title: 'Lord Of The Fries', cron: '2020-01-01T00:10:00.000Z' },
];
const received = cronBomb.explode(data, {start, end, exclude, utc:true});
expect(received).toEqual(expected);
});
it('still works if exluded dates do not exist in output', () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const data = {
title: 'Lord Of The Fries',
cron: '10 0 * * 1-5', // Every weekday at 11am
};
const exclude = [
'2019-01-02T00:10:00.000Z', // Wrong year
'2019-01-03T00:10:00.000Z',
'2019-01-06T00:10:00.000Z',
'2019-01-07T00:10:00.000Z'
];
const expected = [
{ title: 'Lord Of The Fries', cron: '2020-01-01T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-02T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-03T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-06T00:10:00.000Z' },
{ title: 'Lord Of The Fries', cron: '2020-01-07T00:10:00.000Z' }
];
const received = cronBomb.explode(data, {start, end, exclude, utc:true});
expect(received).toEqual(expected);
});
});
describe('intersection()', () => {
it('gives the correct intersection of two crontabs', () => {
const start = new Date('01 January 2020 00:00 UTC');
const end = new Date('08 January 2020 00:00 UTC');
const expected = [
'2020-01-01T13:10:00.000Z',
'2020-01-02T13:10:00.000Z',
'2020-01-05T13:10:00.000Z',
'2020-01-06T13:10:00.000Z',
'2020-01-07T13:10:00.000Z'
];
const received = cronBomb.intersection({cron1: '10 0 * * 1-5', cron2: '10 0 * * 1-5', start, end});
expect(received).toEqual(expected);
});
});