Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / propertiesToFrequencyModel.spec.js
blobcb420a95f06d3be9eeb20ba012a2f33f1daf44a4
1 import { END_TYPE, FREQUENCY, MONTHLY_TYPE, WEEKLY_TYPE } from '@proton/shared/lib/calendar/constants';
3 import { propertiesToFrequencyModel } from './propertiesToFrequencyModel';
4 import { getInitialFrequencyModel } from './state';
6 jest.mock('@proton/shared/lib/helpers/setupCryptoWorker', () => ({
7     __esModule: true,
8     loadCryptoWorker: jest.fn(),
9 }));
11 const startModel = {
12     date: new Date(2020, 0, 20),
14 const dummyFrequencyModel = (rrule) => ({
15     ...getInitialFrequencyModel(startModel.date),
16     vcalRruleValue: rrule.value,
17 });
19 describe('frequency properties to model, daily recurring rule', () => {
20     test('non-custom: single day, recurring never ends', () => {
21         const rrule = {
22             value: {
23                 freq: 'DAILY',
24             },
25         };
26         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
27             ...dummyFrequencyModel(rrule),
28             type: FREQUENCY.DAILY,
29             frequency: FREQUENCY.DAILY,
30         });
31     });
33     test('every three days, recurring never ends', () => {
34         const rrule = {
35             value: {
36                 freq: 'DAILY',
37                 interval: 3,
38             },
39         };
40         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
41             ...dummyFrequencyModel(rrule),
42             type: FREQUENCY.CUSTOM,
43             frequency: FREQUENCY.DAILY,
44             interval: 3,
45         });
46     });
48     test('every two days, recurring ends after 5 occurrences', () => {
49         const rrule = {
50             value: {
51                 freq: 'DAILY',
52                 interval: 2,
53                 count: 5,
54             },
55         };
56         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
57             ...dummyFrequencyModel(rrule),
58             type: FREQUENCY.CUSTOM,
59             frequency: FREQUENCY.DAILY,
60             interval: 2,
61             ends: {
62                 type: END_TYPE.AFTER_N_TIMES,
63                 count: 5,
64             },
65         });
66     });
68     test('recursion ends on 30th January on Europe/Athens timezone', () => {
69         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
70         const date = new Date(2020, 0, 30);
72         const rrule = {
73             value: {
74                 freq: 'DAILY',
75                 until: dateTime,
76             },
77         };
78         const startAthens = {
79             ...startModel,
80             tzid: 'Europe/Athens',
81         };
82         expect(propertiesToFrequencyModel(rrule, startAthens)).toEqual({
83             ...dummyFrequencyModel(rrule),
84             type: FREQUENCY.CUSTOM,
85             frequency: FREQUENCY.DAILY,
86             ends: {
87                 type: END_TYPE.UNTIL,
88                 count: 2,
89                 until: date,
90             },
91         });
92     });
93     test('all-day event every two days, recursion ends on 30th January', () => {
94         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: false };
95         const date = new Date(2020, 0, 30);
97         const rrule = {
98             value: {
99                 freq: 'DAILY',
100                 interval: 2,
101                 until: dateTime,
102             },
103         };
105         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
106             ...dummyFrequencyModel(rrule),
107             type: FREQUENCY.CUSTOM,
108             frequency: FREQUENCY.DAILY,
109             interval: 2,
110             ends: {
111                 type: END_TYPE.UNTIL,
112                 count: 2,
113                 until: date,
114             },
115         });
116     });
119 describe('frequency properties to model, weekly recurring rule', () => {
120     test('non-custom: single day, recurring never ends', () => {
121         const rrule = {
122             value: {
123                 freq: 'WEEKLY',
124             },
125         };
126         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
127             ...dummyFrequencyModel(rrule),
128             type: FREQUENCY.WEEKLY,
129         });
130     });
132     test('single day, every three weeks, recurring never ends', () => {
133         const rrule = {
134             value: {
135                 freq: 'WEEKLY',
136                 interval: 3,
137                 byday: ['MO'],
138             },
139         };
140         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
141             ...dummyFrequencyModel(rrule),
142             type: FREQUENCY.CUSTOM,
143             frequency: FREQUENCY.WEEKLY,
144             interval: 3,
145         });
146     });
148     test('two days, recurring never ends', () => {
149         const rrule = {
150             value: {
151                 freq: 'WEEKLY',
152                 interval: 3,
153                 byday: ['MO', 'WE'],
154             },
155         };
156         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
157             ...dummyFrequencyModel(rrule),
158             type: FREQUENCY.CUSTOM,
159             frequency: FREQUENCY.WEEKLY,
160             interval: 3,
161             weekly: {
162                 type: WEEKLY_TYPE.ON_DAYS,
163                 days: [1, 3],
164             },
165         });
166     });
168     test('three days, recurring ends after 5 occurrences', () => {
169         const rrule = {
170             value: {
171                 freq: 'WEEKLY',
172                 byday: ['MO', 'WE', 'FR'],
173                 count: 5,
174             },
175         };
176         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
177             ...dummyFrequencyModel(rrule),
178             type: FREQUENCY.CUSTOM,
179             frequency: FREQUENCY.WEEKLY,
180             weekly: {
181                 type: WEEKLY_TYPE.ON_DAYS,
182                 days: [1, 3, 5],
183             },
184             ends: {
185                 type: END_TYPE.AFTER_N_TIMES,
186                 count: 5,
187             },
188         });
189     });
191     test('three days, recursion ends on 30th January on Europe/Athens timezone', () => {
192         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
193         const date = new Date(2020, 0, 30);
195         const rrule = {
196             value: {
197                 freq: 'WEEKLY',
198                 byday: ['MO', 'WE', 'FR'],
199                 until: dateTime,
200             },
201         };
202         const startAthens = {
203             ...startModel,
204             tzid: 'Europe/Athens',
205         };
206         expect(propertiesToFrequencyModel(rrule, startAthens)).toEqual({
207             ...dummyFrequencyModel(rrule),
208             type: FREQUENCY.CUSTOM,
209             frequency: FREQUENCY.WEEKLY,
210             weekly: {
211                 type: WEEKLY_TYPE.ON_DAYS,
212                 days: [1, 3, 5],
213             },
214             ends: {
215                 type: END_TYPE.UNTIL,
216                 count: 2,
217                 until: date,
218             },
219         });
220     });
222     test('one all-day event, recursion ends on 30th January', () => {
223         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: false };
224         const date = new Date(2020, 0, 30);
226         const rrule = {
227             value: {
228                 freq: 'WEEKLY',
229                 byday: ['MO', 'WE', 'FR'],
230                 until: dateTime,
231             },
232         };
234         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
235             ...dummyFrequencyModel(rrule),
236             type: FREQUENCY.CUSTOM,
237             frequency: FREQUENCY.WEEKLY,
238             weekly: {
239                 type: WEEKLY_TYPE.ON_DAYS,
240                 days: [1, 3, 5],
241             },
242             ends: {
243                 type: END_TYPE.UNTIL,
244                 count: 2,
245                 until: date,
246             },
247         });
248     });
251 describe('frequency properties to model, monthly recurring rule', () => {
252     test('non-custom: single day, recurring never ends', () => {
253         const rrule = {
254             value: {
255                 freq: 'MONTHLY',
256             },
257         };
258         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
259             ...dummyFrequencyModel(rrule),
260             type: FREQUENCY.MONTHLY,
261             frequency: FREQUENCY.MONTHLY,
262         });
263     });
265     test('single day, every three months, recurring never ends', () => {
266         const rrule = {
267             value: {
268                 freq: 'MONTHLY',
269                 interval: 3,
270             },
271         };
272         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
273             ...dummyFrequencyModel(rrule),
274             type: FREQUENCY.CUSTOM,
275             frequency: FREQUENCY.MONTHLY,
276             interval: 3,
277         });
278     });
280     test('every month on the last Monday, lasting 5 times', () => {
281         const lastMondayStart = {
282             date: new Date(2020, 0, 27),
283         };
284         const rrule = {
285             value: {
286                 freq: 'MONTHLY',
287                 count: 5,
288                 byday: 'MO',
289                 bysetpos: -1,
290             },
291         };
292         expect(propertiesToFrequencyModel(rrule, lastMondayStart)).toEqual({
293             ...getInitialFrequencyModel(lastMondayStart.date),
294             type: FREQUENCY.CUSTOM,
295             frequency: FREQUENCY.MONTHLY,
296             monthly: {
297                 type: MONTHLY_TYPE.ON_MINUS_NTH_DAY,
298             },
299             ends: {
300                 type: END_TYPE.AFTER_N_TIMES,
301                 count: 5,
302             },
303             vcalRruleValue: rrule.value,
304         });
305     });
307     test('every other month on the 12th, recurring ends after 2 occurrences', () => {
308         const twelvethStart = {
309             date: new Date(2020, 0, 12),
310         };
311         const rrule = {
312             value: {
313                 freq: 'MONTHLY',
314                 interval: 2,
315                 bymonthday: 12,
316                 count: 2,
317             },
318         };
319         expect(propertiesToFrequencyModel(rrule, twelvethStart)).toEqual({
320             ...getInitialFrequencyModel(twelvethStart.date),
321             type: FREQUENCY.CUSTOM,
322             frequency: FREQUENCY.MONTHLY,
323             interval: 2,
324             ends: {
325                 type: END_TYPE.AFTER_N_TIMES,
326                 count: 2,
327             },
328             vcalRruleValue: rrule.value,
329         });
330     });
332     test('every second Wednesday, recursion ends on 30th January on Europe/Athens timezone', () => {
333         const secondWednesdayStart = {
334             date: new Date(2020, 0, 8),
335             tzid: 'Europe/Athens',
336         };
337         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
338         const date = new Date(2020, 0, 30);
340         const rrule = {
341             value: {
342                 freq: 'MONTHLY',
343                 byday: 'WE',
344                 bysetpos: 2,
345                 until: dateTime,
346             },
347         };
349         expect(propertiesToFrequencyModel(rrule, secondWednesdayStart)).toEqual({
350             ...getInitialFrequencyModel(secondWednesdayStart.date),
351             type: FREQUENCY.CUSTOM,
352             frequency: FREQUENCY.MONTHLY,
353             monthly: {
354                 type: MONTHLY_TYPE.ON_NTH_DAY,
355             },
356             ends: {
357                 type: END_TYPE.UNTIL,
358                 count: 2,
359                 until: date,
360             },
361             vcalRruleValue: rrule.value,
362         });
363     });
364     test('one all-day event every 12th of every 6 months, recursion ends on 30th January', () => {
365         const twelvethStart = {
366             date: new Date(2020, 0, 12),
367         };
368         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: false };
369         const date = new Date(2020, 0, 30);
371         const rrule = {
372             value: {
373                 freq: 'MONTHLY',
374                 interval: 6,
375                 bymonthday: 12,
376                 until: dateTime,
377             },
378         };
380         expect(propertiesToFrequencyModel(rrule, twelvethStart)).toEqual({
381             ...getInitialFrequencyModel(twelvethStart.date),
382             type: FREQUENCY.CUSTOM,
383             frequency: FREQUENCY.MONTHLY,
384             interval: 6,
385             ends: {
386                 type: END_TYPE.UNTIL,
387                 count: 2,
388                 until: date,
389             },
390             vcalRruleValue: rrule.value,
391         });
392     });
395 describe('frequency properties to model, yearly recurring rule', () => {
396     test('non-custom: single day, recurring never ends', () => {
397         const rrule = {
398             value: {
399                 freq: 'YEARLY',
400             },
401         };
402         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
403             ...dummyFrequencyModel(rrule),
404             type: FREQUENCY.YEARLY,
405             frequency: FREQUENCY.YEARLY,
406         });
407     });
409     test('every other year, recurring never ends', () => {
410         const rrule = {
411             value: {
412                 freq: 'YEARLY',
413                 interval: 2,
414                 bymonth: 7,
415                 bymonthday: 25,
416             },
417         };
418         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
419             ...dummyFrequencyModel(rrule),
420             type: FREQUENCY.CUSTOM,
421             frequency: FREQUENCY.YEARLY,
422             interval: 2,
423         });
424     });
426     test('every other year, recurring ends after 5 occurrences', () => {
427         const rrule = {
428             value: {
429                 freq: 'YEARLY',
430                 interval: 2,
431                 count: 5,
432                 bymonth: 7,
433                 bymonthday: 25,
434             },
435         };
436         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
437             ...dummyFrequencyModel(rrule),
438             type: FREQUENCY.CUSTOM,
439             frequency: FREQUENCY.YEARLY,
440             interval: 2,
441             ends: {
442                 type: END_TYPE.AFTER_N_TIMES,
443                 count: 5,
444             },
445         });
446     });
448     test('recursion ends on 30th January 2020 on Europe/Athens timezone', () => {
449         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
450         const date = new Date(2020, 0, 30);
452         const rrule = {
453             value: {
454                 freq: 'YEARLY',
455                 until: dateTime,
456                 bymonth: 7,
457                 bymonthday: 25,
458             },
459         };
460         const start = {
461             ...startModel,
462             tzid: 'Europe/Athens',
463         };
464         expect(propertiesToFrequencyModel(rrule, start)).toEqual({
465             ...dummyFrequencyModel(rrule),
466             type: FREQUENCY.CUSTOM,
467             frequency: FREQUENCY.YEARLY,
468             ends: {
469                 type: END_TYPE.UNTIL,
470                 count: 2,
471                 until: date,
472             },
473         });
474     });
476     test('all-day event every other year, recursion ends on 30th January 2020', () => {
477         const dateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: false };
478         const date = new Date(2020, 0, 30);
480         const rrule = {
481             value: {
482                 freq: 'YEARLY',
483                 interval: 2,
484                 until: dateTime,
485             },
486         };
487         expect(propertiesToFrequencyModel(rrule, startModel)).toEqual({
488             ...dummyFrequencyModel(rrule),
489             type: FREQUENCY.CUSTOM,
490             frequency: FREQUENCY.YEARLY,
491             interval: 2,
492             ends: {
493                 type: END_TYPE.UNTIL,
494                 count: 2,
495                 until: date,
496             },
497         });
498     });