1 import { getDateFromVCardProperty, guessDateFromText } from '@proton/shared/lib/contacts/property';
2 import type { VCardDateOrText, VCardProperty } from '@proton/shared/lib/interfaces/contacts/VCard';
4 describe('property', () => {
5 describe('guessDateFromText', () => {
6 it('should give expected date when text is a valid date', () => {
7 const text = 'Jun 9, 2022';
8 const text2 = '2014-02-11T11:30:30';
9 const text3 = '2023/12/3';
10 const text4 = '03/12/2023';
12 expect(guessDateFromText(text)).toEqual(new Date(2022, 5, 9));
13 expect(guessDateFromText(text2)).toEqual(new Date(2014, 1, 11, 11, 30, 30));
14 expect(guessDateFromText(text3)).toEqual(new Date(2023, 11, 3));
15 // Notice in this case, the Date constructor opts for the English format mm/dd instead of dd/mm. This may cause problems during import
16 expect(guessDateFromText(text4)).toEqual(new Date(2023, 2, 12));
19 it('should give today date when text is not a valid date', () => {
20 const text = 'random string to make it fail';
22 expect(guessDateFromText(text)).toEqual(undefined);
26 describe('getDateFromVCardProperty', () => {
27 it('should give the expected date when date is valid', () => {
28 const date = new Date(2022, 1, 1);
29 const vCardProperty = {
33 } as VCardProperty<VCardDateOrText>;
35 expect(getDateFromVCardProperty(vCardProperty)).toEqual(date);
38 it('should give today date when date is not valid', () => {
39 const date = new Date('random string to make it fail');
40 const vCardProperty = {
44 } as VCardProperty<VCardDateOrText>;
45 const fallbackDate = new Date();
47 expect(getDateFromVCardProperty(vCardProperty, fallbackDate)).toEqual(fallbackDate);
50 it('should give expected date when text is a valid date', () => {
51 const text = 'Jun 9, 2022';
52 const vCardProperty = {
56 } as VCardProperty<VCardDateOrText>;
58 expect(getDateFromVCardProperty(vCardProperty)).toEqual(new Date(2022, 5, 9));
60 const text2 = '2014-02-11T11:30:30';
61 const vCardProperty2 = {
65 } as VCardProperty<VCardDateOrText>;
67 expect(getDateFromVCardProperty(vCardProperty2)).toEqual(new Date(2014, 1, 11, 11, 30, 30));
70 it('should give today date when text is not a valid date', () => {
71 const vCardProperty = {
73 text: 'random string to make it fail',
75 } as VCardProperty<VCardDateOrText>;
76 const fallbackDate = new Date();
78 expect(getDateFromVCardProperty(vCardProperty, fallbackDate)).toEqual(fallbackDate);