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', () => ({
11 loadCryptoWorker: jest.fn(),
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,
21 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
30 test('count 1 with other end type', () => {
31 const frequencyModel = {
32 ...dummyFrequencyModel,
33 type: FREQUENCY.CUSTOM,
34 frequency: FREQUENCY.DAILY,
39 until: new Date(2020, 0, 30),
42 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
61 test('every two days, ends after two occurrences', () => {
62 const frequencyModel = {
63 ...dummyFrequencyModel,
64 type: FREQUENCY.CUSTOM,
65 frequency: FREQUENCY.DAILY,
68 type: END_TYPE.AFTER_N_TIMES,
72 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
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,
97 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
102 until: untilDateTime,
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,
118 type: END_TYPE.UNTIL,
123 expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
128 until: untilDateTime,
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,
143 type: END_TYPE.UNTIL,
148 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart, isAllDay: true })).toEqual({
153 until: untilDateTime,
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,
167 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
176 test('single day, every two weeks, ends after two occurrences', () => {
177 const frequencyModel = {
178 ...dummyFrequencyModel,
179 type: FREQUENCY.CUSTOM,
180 frequency: FREQUENCY.WEEKLY,
183 type: END_TYPE.AFTER_N_TIMES,
187 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
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,
207 type: WEEKLY_TYPE.ON_DAYS,
211 type: END_TYPE.UNTIL,
216 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
221 until: untilDateTime,
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,
236 type: WEEKLY_TYPE.ON_DAYS,
240 type: END_TYPE.UNTIL,
245 expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
250 until: untilDateTime,
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,
264 type: WEEKLY_TYPE.ON_DAYS,
268 type: END_TYPE.UNTIL,
273 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart, isAllDay: true })).toEqual({
278 until: untilDateTime,
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,
292 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
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,
308 type: END_TYPE.AFTER_N_TIMES,
312 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
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,
331 type: MONTHLY_TYPE.ON_MINUS_NTH_DAY,
334 expect(modelToFrequencyProperties({ frequencyModel, start: lastWednesdayStart })).toEqual({
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,
355 type: MONTHLY_TYPE.ON_NTH_DAY,
358 type: END_TYPE.UNTIL,
363 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
367 until: untilDateTime,
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,
385 type: END_TYPE.UNTIL,
390 expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
394 until: untilDateTime,
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,
411 type: MONTHLY_TYPE.ON_NTH_DAY,
414 type: END_TYPE.UNTIL,
419 expect(modelToFrequencyProperties({ frequencyModel, start: fourthThursdayStart, isAllDay: true })).toEqual({
426 until: untilDateTime,
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,
440 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
449 test('every other year, ends after two occurrences', () => {
450 const frequencyModel = {
451 ...dummyFrequencyModel,
452 type: FREQUENCY.CUSTOM,
453 frequency: FREQUENCY.YEARLY,
456 type: END_TYPE.AFTER_N_TIMES,
460 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
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,
479 type: END_TYPE.UNTIL,
484 expect(modelToFrequencyProperties({ frequencyModel, start: dummyStart })).toEqual({
489 until: untilDateTime,
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,
505 type: END_TYPE.UNTIL,
510 expect(modelToFrequencyProperties({ start, frequencyModel })).toEqual({
515 until: untilDateTime,
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,
530 type: END_TYPE.UNTIL,
535 expect(modelToFrequencyProperties({ frequencyModel, isAllDay: true, start: dummyStart })).toEqual({
540 until: untilDateTime,