Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / modelToFrequencyProperties.spec.js
blobf920545fc119e9c7b25c0af31c4ec9bae5128f61
1 import { END_TYPE, FREQUENCY, MONTHLY_TYPE, WEEKLY_TYPE } from '@proton/shared/lib/calendar/constants';
3 import modelToFrequencyProperties from './modelToFrequencyProperties';
4 import { getInitialFrequencyModel } from './state';
6 const dummyStart = { date: new Date(2020, 0, 20), tzid: 'Europe/Athens' };
7 const dummyFrequencyModel = getInitialFrequencyModel(dummyStart.date);
9 jest.mock('@proton/shared/lib/helpers/setupCryptoWorker', () => ({
10     __esModule: true,
11     loadCryptoWorker: jest.fn(),
12 }));
14 describe('frequency model to frequency properties, daily recurring rule', () => {
15     test('non-custom: recursion ends', () => {
16         const frequencyModel = {
17             ...dummyFrequencyModel,
18             type: FREQUENCY.DAILY,
19             frequency: FREQUENCY.DAILY,
20         };
21         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
22             rrule: {
23                 value: {
24                     freq: 'DAILY',
25                 },
26             },
27         });
28     });
30     test('count 1 with other end type', () => {
31         const frequencyModel = {
32             ...dummyFrequencyModel,
33             type: FREQUENCY.CUSTOM,
34             frequency: FREQUENCY.DAILY,
35             interval: 2,
36             ends: {
37                 type: END_TYPE.UNTIL,
38                 count: 1,
39                 until: new Date(2020, 0, 30),
40             },
41         };
42         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
43             rrule: {
44                 value: {
45                     freq: 'DAILY',
46                     interval: 2,
47                     until: {
48                         year: 2020,
49                         month: 1,
50                         day: 30,
51                         hours: 21,
52                         minutes: 59,
53                         seconds: 59,
54                         isUTC: true,
55                     },
56                 },
57             },
58         });
59     });
61     test('every two days, ends after two occurrences', () => {
62         const frequencyModel = {
63             ...dummyFrequencyModel,
64             type: FREQUENCY.CUSTOM,
65             frequency: FREQUENCY.DAILY,
66             interval: 2,
67             ends: {
68                 type: END_TYPE.AFTER_N_TIMES,
69                 count: 2,
70             },
71         };
72         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
73             rrule: {
74                 value: {
75                     freq: 'DAILY',
76                     interval: 2,
77                     count: 2,
78                 },
79             },
80         });
81     });
83     test('ends on 30th January 2020 in Europe/Athens timezone', () => {
84         const until = new Date(2020, 0, 30);
85         const untilDateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
87         const frequencyModel = {
88             ...dummyFrequencyModel,
89             type: FREQUENCY.CUSTOM,
90             frequency: FREQUENCY.DAILY,
91             ends: {
92                 type: END_TYPE.UNTIL,
93                 until,
94                 count: 2,
95             },
96         };
97         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
98             rrule: {
99                 value: {
100                     freq: 'DAILY',
101                     interval: undefined,
102                     until: untilDateTime,
103                 },
104             },
105         });
106     });
107     test('every two days, ends on 30th January 2020 in Pacific/Pago_Pago timezone', () => {
108         const until = new Date(2020, 0, 30);
109         const untilDateTime = { year: 2020, month: 1, day: 31, hours: 10, minutes: 59, seconds: 59, isUTC: true };
111         const start = { ...dummyStart, tzid: 'Pacific/Pago_Pago' };
112         const frequencyModel = {
113             ...dummyFrequencyModel,
114             type: FREQUENCY.CUSTOM,
115             frequency: FREQUENCY.DAILY,
116             interval: 2,
117             ends: {
118                 type: END_TYPE.UNTIL,
119                 until,
120                 count: 2,
121             },
122         };
123         expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
124             rrule: {
125                 value: {
126                     freq: 'DAILY',
127                     interval: 2,
128                     until: untilDateTime,
129                 },
130             },
131         });
132     });
133     test('every two days, all-day event, ends on 30th January 2020', () => {
134         const until = new Date(2020, 0, 30);
135         const untilDateTime = { year: 2020, month: 1, day: 30 };
137         const frequencyModel = {
138             ...dummyFrequencyModel,
139             type: FREQUENCY.CUSTOM,
140             frequency: FREQUENCY.DAILY,
141             interval: 2,
142             ends: {
143                 type: END_TYPE.UNTIL,
144                 until,
145                 count: 2,
146             },
147         };
148         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart, isAllDay: true })).toEqual({
149             rrule: {
150                 value: {
151                     freq: 'DAILY',
152                     interval: 2,
153                     until: untilDateTime,
154                 },
155             },
156         });
157     });
160 describe('frequency model to frequency properties, weekly recurring rule', () => {
161     test('non-custom: single day, recursion ends', () => {
162         const frequencyModel = {
163             ...dummyFrequencyModel,
164             type: FREQUENCY.WEEKLY,
165             frequency: FREQUENCY.WEEKLY,
166         };
167         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
168             rrule: {
169                 value: {
170                     freq: 'WEEKLY',
171                 },
172             },
173         });
174     });
176     test('single day, every two weeks, ends after two occurrences', () => {
177         const frequencyModel = {
178             ...dummyFrequencyModel,
179             type: FREQUENCY.CUSTOM,
180             frequency: FREQUENCY.WEEKLY,
181             interval: 2,
182             ends: {
183                 type: END_TYPE.AFTER_N_TIMES,
184                 count: 2,
185             },
186         };
187         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
188             rrule: {
189                 value: {
190                     freq: 'WEEKLY',
191                     interval: 2,
192                     count: 2,
193                 },
194             },
195         });
196     });
198     test('two days per week, ends on 30th January 2020 in Europe/Athens timezone', () => {
199         const until = new Date(2020, 0, 30);
200         const untilDateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
202         const frequencyModel = {
203             ...dummyFrequencyModel,
204             type: FREQUENCY.CUSTOM,
205             frequency: FREQUENCY.WEEKLY,
206             weekly: {
207                 type: WEEKLY_TYPE.ON_DAYS,
208                 days: [1, 6],
209             },
210             ends: {
211                 type: END_TYPE.UNTIL,
212                 until,
213                 count: 2,
214             },
215         };
216         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
217             rrule: {
218                 value: {
219                     freq: 'WEEKLY',
220                     byday: ['MO', 'SA'],
221                     until: untilDateTime,
222                 },
223             },
224         });
225     });
226     test('two days per week, ends on 30th January 2020 in Pacific/Pago_Pago timezone', () => {
227         const until = new Date(2020, 0, 30);
228         const untilDateTime = { year: 2020, month: 1, day: 31, hours: 10, minutes: 59, seconds: 59, isUTC: true };
230         const start = { ...dummyStart, tzid: 'Pacific/Pago_Pago' };
231         const frequencyModel = {
232             ...dummyFrequencyModel,
233             type: FREQUENCY.CUSTOM,
234             frequency: FREQUENCY.WEEKLY,
235             weekly: {
236                 type: WEEKLY_TYPE.ON_DAYS,
237                 days: [1, 6],
238             },
239             ends: {
240                 type: END_TYPE.UNTIL,
241                 until,
242                 count: 2,
243             },
244         };
245         expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
246             rrule: {
247                 value: {
248                     freq: 'WEEKLY',
249                     byday: ['MO', 'SA'],
250                     until: untilDateTime,
251                 },
252             },
253         });
254     });
255     test('two all-day events per week, ends on 30th January 2020', () => {
256         const until = new Date(2020, 0, 30);
257         const untilDateTime = { year: 2020, month: 1, day: 30 };
259         const frequencyModel = {
260             ...dummyFrequencyModel,
261             type: FREQUENCY.CUSTOM,
262             frequency: FREQUENCY.WEEKLY,
263             weekly: {
264                 type: WEEKLY_TYPE.ON_DAYS,
265                 days: [6, 1],
266             },
267             ends: {
268                 type: END_TYPE.UNTIL,
269                 until,
270                 count: 2,
271             },
272         };
273         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart, isAllDay: true })).toEqual({
274             rrule: {
275                 value: {
276                     freq: 'WEEKLY',
277                     byday: ['MO', 'SA'],
278                     until: untilDateTime,
279                 },
280             },
281         });
282     });
285 describe('frequency model to frequency properties, monthly recurring rule', () => {
286     test('non-custom: single day, recursion ends', () => {
287         const frequencyModel = {
288             ...dummyFrequencyModel,
289             type: FREQUENCY.MONTHLY,
290             frequency: FREQUENCY.MONTHLY,
291         };
292         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
293             rrule: {
294                 value: {
295                     freq: 'MONTHLY',
296                 },
297             },
298         });
299     });
301     test('every two months on the 20th, ends after two occurrences', () => {
302         const frequencyModel = {
303             ...dummyFrequencyModel,
304             type: FREQUENCY.CUSTOM,
305             frequency: FREQUENCY.MONTHLY,
306             interval: 2,
307             ends: {
308                 type: END_TYPE.AFTER_N_TIMES,
309                 count: 2,
310             },
311         };
312         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
313             rrule: {
314                 value: {
315                     freq: 'MONTHLY',
316                     interval: 2,
317                     count: 2,
318                 },
319             },
320         });
321     });
323     test('every three months on the last Wednesday, recursion never ends', () => {
324         const lastWednesdayStart = { date: new Date(2020, 0, 29) };
325         const frequencyModel = {
326             ...getInitialFrequencyModel(lastWednesdayStart.date),
327             type: FREQUENCY.CUSTOM,
328             frequency: FREQUENCY.MONTHLY,
329             interval: 3,
330             monthly: {
331                 type: MONTHLY_TYPE.ON_MINUS_NTH_DAY,
332             },
333         };
334         expect(modelToFrequencyProperties({ frequencyModel, start: lastWednesdayStart })).toEqual({
335             rrule: {
336                 value: {
337                     freq: 'MONTHLY',
338                     interval: 3,
339                     bysetpos: -1,
340                     byday: 'WE',
341                 },
342             },
343         });
344     });
346     test('every third Monday, ends on 30th January 2020 in Europe/Athens timezone', () => {
347         const until = new Date(2020, 0, 30);
348         const untilDateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
350         const frequencyModel = {
351             ...dummyFrequencyModel,
352             type: FREQUENCY.CUSTOM,
353             frequency: FREQUENCY.MONTHLY,
354             monthly: {
355                 type: MONTHLY_TYPE.ON_NTH_DAY,
356             },
357             ends: {
358                 type: END_TYPE.UNTIL,
359                 until,
360                 count: 2,
361             },
362         };
363         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
364             rrule: {
365                 value: {
366                     freq: 'MONTHLY',
367                     until: untilDateTime,
368                     bysetpos: 3,
369                     byday: 'MO',
370                 },
371             },
372         });
373     });
375     test('every month on the 20th, ends on 30th January 2020 in Pacific/Pago_Pago timezone', () => {
376         const until = new Date(2020, 0, 30);
377         const untilDateTime = { year: 2020, month: 1, day: 31, hours: 10, minutes: 59, seconds: 59, isUTC: true };
379         const start = { ...dummyStart, tzid: 'Pacific/Pago_Pago' };
380         const frequencyModel = {
381             ...dummyFrequencyModel,
382             type: FREQUENCY.CUSTOM,
383             frequency: FREQUENCY.MONTHLY,
384             ends: {
385                 type: END_TYPE.UNTIL,
386                 until,
387                 count: 2,
388             },
389         };
390         expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
391             rrule: {
392                 value: {
393                     freq: 'MONTHLY',
394                     until: untilDateTime,
395                 },
396             },
397         });
398     });
400     test('An all-day event on the fourth Thursday, ends on 30th January 2021', () => {
401         const fourthThursdayStart = { date: new Date(2020, 0, 23) };
402         const until = new Date(2021, 0, 30);
403         const untilDateTime = { year: 2021, month: 1, day: 30 };
405         const frequencyModel = {
406             ...getInitialFrequencyModel(fourthThursdayStart.date),
407             type: FREQUENCY.CUSTOM,
408             frequency: FREQUENCY.MONTHLY,
409             interval: 6,
410             monthly: {
411                 type: MONTHLY_TYPE.ON_NTH_DAY,
412             },
413             ends: {
414                 type: END_TYPE.UNTIL,
415                 until,
416                 count: 2,
417             },
418         };
419         expect(modelToFrequencyProperties({ frequencyModel, start: fourthThursdayStart, isAllDay: true })).toEqual({
420             rrule: {
421                 value: {
422                     freq: 'MONTHLY',
423                     interval: 6,
424                     bysetpos: 4,
425                     byday: 'TH',
426                     until: untilDateTime,
427                 },
428             },
429         });
430     });
433 describe('frequency model to frequency properties, yearly recurring rule', () => {
434     test('non-custom: recursion ends', () => {
435         const frequencyModel = {
436             ...dummyFrequencyModel,
437             type: FREQUENCY.YEARLY,
438             frequency: FREQUENCY.YEARLY,
439         };
440         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
441             rrule: {
442                 value: {
443                     freq: 'YEARLY',
444                 },
445             },
446         });
447     });
449     test('every other year, ends after two occurrences', () => {
450         const frequencyModel = {
451             ...dummyFrequencyModel,
452             type: FREQUENCY.CUSTOM,
453             frequency: FREQUENCY.YEARLY,
454             interval: 2,
455             ends: {
456                 type: END_TYPE.AFTER_N_TIMES,
457                 count: 2,
458             },
459         };
460         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
461             rrule: {
462                 value: {
463                     freq: 'YEARLY',
464                     interval: 2,
465                     count: 2,
466                 },
467             },
468         });
469     });
471     test('ends on 30th January 2020 in Europe/Athens timezone', () => {
472         const until = new Date(2020, 0, 30);
473         const untilDateTime = { year: 2020, month: 1, day: 30, hours: 21, minutes: 59, seconds: 59, isUTC: true };
475         const frequencyModel = {
476             type: FREQUENCY.CUSTOM,
477             frequency: FREQUENCY.YEARLY,
478             ends: {
479                 type: END_TYPE.UNTIL,
480                 until,
481                 count: 2,
482             },
483         };
484         expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
485             rrule: {
486                 value: {
487                     freq: 'YEARLY',
488                     interval: undefined,
489                     until: untilDateTime,
490                 },
491             },
492         });
493     });
495     test('every other year, ends on 30th January 2020 in Pacific/Pago_Pago timezone', () => {
496         const until = new Date(2020, 0, 30);
497         const untilDateTime = { year: 2020, month: 1, day: 31, hours: 10, minutes: 59, seconds: 59, isUTC: true };
499         const start = { ...dummyStart, tzid: 'Pacific/Pago_Pago' };
500         const frequencyModel = {
501             type: FREQUENCY.CUSTOM,
502             frequency: FREQUENCY.YEARLY,
503             interval: 2,
504             ends: {
505                 type: END_TYPE.UNTIL,
506                 until,
507                 count: 2,
508             },
509         };
510         expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
511             rrule: {
512                 value: {
513                     freq: 'YEARLY',
514                     interval: 2,
515                     until: untilDateTime,
516                 },
517             },
518         });
519     });
521     test('every two days, all-day event, ends on 30th January 2020', () => {
522         const until = new Date(2020, 0, 30);
523         const untilDateTime = { year: 2020, month: 1, day: 30 };
525         const frequencyModel = {
526             type: FREQUENCY.CUSTOM,
527             frequency: FREQUENCY.YEARLY,
528             interval: 2,
529             ends: {
530                 type: END_TYPE.UNTIL,
531                 until,
532                 count: 2,
533             },
534         };
535         expect(modelToFrequencyProperties({ frequencyModel, isAllDay: true, start: dummyStart })).toEqual({
536             rrule: {
537                 value: {
538                     freq: 'YEARLY',
539                     interval: 2,
540                     until: untilDateTime,
541                 },
542             },
543         });
544     });