diff --git a/codecov.yml b/codecov.yml index 5918408fd7f45..1a14b3aa46023 100644 --- a/codecov.yml +++ b/codecov.yml @@ -5,7 +5,7 @@ coverage: target: auto threshold: 1% adapters: - target: 94% + target: 100% paths: - 'packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts' - 'packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts' @@ -14,6 +14,8 @@ coverage: - 'packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts' - 'packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts' - 'packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts' + ignore: + - '**/*.test.tsx' patch: off comment: false diff --git a/packages/grid/x-data-grid-premium/tsconfig.json b/packages/grid/x-data-grid-premium/tsconfig.json index 0b7aa077e83e9..199471d58d081 100644 --- a/packages/grid/x-data-grid-premium/tsconfig.json +++ b/packages/grid/x-data-grid-premium/tsconfig.json @@ -6,7 +6,8 @@ }, "include": [ "src/**/*", - "../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", + "../../test/utils/addChaiAssertions.ts", + "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../../node_modules/@mui/material/themeCssVarsAugmentation" ] } diff --git a/packages/grid/x-data-grid-pro/tsconfig.json b/packages/grid/x-data-grid-pro/tsconfig.json index 0b7aa077e83e9..199471d58d081 100644 --- a/packages/grid/x-data-grid-pro/tsconfig.json +++ b/packages/grid/x-data-grid-pro/tsconfig.json @@ -6,7 +6,8 @@ }, "include": [ "src/**/*", - "../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", + "../../test/utils/addChaiAssertions.ts", + "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../../node_modules/@mui/material/themeCssVarsAugmentation" ] } diff --git a/packages/grid/x-data-grid/tsconfig.json b/packages/grid/x-data-grid/tsconfig.json index 0b7aa077e83e9..199471d58d081 100644 --- a/packages/grid/x-data-grid/tsconfig.json +++ b/packages/grid/x-data-grid/tsconfig.json @@ -6,7 +6,8 @@ }, "include": [ "src/**/*", - "../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", + "../../test/utils/addChaiAssertions.ts", + "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../../node_modules/@mui/material/themeCssVarsAugmentation" ] } diff --git a/packages/x-charts/tsconfig.json b/packages/x-charts/tsconfig.json index 10d4ab6a916ae..56b1c72c9db97 100644 --- a/packages/x-charts/tsconfig.json +++ b/packages/x-charts/tsconfig.json @@ -5,6 +5,7 @@ }, "include": [ "src/**/*", + "../../test/utils/addChaiAssertions.ts", "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../node_modules/@mui/material/themeCssVarsAugmentation" ] diff --git a/packages/x-date-pickers-pro/tsconfig.json b/packages/x-date-pickers-pro/tsconfig.json index 10d4ab6a916ae..56b1c72c9db97 100644 --- a/packages/x-date-pickers-pro/tsconfig.json +++ b/packages/x-date-pickers-pro/tsconfig.json @@ -5,6 +5,7 @@ }, "include": [ "src/**/*", + "../../test/utils/addChaiAssertions.ts", "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../node_modules/@mui/material/themeCssVarsAugmentation" ] diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx index da5a2c9f58ddc..64d594c3574bd 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx @@ -15,16 +15,16 @@ import de from 'date-fns/locale/de'; import ru from 'date-fns/locale/ru'; import { describeGregorianAdapter, - TEST_DATE_ISO, + TEST_DATE_ISO_STRING, } from '@mui/x-date-pickers/tests/describeGregorianAdapter'; describe('', () => { - describeGregorianAdapter(AdapterDateFns, { formatDateTime: 'yyyy-MM-dd HH:mm:ss', locale: enUS }); + describeGregorianAdapter(AdapterDateFns, { formatDateTime: 'yyyy-MM-dd HH:mm:ss' }); describe('Adapter localization', () => { describe('English', () => { const adapter = new AdapterDateFns({ locale: enUS }); - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; it('getWeekdays: should start on Sunday', () => { const result = adapter.getWeekdays(); @@ -50,7 +50,7 @@ describe('', () => { }); it('getWeekArray: should start on Monday', () => { - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; const result = adapter.getWeekArray(date); expect(adapter.formatByString(result[0][0], 'EEEEEE')).to.equal('пн'); }); diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts index 7ced377133003..dc56476b80adf 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts @@ -21,7 +21,6 @@ import endOfWeek from 'date-fns/endOfWeek'; import endOfYear from 'date-fns/endOfYear'; import dateFnsFormat from 'date-fns/format'; import getDate from 'date-fns/getDate'; -import getDay from 'date-fns/getDay'; import getDaysInMonth from 'date-fns/getDaysInMonth'; import getHours from 'date-fns/getHours'; import getMinutes from 'date-fns/getMinutes'; @@ -527,18 +526,16 @@ export class AdapterDateFns implements MuiPickersAdapter { let count = 0; let current = start; const nestedWeeks: Date[][] = []; - let lastDay: number | null = null; + while (isBefore(current, end)) { const weekNumber = Math.floor(count / 7); nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; - const day = getDay(current); - if (lastDay !== day) { - lastDay = day; - nestedWeeks[weekNumber].push(current); - count += 1; - } + nestedWeeks[weekNumber].push(current); + current = addDays(current, 1); + count += 1; } + return nestedWeeks; }; diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx index e3dc8b1938ad7..a8021bbd38bfe 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx @@ -16,7 +16,7 @@ import { AdapterFormats } from '@mui/x-date-pickers'; import { expect } from 'chai'; describe('', () => { - describeJalaliAdapter(AdapterDateFnsJalali, { locale: faJalaliIR }); + describeJalaliAdapter(AdapterDateFnsJalali, {}); describe('Adapter localization', () => { it('Formatting', () => { @@ -59,17 +59,13 @@ describe('', () => { }; Object.keys(localizedTexts).forEach((localeKey) => { - const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; - const localeObject = - localeKey === 'undefined' - ? undefined - : { - faIR, - faJalaliIR, - enUS, - }[localeKey]; + const localeObject = { + faIR, + faJalaliIR, + enUS, + }[localeKey]; - describe(`test with the ${localeName} locale`, () => { + describe(`test with the "${localeKey}" locale`, () => { const { render, adapter } = createPickerRenderer({ clock: 'fake', adapterName: 'date-fns-jalali', diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index 390b0f3e87d65..268acf0bab33d 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -27,7 +27,6 @@ import getWeek from 'date-fns-jalali/getWeek'; import getYear from 'date-fns-jalali/getYear'; import getMonth from 'date-fns-jalali/getMonth'; import getDate from 'date-fns-jalali/getDate'; -import getDay from 'date-fns-jalali/getDay'; import getDaysInMonth from 'date-fns-jalali/getDaysInMonth'; import getMinutes from 'date-fns-jalali/getMinutes'; import isAfter from 'date-fns-jalali/isAfter'; @@ -541,18 +540,16 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter', () => { - describeGregorianAdapter(AdapterDayjs, { formatDateTime: 'YYYY-MM-DD HH:mm:ss', locale: 'en' }); + describeGregorianAdapter(AdapterDayjs, { formatDateTime: 'YYYY-MM-DD HH:mm:ss' }); describe('Adapter localization', () => { describe('English', () => { const adapter = new AdapterDayjs({ instance: dayjs, locale: 'en' }); - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; it('getWeekdays: should start on Sunday', () => { const result = adapter.getWeekdays(); @@ -73,7 +58,7 @@ describe('', () => { }); it('getWeekArray: should start on Monday', () => { - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; const result = adapter.getWeekArray(date); expect(result[0][0].format('dd')).to.equal('пн'); }); @@ -112,9 +97,32 @@ describe('', () => { expectDate('keyboardDateTime12h', '02/01/2020 11:44 PM', '01.02.2020 11:44 вечера'); expectDate('keyboardDateTime24h', '02/01/2020 23:44', '01.02.2020 23:44'); }); + + it('should warn when trying to use a non-loaded locale', () => { + const adapter = new AdapterDayjs({ locale: 'pl' }); + expect(() => adapter.is12HourCycleInCurrentLocale()).toWarnDev( + 'Your locale has not been found.', + ); + }); }); describe('Picker localization', () => { + const testDate = new Date(2018, 4, 15, 9, 35); + const localizedTexts = { + undefined: { + placeholder: 'MM/DD/YYYY hh:mm aa', + value: '05/15/2018 09:35 AM', + }, + fr: { + placeholder: 'DD/MM/YYYY hh:mm', + value: '15/05/2018 09:35', + }, + de: { + placeholder: 'DD.MM.YYYY hh:mm', + value: '15.05.2018 09:35', + }, + }; + Object.keys(localizedTexts).forEach((localeKey) => { const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey }; diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index a0f11348a4204..166cfa94c2d66 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -152,7 +152,7 @@ export class AdapterDayjs implements MuiPickersAdapter { } private getLocaleFormats = () => { - const locales = this.rawDayJsInstance.Ls ?? defaultDayjs.Ls; + const locales = defaultDayjs.Ls; const locale = this.locale || 'en'; let localeObject = locales[locale]; diff --git a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.test.tsx b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.test.tsx index 6f6a7e1ddf828..4aef8455e255a 100644 --- a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.test.tsx +++ b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.test.tsx @@ -5,35 +5,19 @@ import { AdapterFormats } from '@mui/x-date-pickers/models'; import { screen } from '@mui/monorepo/test/utils/createRenderer'; import { expect } from 'chai'; import { + cleanText, createPickerRenderer, expectInputPlaceholder, expectInputValue, } from 'test/utils/pickers-utils'; import { describeGregorianAdapter, - TEST_DATE_ISO, + TEST_DATE_ISO_STRING, } from '@mui/x-date-pickers/tests/describeGregorianAdapter'; -const testDate = new Date(2018, 4, 15, 9, 35); -const localizedTexts = { - undefined: { - placeholder: 'MM/DD/YYYY hh:mm aa', - value: '05/15/2018 09:35 AM', - }, - fr: { - placeholder: 'DD/MM/YYYY hh:mm', - value: '15/05/2018 09:35', - }, - de: { - placeholder: 'DD.MM.YYYY hh:mm', - value: '15.05.2018 09:35', - }, -}; - describe('', () => { describeGregorianAdapter(AdapterLuxon, { formatDateTime: 'yyyy-MM-dd HH:mm:ss', - locale: 'en-US', }); describe('Adapter localization', () => { @@ -54,7 +38,7 @@ describe('', () => { }); it('getWeekArray: should start on Monday', () => { - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; const result = adapter.getWeekArray(date); expect(result[0][0].toFormat('ccc')).to.equal('пн'); }); @@ -68,7 +52,7 @@ describe('', () => { }); }); - describe('Formatting', () => { + it('Formatting', () => { const adapter = new AdapterLuxon({ locale: 'en-US' }); const adapterRu = new AdapterLuxon({ locale: 'ru' }); @@ -80,7 +64,7 @@ describe('', () => { const date = adapter.date('2020-02-01T23:44:00.000Z')!; expect(adapter.format(date, format)).to.equal(expectedWithEn); - expect(adapterRu.format(date, format)).to.equal(expectedWithRu); + expect(cleanText(adapterRu.format(date, format))).to.equal(expectedWithRu); }; expectDate('fullDate', 'Feb 1, 2020', '1 февр. 2020 г.'); @@ -96,6 +80,22 @@ describe('', () => { }); describe('Picker localization', () => { + const testDate = new Date(2018, 4, 15, 9, 35); + const localizedTexts = { + undefined: { + placeholder: 'MM/DD/YYYY hh:mm aa', + value: '05/15/2018 09:35 AM', + }, + fr: { + placeholder: 'DD/MM/YYYY hh:mm', + value: '15/05/2018 09:35', + }, + de: { + placeholder: 'DD.MM.YYYY hh:mm', + value: '15.05.2018 09:35', + }, + }; + Object.keys(localizedTexts).forEach((localeKey) => { const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey }; diff --git a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts index 18a1827a5469c..9a9740994aa60 100644 --- a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts +++ b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts @@ -185,11 +185,6 @@ export class AdapterLuxon implements MuiPickersAdapter { }; public expandFormat = (format: string) => { - if (!DateTime.expandFormat) { - throw Error( - 'Your luxon version does not support `expandFormat`. Consider upgrading it to v3.0.2', - ); - } // Extract escaped section to avoid extending them const longFormatRegexp = /''|'(''|[^'])+('|$)|[^']*/g; return ( @@ -250,6 +245,7 @@ export class AdapterLuxon implements MuiPickersAdapter { if (unit) { return Math.floor(value.diff(comparing).as(unit)); } + return value.diff(comparing).as('millisecond'); }; diff --git a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.test.tsx b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.test.tsx index b5bf61befdb9d..f6b49e4fd72ff 100644 --- a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.test.tsx +++ b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.test.tsx @@ -5,6 +5,7 @@ import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment'; import { AdapterFormats } from '@mui/x-date-pickers/models'; import { screen } from '@mui/monorepo/test/utils/createRenderer'; import { expect } from 'chai'; +import { spy } from 'sinon'; import { createPickerRenderer, expectInputPlaceholder, @@ -15,16 +16,16 @@ import 'moment/locale/fr'; import 'moment/locale/ko'; import { describeGregorianAdapter, - TEST_DATE_ISO, + TEST_DATE_ISO_STRING, } from '@mui/x-date-pickers/tests/describeGregorianAdapter'; describe('', () => { - describeGregorianAdapter(AdapterMoment, { formatDateTime: 'YYYY-MM-DD HH:mm:ss', locale: 'en' }); + describeGregorianAdapter(AdapterMoment, { formatDateTime: 'YYYY-MM-DD HH:mm:ss' }); describe('Adapter localization', () => { describe('English', () => { const adapter = new AdapterMoment({ locale: 'en' }); - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; it('getWeekdays: should start on Monday', () => { const result = adapter.getWeekdays(); @@ -39,11 +40,15 @@ describe('', () => { it('is12HourCycleInCurrentLocale: should have meridiem', () => { expect(adapter.is12HourCycleInCurrentLocale()).to.equal(true); }); + + it('parse: should have the right locale', () => { + expect(adapter.parse('01/01/2020', 'MM/DD/YYYY')!.locale()).to.equal('en'); + }); }); describe('Russian', () => { const adapter = new AdapterMoment({ locale: 'ru' }); - const date = adapter.date(TEST_DATE_ISO)!; + const date = adapter.date(TEST_DATE_ISO_STRING)!; beforeEach(() => { moment.locale('ru'); @@ -70,6 +75,10 @@ describe('', () => { it('getCurrentLocaleCode: should return locale code', () => { expect(adapter.getCurrentLocaleCode()).to.equal('ru'); }); + + it('parse: should have the right locale', () => { + expect(adapter.parse('01/01/2020', 'MM/DD/YYYY')!.locale()).to.equal('ru'); + }); }); describe('Korean', () => { @@ -87,6 +96,10 @@ describe('', () => { expect(adapter.getMeridiemText('am')).to.equal('오전'); expect(adapter.getMeridiemText('pm')).to.equal('오후'); }); + + it('parse: should have the right locale', () => { + expect(adapter.parse('01/01/2020', 'MM/DD/YYYY')!.locale()).to.equal('ko'); + }); }); it('Formatting', () => { @@ -161,4 +174,12 @@ describe('', () => { }); }); }); + + it('should use moment custom instance if provided', () => { + const spiedInstance = spy(moment); + + const adapter = new AdapterMoment({ instance: spiedInstance as unknown as typeof moment }); + adapter.date()!; + expect(spiedInstance.callCount).to.equal(1); + }); }); diff --git a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts index 4710c8d7e9a60..269c267396bb7 100644 --- a/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts +++ b/packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts @@ -408,7 +408,7 @@ export class AdapterMoment implements MuiPickersAdapter { }; public getMonthArray = (value: Moment) => { - const firstMonth = value.clone().startOf('year'); + const firstMonth = this.startOfYear(value); const monthArray = [firstMonth]; while (monthArray.length < 12) { @@ -472,7 +472,7 @@ export class AdapterMoment implements MuiPickersAdapter { public getMeridiemText = (ampm: 'am' | 'pm') => { if (this.is12HourCycleInCurrentLocale()) { // AM/PM translation only possible in those who have 12 hour cycle in locale. - return this.moment + return defaultMoment .localeData(this.getCurrentLocaleCode()) .meridiem(ampm === 'am' ? 0 : 13, 0, false); } diff --git a/packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts b/packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts index 942df87791699..e9ef4fa40d785 100644 --- a/packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts +++ b/packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts @@ -122,10 +122,6 @@ export class AdapterMomentHijri extends AdapterMoment implements MuiPickersAdapt this.formats = { ...defaultFormats, ...formats }; } - private toIMoment = (value?: Moment | undefined) => { - return this.moment(value ? value.clone() : undefined).locale('ar-SA'); - }; - public date = (value?: any) => { if (value === null) { return null; @@ -227,7 +223,7 @@ export class AdapterMomentHijri extends AdapterMoment implements MuiPickersAdapt public getWeekdays = () => { return [0, 1, 2, 3, 4, 5, 6].map((dayOfWeek) => { - return this.toIMoment().weekday(dayOfWeek).format('dd'); + return this.date()!.weekday(dayOfWeek).format('dd'); }); }; @@ -280,8 +276,6 @@ export class AdapterMomentHijri extends AdapterMoment implements MuiPickersAdapt }; public getMeridiemText = (ampm: 'am' | 'pm') => { - return ampm === 'am' - ? this.toIMoment().hours(2).format('A') - : this.toIMoment().hours(14).format('A'); + return ampm === 'am' ? this.date()!.hours(2).format('A') : this.date()!.hours(14).format('A'); }; } diff --git a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.test.tsx b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.test.tsx index 5e9c8cb3fbcf0..f9843cf37eaf6 100644 --- a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.test.tsx +++ b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.test.tsx @@ -16,7 +16,6 @@ import { describeJalaliAdapter } from '@mui/x-date-pickers/tests/describeJalaliA describe('', () => { describeJalaliAdapter(AdapterMomentJalaali, { - locale: 'fa', before: () => { jMoment.loadPersian({ dialect: 'persian-modern', usePersianDigits: true }); }, diff --git a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts index d306628aa5972..10a16610ff7cf 100644 --- a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts +++ b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts @@ -124,10 +124,6 @@ export class AdapterMomentJalaali this.formats = { ...defaultFormats, ...formats }; } - private toJMoment = (value?: Moment | undefined) => { - return this.moment(value ? value.clone() : undefined).locale('fa'); - }; - public date = (value?: any) => { if (value === null) { return null; @@ -136,6 +132,10 @@ export class AdapterMomentJalaali return this.moment(value).locale('fa'); }; + public parseISO = (isoString: string) => { + return this.moment(isoString).locale('fa'); + }; + public parse = (value: string, format: string) => { if (value === '') { return null; @@ -176,6 +176,18 @@ export class AdapterMomentJalaali return this.moment(value).isSame(comparing); }; + public isSameYear = (value: Moment, comparing: Moment) => { + // `isSame` seems to mutate the date on `moment-jalaali` + // @ts-ignore + return value.clone().isSame(comparing, 'jYear'); + }; + + public isSameMonth = (value: Moment, comparing: Moment) => { + // `isSame` seems to mutate the date on `moment-jalaali` + // @ts-ignore + return value.clone().isSame(comparing, 'jMonth'); + }; + public isAfterYear = (value: Moment, comparing: Moment) => { return value.jYear() > comparing.jYear(); }; @@ -246,7 +258,7 @@ export class AdapterMomentJalaali public getWeekdays = () => { return [0, 1, 2, 3, 4, 5, 6].map((dayOfWeek) => { - return this.toJMoment().weekday(dayOfWeek).format('dd'); + return this.date()!.weekday(dayOfWeek).format('dd'); }); }; @@ -289,8 +301,6 @@ export class AdapterMomentJalaali }; public getMeridiemText = (ampm: 'am' | 'pm') => { - return ampm === 'am' - ? this.toJMoment().hours(2).format('A') - : this.toJMoment().hours(14).format('A'); + return ampm === 'am' ? this.date()!.hours(2).format('A') : this.date()!.hours(14).format('A'); }; } diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.ts index bd2ae4112cfff..a1cc162162cfd 100644 --- a/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.ts +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.ts @@ -5,20 +5,16 @@ import { testLocalization } from './testLocalization'; import { testFormat } from './testFormat'; import { DescribeGregorianAdapterParams } from './describeGregorianAdapter.types'; -export const TEST_DATE_ISO = '2018-10-30T11:44:00.000Z'; - function innerGregorianDescribeAdapter( Adapter: new (...args: any) => MuiPickersAdapter, params: DescribeGregorianAdapterParams, ) { - const adapter = new Adapter({ locale: params.locale }); + const adapter = new Adapter(); describe(adapter.lib, () => { const testSuitParams = { ...params, adapter, - testDateISO: TEST_DATE_ISO, - testDate: adapter.date(TEST_DATE_ISO), }; testCalculations(testSuitParams); diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.types.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.types.ts index e9fc6ffe52813..be0f4b2eed8c8 100644 --- a/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.types.ts +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.types.ts @@ -2,13 +2,8 @@ import { MuiPickersAdapter } from '@mui/x-date-pickers/models'; export interface DescribeGregorianAdapterParams { formatDateTime: string; - // TODO: Type once the adapter locale is correctly types - locale: any; } export type DescribeGregorianAdapterTestSuite = (params: { adapter: MuiPickersAdapter; - testDate: TDate; - testDateISO: string; - formatDateTime: string; }) => void; diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.utils.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.utils.ts new file mode 100644 index 0000000000000..d2f83810af6bc --- /dev/null +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/describeGregorianAdapter.utils.ts @@ -0,0 +1,3 @@ +export const TEST_DATE_ISO_STRING = '2018-10-30T11:44:00.000Z'; + +export const TEST_DATE_LOCALE_STRING = '2018-10-30'; diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/index.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/index.ts index 298603b1355ee..983c2e9fd7f05 100644 --- a/packages/x-date-pickers/src/tests/describeGregorianAdapter/index.ts +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/index.ts @@ -1 +1,2 @@ -export { describeGregorianAdapter, TEST_DATE_ISO } from './describeGregorianAdapter'; +export { describeGregorianAdapter } from './describeGregorianAdapter'; +export { TEST_DATE_ISO_STRING } from './describeGregorianAdapter.utils'; diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/testCalculations.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/testCalculations.ts index 6b5c10348ba8b..25fe5d626548b 100644 --- a/packages/x-date-pickers/src/tests/describeGregorianAdapter/testCalculations.ts +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/testCalculations.ts @@ -1,275 +1,529 @@ import { expect } from 'chai'; import { DescribeGregorianAdapterTestSuite } from './describeGregorianAdapter.types'; +import { TEST_DATE_ISO_STRING, TEST_DATE_LOCALE_STRING } from './describeGregorianAdapter.utils'; -export const testCalculations: DescribeGregorianAdapterTestSuite = ({ - adapter, - testDate, - testDateISO, - formatDateTime, -}) => { - it('Method: date', () => { - // ISO string - expect(adapter.isEqual(testDate, adapter.date(testDateISO))).to.equal(true); +export const testCalculations: DescribeGregorianAdapterTestSuite = ({ adapter }) => { + const testDateIso = adapter.date(TEST_DATE_ISO_STRING)!; + const testDateLocale = adapter.date(TEST_DATE_LOCALE_STRING)!; - // Native Date - expect(adapter.isEqual(testDate, adapter.date(new Date(testDateISO)))).to.equal(true); + describe('Method: date', () => { + it('should parse strings', () => { + expect(adapter.isEqual(testDateIso, adapter.date(TEST_DATE_ISO_STRING))).to.equal(true); + expect(adapter.isEqual(testDateLocale, adapter.date(TEST_DATE_LOCALE_STRING))).to.equal(true); + }); + + it('should parse native Date object', () => { + expect(adapter.isEqual(testDateIso, adapter.date(new Date(TEST_DATE_ISO_STRING)))).to.equal( + true, + ); + expect( + adapter.isEqual(testDateLocale, adapter.date(new Date(TEST_DATE_LOCALE_STRING))), + ).to.equal(true); + }); + + it('should parse already-parsed object', () => { + expect( + adapter.isEqual(testDateIso, adapter.date(adapter.date(TEST_DATE_ISO_STRING))), + ).to.equal(true); + expect( + adapter.isEqual(testDateLocale, adapter.date(adapter.date(TEST_DATE_LOCALE_STRING))), + ).to.equal(true); + }); + + it('should parse null', () => { + expect(adapter.date(null)).to.equal(null); + }); + + it('should parse undefined', () => { + expect(adapter.date(undefined)).toEqualDateTime(new Date()); + }); + }); + + it('Method: toJsDate', () => { + expect(adapter.toJsDate(testDateIso)).to.be.instanceOf(Date); + expect(adapter.toJsDate(testDateLocale)).to.be.instanceOf(Date); + }); + + it('Method: parseISO', () => { + expect(adapter.parseISO(TEST_DATE_ISO_STRING)).toEqualDateTime(testDateIso); + }); + + it('Method: toISO', () => { + const outputtedISO = adapter.toISO(testDateIso); + + if (adapter.lib === 'date-fns') { + // date-fns never suppress useless milliseconds in the end + expect(outputtedISO).to.equal(TEST_DATE_ISO_STRING.replace('.000Z', 'Z')); + } else if (adapter.lib === 'luxon') { + // luxon does not shorthand +00:00 to Z, which is also valid ISO string + expect(outputtedISO).to.equal(TEST_DATE_ISO_STRING.replace('Z', '+00:00')); + } else { + expect(outputtedISO).to.equal(TEST_DATE_ISO_STRING); + } + }); + + it('Method: parse', () => { + // Date time + expect(adapter.parse('10/30/2018 11:44', adapter.formats.keyboardDateTime24h)).toEqualDateTime( + '2018-10-30T11:44:00.000Z', + ); - // Parse already date-specific object - expect(adapter.isEqual(testDate, adapter.date(adapter.date(testDateISO)))).to.equal(true); + // Date + expect(adapter.parse('10/30/2018', adapter.formats.keyboardDate)).toEqualDateTime( + '2018-10-30T00:00:00.000Z', + ); - // Parse null inputs - expect(adapter.date(null)).to.equal(null); + // Empty string + expect(adapter.parse('', adapter.formats.keyboardDate)).to.equal(null); - // Undefined - expect(!!adapter.date(undefined)).to.equal(true); + // Invalid input + expect(adapter.isValid(adapter.parse('99/99/9999', adapter.formats.keyboardDate))).to.equal( + false, + ); + }); + + it('Method: isNull', () => { + expect(adapter.isNull(null)).to.equal(true); + expect(adapter.isNull(testDateIso)).to.equal(false); + expect(adapter.isNull(testDateLocale)).to.equal(false); }); it('Method: isValid', () => { const invalidDate = adapter.date('2018-42-30T11:60:00.000Z'); - expect(adapter.isValid(testDate)).to.equal(true); + expect(adapter.isValid(testDateIso)).to.equal(true); + expect(adapter.isValid(testDateLocale)).to.equal(true); expect(adapter.isValid(invalidDate)).to.equal(false); expect(adapter.isValid(undefined)).to.equal(true); expect(adapter.isValid(null)).to.equal(false); expect(adapter.isValid('2018-42-30T11:60:00.000Z')).to.equal(false); }); - it('Method: addYears', () => { - expect(adapter.format(adapter.addYears(testDate, 2), 'year')).to.equal('2020'); - expect(adapter.format(adapter.addYears(testDate, -2), 'year')).to.equal('2016'); + describe('Method: getDiff', () => { + it('should compute the millisecond diff when there is no unit', () => { + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-29T11:44:00.000Z')!)).to.equal( + 86400000, + ); + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( + -86400000, + ); + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( + -86400000, + ); + }); + + it('should compute the diff in the provided unit (ISO)', () => { + expect( + adapter.getDiff(testDateIso, adapter.date('2017-09-29T11:44:00.000Z')!, 'years'), + ).to.equal(1); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-08-29T11:44:00.000Z')!, 'months'), + ).to.equal(2); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-05-29T11:44:00.000Z')!, 'quarters'), + ).to.equal(1); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'days'), + ).to.equal(31); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'weeks'), + ).to.equal(4); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'hours'), + ).to.equal(744); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'minutes'), + ).to.equal(44640); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-10-30T10:44:00.000Z')!, 'seconds'), + ).to.equal(3600); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-10-30T10:44:00.000Z')!, 'milliseconds'), + ).to.equal(3600000); + }); + + it('should compute the diff in the provided unit (locale)', () => { + expect(adapter.getDiff(testDateLocale, adapter.date('2017-09-29')!, 'years')).to.equal(1); + expect(adapter.getDiff(testDateLocale, adapter.date('2018-08-29')!, 'months')).to.equal(2); + expect(adapter.getDiff(testDateLocale, adapter.date('2018-05-29')!, 'quarters')).to.equal(1); + expect(adapter.getDiff(testDateLocale, adapter.date('2018-09-29')!, 'days')).to.equal(31); + expect(adapter.getDiff(testDateLocale, adapter.date('2018-09-29')!, 'weeks')).to.equal(4); + }); + + it('should compute the diff with string "comparing" param', () => { + expect(adapter.getDiff(testDateLocale, '2017-09-29', 'years')).to.equal(1); + expect(adapter.getDiff(testDateLocale, '2018-08-29', 'months')).to.equal(2); + expect(adapter.getDiff(testDateLocale, '2018-05-29', 'quarters')).to.equal(1); + expect(adapter.getDiff(testDateLocale, '2018-09-29', 'days')).to.equal(31); + expect(adapter.getDiff(testDateLocale, '2018-09-29', 'weeks')).to.equal(4); + }); }); - it('Method: addMonths', () => { - expect(adapter.format(adapter.addMonths(testDate, 2), 'monthAndYear')).to.equal( - 'December 2018', + it('Method: isEqual', () => { + expect(adapter.isEqual(adapter.date(null), null)).to.equal(true); + expect(adapter.isEqual(testDateIso, adapter.date(TEST_DATE_ISO_STRING))).to.equal(true); + expect(adapter.isEqual(null, testDateIso)).to.equal(false); + expect(adapter.isEqual(testDateLocale, adapter.date(TEST_DATE_LOCALE_STRING))).to.equal(true); + expect(adapter.isEqual(null, testDateLocale)).to.equal(false); + }); + + it('Method: isSameYear', () => { + expect(adapter.isSameYear(testDateIso, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameYear(testDateIso, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, + ); + expect(adapter.isSameYear(testDateLocale, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameYear(testDateLocale, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, ); - expect(adapter.format(adapter.addMonths(testDate, -2), 'monthAndYear')).to.equal('August 2018'); }); - it('Method: addWeeks', () => { - expect(adapter.getDiff(adapter.addWeeks(testDate, 1), testDate, 'weeks')).to.equal(1); - expect(adapter.getDiff(adapter.addWeeks(testDate, -1), testDate, 'weeks')).to.equal(-1); + it('Method: isSameMonth', () => { + expect(adapter.isSameMonth(testDateIso, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameMonth(testDateIso, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, + ); + expect(adapter.isSameMonth(testDateLocale, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameMonth(testDateLocale, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, + ); }); - it('Method: addDays', () => { - expect(adapter.format(adapter.addDays(testDate, 1), 'dayOfMonth')).to.equal('31'); - expect(adapter.format(adapter.addDays(testDate, -1), 'dayOfMonth')).to.equal('29'); + it('Method: isSameDay', () => { + expect(adapter.isSameDay(testDateIso, adapter.date('2018-10-30T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameDay(testDateIso, adapter.date('2019-10-30T00:00:00.000Z')!)).to.equal( + false, + ); + expect(adapter.isSameDay(testDateLocale, adapter.date('2018-10-30T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameDay(testDateLocale, adapter.date('2019-10-30T00:00:00.000Z')!)).to.equal( + false, + ); }); - it('Method: addHours', () => { - expect(adapter.format(adapter.addHours(testDate, 65), 'hours24h')).to.equal('04'); - expect(adapter.format(adapter.addHours(testDate, -5), 'hours24h')).to.equal('06'); + it('Method: isSameHour', () => { + expect(adapter.isSameHour(testDateIso, adapter.date('2018-10-30T11:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameHour(testDateIso, adapter.date('2018-10-30T12:00:00.000Z')!)).to.equal( + false, + ); }); - it('Method: addMinutes', () => { - expect(adapter.format(adapter.addMinutes(testDate, 65), 'minutes')).to.equal('49'); - expect(adapter.format(adapter.addMinutes(testDate, -5), 'minutes')).to.equal('39'); + it('Method: isAfter', () => { + expect(adapter.isAfter(adapter.date()!, testDateIso)).to.equal(true); + expect(adapter.isAfter(testDateIso, adapter.date()!)).to.equal(false); + + expect(adapter.isAfter(adapter.date()!, testDateLocale)).to.equal(true); + expect(adapter.isAfter(testDateLocale, adapter.date()!)).to.equal(false); }); - it('Method: addSeconds', () => { - expect(adapter.format(adapter.addSeconds(testDate, 65), 'seconds')).to.equal('05'); - expect(adapter.format(adapter.addSeconds(testDate, -5), 'seconds')).to.equal('55'); + it('Method: isAfterYear', () => { + const nextYearIso = adapter.addYears(testDateIso, 1); + expect(adapter.isAfterYear(nextYearIso, testDateIso)).to.equal(true); + expect(adapter.isAfterYear(testDateIso, nextYearIso)).to.equal(false); + + const nextYearLocale = adapter.addYears(testDateLocale, 1); + expect(adapter.isAfterYear(nextYearLocale, testDateLocale)).to.equal(true); + expect(adapter.isAfterYear(testDateLocale, nextYearLocale)).to.equal(false); + }); + + it('Method: isAfterDay', () => { + const nextDayIso = adapter.addDays(testDateIso, 1); + expect(adapter.isAfterDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isAfterDay(testDateIso, nextDayIso)).to.equal(false); + + const nextDayLocale = adapter.addDays(testDateLocale, 1); + expect(adapter.isAfterDay(nextDayLocale, testDateLocale)).to.equal(true); + expect(adapter.isAfterDay(testDateLocale, nextDayLocale)).to.equal(false); + }); + + it('Method: isBefore', () => { + expect(adapter.isBefore(testDateIso, adapter.date()!)).to.equal(true); + expect(adapter.isBefore(adapter.date()!, testDateIso)).to.equal(false); + + expect(adapter.isBefore(testDateLocale, adapter.date()!)).to.equal(true); + expect(adapter.isBefore(adapter.date()!, testDateLocale)).to.equal(false); + }); + + it('Method: isBeforeYear', () => { + const nextYearIso = adapter.addYears(testDateIso, -1); + expect(adapter.isBeforeYear(nextYearIso, testDateIso)).to.equal(true); + expect(adapter.isBeforeYear(testDateIso, nextYearIso)).to.equal(false); + + const nextYearLocale = adapter.addYears(testDateLocale, -1); + expect(adapter.isBeforeYear(nextYearLocale, testDateLocale)).to.equal(true); + expect(adapter.isBeforeYear(testDateLocale, nextYearLocale)).to.equal(false); + }); + + it('Method: isBeforeDay', () => { + const nextDayIso = adapter.addDays(testDateIso, -1); + expect(adapter.isBeforeDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isBeforeDay(testDateIso, nextDayIso)).to.equal(false); + + const nextDayLocale = adapter.addDays(testDateLocale, -1); + expect(adapter.isBeforeDay(nextDayLocale, testDateLocale)).to.equal(true); + expect(adapter.isBeforeDay(testDateLocale, nextDayLocale)).to.equal(false); + }); + + describe('Method: isWithinRange', () => { + it('should work on simple examples', () => { + expect( + adapter.isWithinRange(adapter.date('2019-10-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-11-01T00:00:00.000Z')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-11-01T00:00:00.000Z')!, + ]), + ).to.equal(false); + + expect( + adapter.isWithinRange(adapter.date('2019-10-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-11-01')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-11-01')!, + ]), + ).to.equal(false); + }); + + it('should use inclusivity of range', () => { + expect( + adapter.isWithinRange(adapter.date('2019-09-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-12-01T00:00:00.000Z')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-12-01T00:00:00.000Z')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-09-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-12-01')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-12-01')!, + ]), + ).to.equal(true); + }); }); it('Method: startOfYear', () => { - expect(adapter.formatByString(adapter.startOfYear(testDate), formatDateTime)).to.equal( - '2018-01-01 00:00:00', - ); + const expected = '2018-01-01T00:00:00.000Z'; + expect(adapter.startOfYear(testDateIso)).toEqualDateTime(expected); + expect(adapter.startOfYear(testDateLocale)).toEqualDateTime(expected); }); it('Method: startOfMonth', () => { - expect(adapter.formatByString(adapter.startOfMonth(testDate), formatDateTime)).to.equal( - '2018-10-01 00:00:00', - ); + const expected = '2018-10-01T00:00:00.000Z'; + expect(adapter.startOfMonth(testDateIso)).toEqualDateTime(expected); + expect(adapter.startOfMonth(testDateLocale)).toEqualDateTime(expected); }); it('Method: startOfWeek', () => { - expect(adapter.formatByString(adapter.startOfWeek(testDate), formatDateTime)).to.equal( - adapter.lib === 'luxon' ? '2018-10-29 00:00:00' : '2018-10-28 00:00:00', - ); - - // Non ISO - expect( - adapter.formatByString( - adapter.startOfWeek(adapter.date('2018-10-28T00:00:00.000Z')!), - formatDateTime, - ), - ).to.equal(adapter.lib === 'luxon' ? '2018-10-22 00:00:00' : '2018-10-28 00:00:00'); + const expected = + adapter.lib === 'luxon' ? '2018-10-29T00:00:00.000Z' : '2018-10-28T00:00:00.000Z'; + expect(adapter.startOfWeek(testDateIso)).toEqualDateTime(expected); + expect(adapter.startOfWeek(testDateLocale)).toEqualDateTime(expected); }); it('Method: startOfDay', () => { - expect(adapter.formatByString(adapter.startOfDay(testDate), formatDateTime)).to.equal( - '2018-10-30 00:00:00', - ); + const expected = '2018-10-30T00:00:00.000Z'; + expect(adapter.startOfDay(testDateIso)).toEqualDateTime(expected); + expect(adapter.startOfDay(testDateLocale)).toEqualDateTime(expected); }); it('Method: endOfYear', () => { - expect(adapter.formatByString(adapter.endOfYear(testDate), formatDateTime)).to.equal( - '2018-12-31 23:59:59', - ); + const expected = '2018-12-31T23:59:59.999Z'; + expect(adapter.endOfYear(testDateIso)).toEqualDateTime(expected); + expect(adapter.endOfYear(testDateLocale)).toEqualDateTime(expected); }); it('Method: endOfMonth', () => { - expect(adapter.formatByString(adapter.endOfMonth(testDate), formatDateTime)).to.equal( - '2018-10-31 23:59:59', - ); + const expected = '2018-10-31T23:59:59.999Z'; + expect(adapter.endOfMonth(testDateIso)).toEqualDateTime(expected); + expect(adapter.endOfMonth(testDateLocale)).toEqualDateTime(expected); }); it('Method: endOfWeek', () => { - expect(adapter.formatByString(adapter.endOfWeek(testDate), formatDateTime)).to.equal( - adapter.lib === 'luxon' ? '2018-11-04 23:59:59' : '2018-11-03 23:59:59', - ); - - // Non ISO - expect( - adapter.formatByString( - adapter.endOfWeek(adapter.date('2018-10-28T00:00:00.000Z')!), - formatDateTime, - ), - ).to.equal(adapter.lib === 'luxon' ? '2018-10-28 23:59:59' : '2018-11-03 23:59:59'); + const expected = + adapter.lib === 'luxon' ? '2018-11-04T23:59:59.999Z' : '2018-11-03T23:59:59.999Z'; + expect(adapter.endOfWeek(testDateIso)).toEqualDateTime(expected); + expect(adapter.endOfWeek(testDateLocale)).toEqualDateTime(expected); }); it('Method: endOfDay', () => { - expect(adapter.formatByString(adapter.endOfDay(testDate), formatDateTime)).to.equal( - '2018-10-30 23:59:59', - ); + const expected = '2018-10-30T23:59:59.999Z'; + expect(adapter.endOfDay(testDateIso)).toEqualDateTime(expected); + expect(adapter.endOfDay(testDateLocale)).toEqualDateTime(expected); }); - it('Method: getPreviousMonth', () => { - expect(adapter.formatByString(adapter.getPreviousMonth(testDate), formatDateTime)).to.equal( - '2018-09-30 11:44:00', - ); + it('Method: addYears', () => { + expect(adapter.addYears(testDateIso, 2)).toEqualDateTime('2020-10-30T11:44:00.000Z'); + expect(adapter.addYears(testDateIso, -2)).toEqualDateTime('2016-10-30T11:44:00.000Z'); }); - it('Method: getMonthArray', () => { - expect( - adapter.getMonthArray(testDate).map((date) => adapter.formatByString(date, formatDateTime)), - ).to.deep.equal([ - '2018-01-01 00:00:00', - '2018-02-01 00:00:00', - '2018-03-01 00:00:00', - '2018-04-01 00:00:00', - '2018-05-01 00:00:00', - '2018-06-01 00:00:00', - '2018-07-01 00:00:00', - '2018-08-01 00:00:00', - '2018-09-01 00:00:00', - '2018-10-01 00:00:00', - '2018-11-01 00:00:00', - '2018-12-01 00:00:00', - ]); + it('Method: addMonths', () => { + expect(adapter.addMonths(testDateIso, 2)).toEqualDateTime('2018-12-30T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, -2)).toEqualDateTime('2018-08-30T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, 3)).toEqualDateTime('2019-01-30T11:44:00.000Z'); }); - it('Method: getNextMonth', () => { - expect(adapter.formatByString(adapter.getNextMonth(testDate), formatDateTime)).to.equal( - '2018-11-30 11:44:00', - ); + it('Method: addWeeks', () => { + expect(adapter.addWeeks(testDateIso, 2)).toEqualDateTime('2018-11-13T11:44:00.000Z'); + expect(adapter.addWeeks(testDateIso, -2)).toEqualDateTime('2018-10-16T11:44:00.000Z'); }); - it('Method: getHours', () => { - expect(adapter.getHours(testDate)).to.equal(new Date(testDateISO).getHours()); + it('Method: addDays', () => { + expect(adapter.addDays(testDateIso, 2)).toEqualDateTime('2018-11-01T11:44:00.000Z'); + expect(adapter.addDays(testDateIso, -2)).toEqualDateTime('2018-10-28T11:44:00.000Z'); }); - it('Method: getMinutes', () => { - expect(adapter.getMinutes(testDate)).to.equal(44); + it('Method: addHours', () => { + expect(adapter.addHours(testDateIso, 2)).toEqualDateTime('2018-10-30T13:44:00.000Z'); + expect(adapter.addHours(testDateIso, -2)).toEqualDateTime('2018-10-30T09:44:00.000Z'); + expect(adapter.addHours(testDateIso, 15)).toEqualDateTime('2018-10-31T02:44:00.000Z'); }); - it('Method: getSeconds', () => { - expect(adapter.getSeconds(testDate)).to.equal(0); + it('Method: addMinutes', () => { + expect(adapter.addMinutes(testDateIso, 2)).toEqualDateTime('2018-10-30T11:46:00.000Z'); + expect(adapter.addMinutes(testDateIso, -2)).toEqualDateTime('2018-10-30T11:42:00.000Z'); + expect(adapter.addMinutes(testDateIso, 20)).toEqualDateTime('2018-10-30T12:04:00.000Z'); }); - it('Method: getDate', () => { - expect(adapter.getDate(testDate)).to.equal(30); + it('Method: addSeconds', () => { + expect(adapter.addSeconds(testDateIso, 2)).toEqualDateTime('2018-10-30T11:44:02.000Z'); + expect(adapter.addSeconds(testDateIso, -2)).toEqualDateTime('2018-10-30T11:43:58.000Z'); + expect(adapter.addSeconds(testDateIso, 70)).toEqualDateTime('2018-10-30T11:45:10.000Z'); }); it('Method: getYear', () => { - expect(adapter.getYear(testDate)).to.equal(2018); + expect(adapter.getYear(testDateIso)).to.equal(2018); }); it('Method: getMonth', () => { - expect(adapter.getMonth(testDate)).to.equal(9); + expect(adapter.getMonth(testDateIso)).to.equal(9); }); - it('Method: getDaysInMonth', () => { - expect(adapter.getDaysInMonth(testDate)).to.equal(31); + it('Method: getDate', () => { + expect(adapter.getDate(testDateIso)).to.equal(30); }); - it('Method: setMonth', () => { - const updatedTime = adapter.formatByString(adapter.setMonth(testDate, 4), formatDateTime); - expect(updatedTime).to.equal('2018-05-30 11:44:00'); + it('Method: getHours', () => { + expect(adapter.getHours(testDateIso)).to.equal(11); }); - it('Method: setHours', () => { - const updatedTime = adapter.formatByString(adapter.setHours(testDate, 0), formatDateTime); - expect(updatedTime).to.equal('2018-10-30 00:44:00'); + it('Method: getMinutes', () => { + expect(adapter.getMinutes(testDateIso)).to.equal(44); }); - it('Method: setMinutes', () => { - const updatedTime = adapter.formatByString(adapter.setMinutes(testDate, 12), formatDateTime); - expect(updatedTime).to.equal('2018-10-30 11:12:00'); + it('Method: getSeconds', () => { + expect(adapter.getSeconds(testDateIso)).to.equal(0); }); - it('Method: setMinutes', () => { - const updatedTime = adapter.formatByString(adapter.setMinutes(testDate, 12), formatDateTime); - expect(updatedTime).to.equal('2018-10-30 11:12:00'); + it('Method: setYear', () => { + expect(adapter.setYear(testDateIso, 2011)).toEqualDateTime('2011-10-30T11:44:00.000Z'); }); - it('Method: setYear', () => { - const updatedTime = adapter.formatByString(adapter.setYear(testDate, 2011), formatDateTime); - expect(updatedTime).to.equal('2011-10-30 11:44:00'); + it('Method: setMonth', () => { + expect(adapter.setMonth(testDateIso, 4)).toEqualDateTime('2018-05-30T11:44:00.000Z'); }); it('Method: setDate', () => { - const updatedTime = adapter.formatByString(adapter.setDate(testDate, 15), formatDateTime); - expect(updatedTime).to.equal('2018-10-15 11:44:00'); + expect(adapter.setDate(testDateIso, 15)).toEqualDateTime('2018-10-15T11:44:00.000Z'); }); - it('Method: setSeconds', () => { - const updatedValue = adapter.formatByString(adapter.setSeconds(testDate, 11), formatDateTime); + it('Method: setHours', () => { + expect(adapter.setHours(testDateIso, 0)).toEqualDateTime('2018-10-30T00:44:00.000Z'); + }); - expect(updatedValue).to.equal('2018-10-30 11:44:11'); + it('Method: setMinutes', () => { + expect(adapter.setMinutes(testDateIso, 12)).toEqualDateTime('2018-10-30T11:12:00.000Z'); }); - it('Method: isAfter', () => { - expect(adapter.isAfter(adapter.date()!, testDate)).to.equal(true); - expect(adapter.isAfter(testDate, adapter.date()!)).to.equal(false); + it('Method: setSeconds', () => { + expect(adapter.setSeconds(testDateIso, 11)).toEqualDateTime('2018-10-30T11:44:11.000Z'); }); - it('Method: isBefore', () => { - expect(adapter.isBefore(testDate, adapter.date()!)).to.equal(true); - expect(adapter.isBefore(adapter.date()!, testDate)).to.equal(false); + it('Method: getDaysInMonth', () => { + expect(adapter.getDaysInMonth(testDateIso)).to.equal(31); + expect(adapter.getDaysInMonth(testDateLocale)).to.equal(31); + expect(adapter.getDaysInMonth(adapter.addMonths(testDateIso, 1))).to.equal(30); }); - it('Method: isAfterDay', () => { - const nextDay = adapter.addDays(testDate, 1); + it('Method: getNextMonth', () => { + expect(adapter.getNextMonth(testDateIso)).toEqualDateTime('2018-11-30T11:44:00.000Z'); + }); - expect(adapter.isAfterDay(nextDay, testDate)).to.equal(true); - expect(adapter.isAfterDay(testDate, nextDay)).to.equal(false); + it('Method: getPreviousMonth', () => { + expect(adapter.getPreviousMonth(testDateIso)).toEqualDateTime('2018-09-30T11:44:00.000Z'); }); - it('Method: isBeforeDay', () => { - const previousDay = adapter.addDays(testDate, -1); + it('Method: getMonthArray', () => { + const monthArray = adapter.getMonthArray(testDateIso); + let expectedDate = adapter.date('2018-01-01T00:00:00.000Z')!; - expect(adapter.isBeforeDay(testDate, previousDay)).to.equal(false); - expect(adapter.isBeforeDay(previousDay, testDate)).to.equal(true); + monthArray.forEach((month) => { + expect(month).toEqualDateTime(expectedDate); + expectedDate = adapter.addMonths(expectedDate, 1)!; + }); }); - it('Method: isAfterYear', () => { - const nextYear = adapter.setYear(testDate, 2019); + it('Method: mergeDateAndTime', () => { + const mergedDate = adapter.mergeDateAndTime( + testDateIso, + adapter.date('2018-01-01T14:15:16.000Z')!, + ); - expect(adapter.isAfterYear(nextYear, testDate)).to.equal(true); - expect(adapter.isAfterYear(testDate, nextYear)).to.equal(false); + expect(adapter.toJsDate(mergedDate)).toEqualDateTime('2018-10-30T14:15:16.000Z'); }); - it('Method: isBeforeYear', () => { - const previousYear = adapter.setYear(testDate, 2017); + it('Method: getWeekdays', () => { + const weekDays = adapter.getWeekdays(); + let date = adapter.startOfWeek(testDateIso); - expect(adapter.isBeforeYear(testDate, previousYear)).to.equal(false); - expect(adapter.isBeforeYear(previousYear, testDate)).to.equal(true); + weekDays.forEach((dayLabel) => { + expect(adapter.format(date, 'weekday').startsWith(dayLabel)).to.equal(true); + date = adapter.addDays(date, 1); + }); }); it('Method: getWeekArray', () => { - const weekArray = adapter.getWeekArray(testDate); + const weekArray = adapter.getWeekArray(testDateIso); expect(weekArray).to.have.length(5); weekArray.forEach((week) => { @@ -278,171 +532,20 @@ export const testCalculations: DescribeGregorianAdapterTestSuite = ({ }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDate)).to.equal(44); + expect(adapter.getWeekNumber!(testDateIso)).to.equal(44); }); it('Method: getYearRange', () => { - const yearRange = adapter.getYearRange(testDate, adapter.setYear(testDate, 2124)); + const yearRange = adapter.getYearRange(testDateIso, adapter.setYear(testDateIso, 2124)); expect(yearRange).to.have.length(107); expect(adapter.getYear(yearRange[yearRange.length - 1])).to.equal(2124); const emptyYearRange = adapter.getYearRange( - testDate, - adapter.setYear(testDate, adapter.getYear(testDate) - 1), + testDateIso, + adapter.setYear(testDateIso, adapter.getYear(testDateIso) - 1), ); expect(emptyYearRange).to.have.length(0); }); - - it('Method: getDiff', () => { - expect(adapter.getDiff(testDate, adapter.date('2018-10-29T11:44:00.000Z')!)).to.equal(86400000); - expect(adapter.getDiff(testDate, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( - -86400000, - ); - expect(adapter.getDiff(testDate, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( - -86400000, - ); - - // With units - expect(adapter.getDiff(testDate, adapter.date('2017-09-29T11:44:00.000Z')!, 'years')).to.equal( - 1, - ); - expect(adapter.getDiff(testDate, adapter.date('2018-08-29T11:44:00.000Z')!, 'months')).to.equal( - 2, - ); - expect( - adapter.getDiff(testDate, adapter.date('2018-05-29T11:44:00.000Z')!, 'quarters'), - ).to.equal(1); - expect(adapter.getDiff(testDate, adapter.date('2018-09-29T11:44:00.000Z')!, 'days')).to.equal( - 31, - ); - expect(adapter.getDiff(testDate, adapter.date('2018-09-29T11:44:00.000Z')!, 'weeks')).to.equal( - 4, - ); - expect(adapter.getDiff(testDate, adapter.date('2018-09-29T11:44:00.000Z')!, 'hours')).to.equal( - 744, - ); - - expect( - adapter.getDiff(testDate, adapter.date('2018-09-29T11:44:00.000Z')!, 'minutes'), - ).to.equal(44640); - - expect( - adapter.getDiff(testDate, adapter.date('2018-10-30T10:44:00.000Z')!, 'seconds'), - ).to.equal(3600); - - expect( - adapter.getDiff(testDate, adapter.date('2018-10-30T10:44:00.000Z')!, 'milliseconds'), - ).to.equal(3600000); - }); - - it('Method: mergeDateAndTime', () => { - const mergedDate = adapter.mergeDateAndTime( - testDate, - adapter.date('2018-01-01T14:15:16.000Z')!, - ); - - expect(adapter.toJsDate(mergedDate).toISOString()).to.equal('2018-10-30T14:15:16.000Z'); - }); - - it('Method: isEqual', () => { - expect(adapter.isEqual(adapter.date(null), null)).to.equal(true); - expect(adapter.isEqual(testDate, adapter.date(testDateISO))).to.equal(true); - expect(adapter.isEqual(null, adapter.date(testDateISO))).to.equal(false); - }); - - it('Method: parseISO', () => { - const parsedDate = adapter.parseISO(testDateISO); - const outputtedISO = adapter.toISO(parsedDate); - - if (adapter.lib === 'date-fns') { - // date-fns never suppress useless milliseconds in the end - expect(outputtedISO).to.equal(testDateISO.replace('.000Z', 'Z')); - } else if (adapter.lib === 'luxon') { - // luxon does not shorthand +00:00 to Z, which is also valid ISO string - expect(outputtedISO).to.equal(testDateISO.replace('Z', '+00:00')); - } else { - expect(outputtedISO).to.equal(testDateISO); - } - }); - - it('Method: parse', () => { - const parsedDate = adapter.parse('2018-10-30 11:44:00', formatDateTime); - expect(adapter.isEqual(parsedDate, testDate)).to.equal(true); - expect(adapter.parse('', formatDateTime)).to.equal(null); - - // Invalid input - const invalidateParsedDate = adapter.parse('99-99-9999', formatDateTime); - expect(adapter.isValid(invalidateParsedDate)).to.equal(false); - }); - - it('Method: isNull', () => { - expect(adapter.isNull(null)).to.equal(true); - expect(adapter.isNull(testDate)).to.equal(false); - }); - - it('Method: isSameDay', () => { - expect(adapter.isSameDay(testDate, adapter.date('2018-10-30T00:00:00.000Z')!)).to.equal(true); - expect(adapter.isSameDay(testDate, adapter.date('2019-10-30T00:00:00.000Z')!)).to.equal(false); - }); - - it('Method: isSameMonth', () => { - expect(adapter.isSameMonth(testDate, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal(true); - expect(adapter.isSameMonth(testDate, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( - false, - ); - }); - - it('Method: isSameYear', () => { - expect(adapter.isSameYear(testDate, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal(true); - expect(adapter.isSameYear(testDate, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal(false); - }); - - it('Method: isSameHour', () => { - expect(adapter.isSameHour(testDate, adapter.date(testDateISO)!)).to.equal(true); - expect(adapter.isSameHour(testDate, adapter.addDays(adapter.date(testDateISO)!, 5))).to.equal( - false, - ); - }); - - it('Method: getCurrentLocaleCode', () => { - // Returns the default location - expect(adapter.getCurrentLocaleCode()).to.match(/en/); - }); - - it('Method: toJsDate', () => { - expect(adapter.toJsDate(testDate)).to.be.instanceOf(Date); - }); - - it('Method: isWithinRange', () => { - expect( - adapter.isWithinRange(adapter.date('2019-10-01T00:00:00.000Z')!, [ - adapter.date('2019-09-01T00:00:00.000Z')!, - adapter.date('2019-11-01T00:00:00.000Z')!, - ]), - ).to.equal(true); - - expect( - adapter.isWithinRange(adapter.date('2019-12-01T00:00:00.000Z')!, [ - adapter.date('2019-09-01T00:00:00.000Z')!, - adapter.date('2019-11-01T00:00:00.000Z')!, - ]), - ).to.equal(false); - - // Should use inclusivity of range - expect( - adapter.isWithinRange(adapter.date('2019-09-01T00:00:00.000Z')!, [ - adapter.date('2019-09-01T00:00:00.000Z')!, - adapter.date('2019-12-01T00:00:00.000Z')!, - ]), - ).to.equal(true); - - expect( - adapter.isWithinRange(adapter.date('2019-12-01T00:00:00.000Z')!, [ - adapter.date('2019-09-01T00:00:00.000Z')!, - adapter.date('2019-12-01T00:00:00.000Z')!, - ]), - ).to.equal(true); - }); }; diff --git a/packages/x-date-pickers/src/tests/describeGregorianAdapter/testLocalization.ts b/packages/x-date-pickers/src/tests/describeGregorianAdapter/testLocalization.ts index 23e8b9ec68208..f979dabb5a557 100644 --- a/packages/x-date-pickers/src/tests/describeGregorianAdapter/testLocalization.ts +++ b/packages/x-date-pickers/src/tests/describeGregorianAdapter/testLocalization.ts @@ -1,7 +1,13 @@ import { expect } from 'chai'; +import sinon from 'sinon'; +import { AdapterFormats } from '@mui/x-date-pickers/models'; +import { cleanText } from 'test/utils/pickers-utils'; import { DescribeGregorianAdapterTestSuite } from './describeGregorianAdapter.types'; +import { TEST_DATE_ISO_STRING } from './describeGregorianAdapter.utils'; export const testLocalization: DescribeGregorianAdapterTestSuite = ({ adapter }) => { + const testDateIso = adapter.date(TEST_DATE_ISO_STRING)!; + it('Method: formatNumber', () => { expect(adapter.formatNumber('1')).to.equal('1'); }); @@ -9,6 +15,37 @@ export const testLocalization: DescribeGregorianAdapterTestSuite = ({ adapter }) it('Method: getMeridiemText', () => { expect(adapter.getMeridiemText('am')).to.equal('AM'); expect(adapter.getMeridiemText('pm')).to.equal('PM'); + + // Moment only translates for 12-hour cycle. + if (adapter.lib === 'moment') { + const stub = sinon.stub(adapter, 'is12HourCycleInCurrentLocale').returns(false); + expect(adapter.getMeridiemText('am')).to.equal('AM'); + expect(adapter.getMeridiemText('pm')).to.equal('PM'); + stub.restore(); + } + }); + + it('Method: expandFormat', () => { + const testFormat = (formatKey: keyof AdapterFormats) => { + const formatString = adapter.formats[formatKey]; + const expandedFormat = cleanText(adapter.expandFormat(formatString)); + + if (expandedFormat === formatString) { + return; + } + + // The expanded format should be fully expanded + expect(cleanText(adapter.expandFormat(expandedFormat))).to.equal(expandedFormat); + + // Both format should be equivalent + expect(cleanText(adapter.formatByString(testDateIso, expandedFormat))).to.equal( + cleanText(adapter.format(testDateIso, formatKey)), + ); + }; + + Object.keys(adapter.formats).forEach((formatKey) => { + testFormat(formatKey as keyof AdapterFormats); + }); }); it('Method: getFormatHelperText', () => { @@ -19,4 +56,9 @@ export const testLocalization: DescribeGregorianAdapterTestSuite = ({ adapter }) adapter.lib === 'luxon' ? 'm/d/yyyy hh:mm (a|p)m' : 'mm/dd/yyyy hh:mm (a|p)m', ); }); + + it('Method: getCurrentLocaleCode', () => { + // Returns the default locale + expect(adapter.getCurrentLocaleCode()).to.match(/en/); + }); }; diff --git a/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.ts b/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.ts index 898530d8668e8..199c178b0ad12 100644 --- a/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.ts +++ b/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.ts @@ -3,7 +3,6 @@ import { MuiPickersAdapter } from '@mui/x-date-pickers'; import { testCalculations } from './testCalculations'; import { testLocalization } from './testLocalization'; import { DescribeHijriAdapterParams } from './describeHijriAdapter.types'; -import { TEST_DATE_ISO } from '../describeGregorianAdapter'; function innerJalaliDescribeAdapter( Adapter: new (...args: any) => MuiPickersAdapter, @@ -15,7 +14,6 @@ function innerJalaliDescribeAdapter( const testSuitParams = { ...params, adapter, - testDate: adapter.date(TEST_DATE_ISO), }; if (params.before) { diff --git a/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.types.ts b/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.types.ts index 0d6590b7e2bd8..30fb1493e7683 100644 --- a/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.types.ts +++ b/packages/x-date-pickers/src/tests/describeHijriAdapter/describeHijriAdapter.types.ts @@ -7,5 +7,4 @@ export interface DescribeHijriAdapterParams { export type DescribeHijriAdapterTestSuite = (params: { adapter: MuiPickersAdapter; - testDate: TDate; }) => void; diff --git a/packages/x-date-pickers/src/tests/describeHijriAdapter/testCalculations.ts b/packages/x-date-pickers/src/tests/describeHijriAdapter/testCalculations.ts index 222636a1cc00d..5f97a06a8e1f6 100644 --- a/packages/x-date-pickers/src/tests/describeHijriAdapter/testCalculations.ts +++ b/packages/x-date-pickers/src/tests/describeHijriAdapter/testCalculations.ts @@ -1,8 +1,10 @@ import { expect } from 'chai'; import { DescribeHijriAdapterTestSuite } from './describeHijriAdapter.types'; -import { TEST_DATE_ISO } from '../describeGregorianAdapter'; +import { TEST_DATE_ISO_STRING } from '../describeGregorianAdapter'; + +export const testCalculations: DescribeHijriAdapterTestSuite = ({ adapter }) => { + const testDateIso = adapter.date(TEST_DATE_ISO_STRING)!; -export const testCalculations: DescribeHijriAdapterTestSuite = ({ adapter, testDate }) => { it('Method: date', () => { expect(adapter.date(null)).to.equal(null); }); @@ -13,74 +15,165 @@ export const testCalculations: DescribeHijriAdapterTestSuite = ({ adapter, testD }); it('Method: isEqual', () => { - const anotherDate = adapter.date(TEST_DATE_ISO); + const anotherDate = adapter.date(TEST_DATE_ISO_STRING); - expect(adapter.isEqual(testDate, anotherDate)).to.equal(true); + expect(adapter.isEqual(testDateIso, anotherDate)).to.equal(true); expect(adapter.isEqual(null, null)).to.equal(true); }); + it('Method: isAfter', () => { + expect(adapter.isAfter(adapter.date()!, testDateIso)).to.equal(true); + expect(adapter.isAfter(testDateIso, adapter.date()!)).to.equal(false); + }); + it('Method: isAfterYear', () => { - const afterYear = adapter.addYears(testDate, 2); - expect(adapter.isAfterYear(afterYear, testDate)).to.equal(true); + const afterYear = adapter.addYears(testDateIso, 2); + expect(adapter.isAfterYear(afterYear, testDateIso)).to.equal(true); + }); + + it('Method: isAfterDay', () => { + const nextDayIso = adapter.addDays(testDateIso, 1); + expect(adapter.isAfterDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isAfterDay(testDateIso, nextDayIso)).to.equal(false); + }); + + it('Method: isBefore', () => { + expect(adapter.isBefore(testDateIso, adapter.date()!)).to.equal(true); + expect(adapter.isBefore(adapter.date()!, testDateIso)).to.equal(false); }); it('Method: isBeforeYear', () => { - const afterYear = adapter.addYears(testDate, 2); - expect(adapter.isBeforeYear(testDate, afterYear)).to.equal(true); + const afterYear = adapter.addYears(testDateIso, 2); + expect(adapter.isBeforeYear(testDateIso, afterYear)).to.equal(true); + }); + + it('Method: isBeforeDay', () => { + const nextDayIso = adapter.addDays(testDateIso, -1); + expect(adapter.isBeforeDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isBeforeDay(testDateIso, nextDayIso)).to.equal(false); }); it('Method: startOfYear', () => { - expect(adapter.startOfYear(testDate)).toEqualDateTime(new Date('2018-09-11T00:00:00.000Z')); + expect(adapter.startOfYear(testDateIso)).toEqualDateTime('2018-09-11T00:00:00.000Z'); + }); + + it('Method: startOfWeek', () => { + expect(adapter.startOfWeek(testDateIso)).toEqualDateTime('2018-10-28T00:00:00.000Z'); + }); + + it('Method: startOfDay', () => { + expect(adapter.startOfDay(testDateIso)).toEqualDateTime('2018-10-30T00:00:00.000Z'); }); it('Method: startOfMonth', () => { - expect(adapter.startOfMonth(testDate)).toEqualDateTime(new Date('2018-10-10T00:00:00.000Z')); + expect(adapter.startOfMonth(testDateIso)).toEqualDateTime('2018-10-10T00:00:00.000Z'); }); it('Method: endOfYear', () => { - expect(adapter.endOfYear(testDate)).toEqualDateTime(new Date('2019-08-30T23:59:59.999Z')); + expect(adapter.endOfYear(testDateIso)).toEqualDateTime('2019-08-30T23:59:59.999Z'); }); it('Method: endOfMonth', () => { - expect(adapter.endOfMonth(testDate)).toEqualDateTime(new Date('2018-11-08T23:59:59.999Z')); + expect(adapter.endOfMonth(testDateIso)).toEqualDateTime('2018-11-08T23:59:59.999Z'); + }); + + it('Method: endOfWeek', () => { + expect(adapter.endOfWeek(testDateIso)).toEqualDateTime('2018-11-03T23:59:59.999Z'); + }); + + it('Method: endOfDay', () => { + expect(adapter.endOfDay(testDateIso)).toEqualDateTime('2018-10-30T23:59:59.999Z'); + }); + + it('Method: addYears', () => { + expect(adapter.addYears(testDateIso, 2)).toEqualDateTime('2020-10-08T11:44:00.000Z'); + expect(adapter.addYears(testDateIso, -2)).toEqualDateTime('2016-11-21T11:44:00.000Z'); + }); + + it('Method: addMonths', () => { + expect(adapter.addMonths(testDateIso, 2)).toEqualDateTime('2018-12-28T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, -2)).toEqualDateTime('2018-09-01T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, 3)).toEqualDateTime('2019-01-27T11:44:00.000Z'); + }); + + it('Method: addWeeks', () => { + expect(adapter.addWeeks(testDateIso, 2)).toEqualDateTime('2018-11-13T11:44:00.000Z'); + expect(adapter.addWeeks(testDateIso, -2)).toEqualDateTime('2018-10-16T11:44:00.000Z'); + }); + + it('Method: addDays', () => { + expect(adapter.addDays(testDateIso, 2)).toEqualDateTime('2018-11-01T11:44:00.000Z'); + expect(adapter.addDays(testDateIso, -2)).toEqualDateTime('2018-10-28T11:44:00.000Z'); + }); + + it('Method: addHours', () => { + expect(adapter.addHours(testDateIso, 2)).toEqualDateTime('2018-10-30T13:44:00.000Z'); + expect(adapter.addHours(testDateIso, -2)).toEqualDateTime('2018-10-30T09:44:00.000Z'); + expect(adapter.addHours(testDateIso, 15)).toEqualDateTime('2018-10-31T02:44:00.000Z'); + }); + + it('Method: addMinutes', () => { + expect(adapter.addMinutes(testDateIso, 2)).toEqualDateTime('2018-10-30T11:46:00.000Z'); + expect(adapter.addMinutes(testDateIso, -2)).toEqualDateTime('2018-10-30T11:42:00.000Z'); + expect(adapter.addMinutes(testDateIso, 20)).toEqualDateTime('2018-10-30T12:04:00.000Z'); + }); + + it('Method: addSeconds', () => { + expect(adapter.addSeconds(testDateIso, 2)).toEqualDateTime('2018-10-30T11:44:02.000Z'); + expect(adapter.addSeconds(testDateIso, -2)).toEqualDateTime('2018-10-30T11:43:58.000Z'); + expect(adapter.addSeconds(testDateIso, 70)).toEqualDateTime('2018-10-30T11:45:10.000Z'); }); it('Method: getYear', () => { - expect(adapter.getYear(testDate)).to.equal(1440); + expect(adapter.getYear(testDateIso)).to.equal(1440); }); it('Method: getMonth', () => { - expect(adapter.getMonth(testDate)).to.equal(1); + expect(adapter.getMonth(testDateIso)).to.equal(1); }); it('Method: getDate', () => { - expect(adapter.getDate(testDate)).to.equal(21); + expect(adapter.getDate(testDateIso)).to.equal(21); }); it('Method: setYear', () => { - expect(adapter.setYear(testDate, 1441)).toEqualDateTime(new Date('2019-10-20T11:44:00.000Z')); + expect(adapter.setYear(testDateIso, 1441)).toEqualDateTime('2019-10-20T11:44:00.000Z'); + }); + + it('Method: setMonth', () => { + expect(adapter.setMonth(testDateIso, 4)).toEqualDateTime('2019-01-27T11:44:00.000Z'); }); it('Method: setDate', () => { - expect(adapter.setDate(testDate, 22)).toEqualDateTime(new Date('2018-10-31T11:44:00.000Z')); + expect(adapter.setDate(testDateIso, 22)).toEqualDateTime('2018-10-31T11:44:00.000Z'); }); it('Method: getNextMonth', () => { - expect(adapter.getNextMonth(testDate)).toEqualDateTime(new Date('2018-11-29T11:44:00.000Z')); + expect(adapter.getNextMonth(testDateIso)).toEqualDateTime('2018-11-29T11:44:00.000Z'); }); it('Method: getPreviousMonth', () => { - expect(adapter.getPreviousMonth(testDate)).toEqualDateTime( + expect(adapter.getPreviousMonth(testDateIso)).toEqualDateTime( new Date('2018-10-01T11:44:00.000Z'), ); }); + it('Method: getMonthArray', () => { + const monthArray = adapter.getMonthArray(testDateIso); + let expectedDate = adapter.date('2018-09-11T00:00:00.000Z')!; + + monthArray.forEach((month) => { + expect(month).toEqualDateTime(expectedDate); + expectedDate = adapter.addMonths(expectedDate, 1)!; + }); + }); + it('Method: getWeekdays', () => { expect(adapter.getWeekdays()).to.deep.equal(['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س']); }); it('Method: getWeekArray', () => { - const weekArray = adapter.getWeekArray(testDate); + const weekArray = adapter.getWeekArray(testDateIso); const expectedDate = new Date('2018-10-07T00:00:00.000Z'); weekArray.forEach((week) => { @@ -92,30 +185,30 @@ export const testCalculations: DescribeHijriAdapterTestSuite = ({ adapter, testD }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDate)).to.equal(8); + expect(adapter.getWeekNumber!(testDateIso)).to.equal(8); }); describe('Method: getYearRange', () => { it('Minimum limit', () => { - const anotherYear = adapter.setYear(testDate, 1355); + const anotherYear = adapter.setYear(testDateIso, 1355); - expect(() => adapter.getYearRange(anotherYear, testDate)).to.throw( + expect(() => adapter.getYearRange(anotherYear, testDateIso)).to.throw( 'min date must be on or after 1356-01-01 H (1937-03-14)', ); }); it('Maximum limit', () => { - const anotherYear = adapter.setYear(testDate, 1500); + const anotherYear = adapter.setYear(testDateIso, 1500); - expect(() => adapter.getYearRange(testDate, anotherYear)).to.throw( + expect(() => adapter.getYearRange(testDateIso, anotherYear)).to.throw( 'max date must be on or before 1499-12-29 H (2076-11-26)', ); }); }); it('Method: getYearRange', () => { - const anotherDate = adapter.setYear(testDate, 1445); - const yearRange = adapter.getYearRange(testDate, anotherDate); + const anotherDate = adapter.setYear(testDateIso, 1445); + const yearRange = adapter.getYearRange(testDateIso, anotherDate); expect(yearRange).to.have.length(6); }); }; diff --git a/packages/x-date-pickers/src/tests/describeHijriAdapter/testLocalization.ts b/packages/x-date-pickers/src/tests/describeHijriAdapter/testLocalization.ts index 5eea8aad057dc..524c31fd32281 100644 --- a/packages/x-date-pickers/src/tests/describeHijriAdapter/testLocalization.ts +++ b/packages/x-date-pickers/src/tests/describeHijriAdapter/testLocalization.ts @@ -11,4 +11,16 @@ export const testLocalization: DescribeHijriAdapterTestSuite = ({ adapter }) => expect(adapter.getMeridiemText('am')).to.equal('ص'); expect(adapter.getMeridiemText('pm')).to.equal('م'); }); + + it('Method: getFormatHelperText', () => { + expect(adapter.getFormatHelperText(adapter.formats.keyboardDate)).to.equal('yyyy/mm/dd'); + expect(adapter.getFormatHelperText(adapter.formats.keyboardDateTime12h)).to.equal( + 'yyyy/mm/dd hh:mm (a|p)m', + ); + }); + + it('Method: getCurrentLocaleCode', () => { + // Returns the default locale + expect(adapter.getCurrentLocaleCode()).to.match(/ar/); + }); }; diff --git a/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.ts b/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.ts index 2d05c6565a4f1..fd7b4edd223f8 100644 --- a/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.ts +++ b/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.ts @@ -3,19 +3,17 @@ import { MuiPickersAdapter } from '@mui/x-date-pickers'; import { testCalculations } from './testCalculations'; import { testLocalization } from './testLocalization'; import { DescribeJalaliAdapterParams } from './describeJalaliAdapter.types'; -import { TEST_DATE_ISO } from '../describeGregorianAdapter'; function innerJalaliDescribeAdapter( Adapter: new (...args: any) => MuiPickersAdapter, params: DescribeJalaliAdapterParams, ) { - const adapter = new Adapter({ locale: params.locale }); + const adapter = new Adapter(); describe(adapter.lib, () => { const testSuitParams = { ...params, adapter, - testDate: adapter.date(TEST_DATE_ISO), }; if (params.before) { diff --git a/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.types.ts b/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.types.ts index 2853760e4193d..be7b2904119bd 100644 --- a/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.types.ts +++ b/packages/x-date-pickers/src/tests/describeJalaliAdapter/describeJalaliAdapter.types.ts @@ -1,13 +1,10 @@ import { MuiPickersAdapter } from '@mui/x-date-pickers/models'; export interface DescribeJalaliAdapterParams { - // TODO: Type once the adapter locale is correctly types - locale: any; before?: () => void; after?: () => void; } export type DescribeJalaliAdapterTestSuite = (params: { adapter: MuiPickersAdapter; - testDate: TDate; }) => void; diff --git a/packages/x-date-pickers/src/tests/describeJalaliAdapter/testCalculations.ts b/packages/x-date-pickers/src/tests/describeJalaliAdapter/testCalculations.ts index 14b48b3210156..0f319e09b9dc0 100644 --- a/packages/x-date-pickers/src/tests/describeJalaliAdapter/testCalculations.ts +++ b/packages/x-date-pickers/src/tests/describeJalaliAdapter/testCalculations.ts @@ -1,8 +1,10 @@ import { expect } from 'chai'; import { DescribeJalaliAdapterTestSuite } from './describeJalaliAdapter.types'; -import { TEST_DATE_ISO } from '../describeGregorianAdapter'; +import { TEST_DATE_ISO_STRING } from '../describeGregorianAdapter'; + +export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => { + const testDateIso = adapter.date(TEST_DATE_ISO_STRING)!; -export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter, testDate }) => { it('Method: date', () => { expect(adapter.date(null)).to.equal(null); }); @@ -12,69 +14,302 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter, test expect(adapter.parse('01/01/1395', adapter.formats.keyboardDate)).not.to.equal(null); }); + it('Method: parseISO', () => { + expect(adapter.parseISO(TEST_DATE_ISO_STRING)).toEqualDateTime(testDateIso); + }); + + it('Method: toISO', () => { + const outputtedISO = adapter.toISO(testDateIso); + + if (adapter.lib === 'date-fns-jalali') { + // date-fns never suppress useless milliseconds in the end + expect(outputtedISO).to.equal(TEST_DATE_ISO_STRING.replace('.000Z', 'Z')); + } else { + expect(outputtedISO).to.equal(TEST_DATE_ISO_STRING); + } + }); + + it('Method: isNull', () => { + expect(adapter.isNull(null)).to.equal(true); + expect(adapter.isNull(testDateIso)).to.equal(false); + }); + + describe('Method: getDiff', () => { + it('should compute the millisecond diff when there is no unit', () => { + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-29T11:44:00.000Z')!)).to.equal( + 86400000, + ); + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( + -86400000, + ); + expect(adapter.getDiff(testDateIso, adapter.date('2018-10-31T11:44:00.000Z')!)).to.equal( + -86400000, + ); + }); + + it('should compute the diff in the provided unit (ISO)', () => { + expect( + adapter.getDiff(testDateIso, adapter.date('2017-09-29T11:44:00.000Z')!, 'years'), + ).to.equal(1); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-08-29T11:44:00.000Z')!, 'months'), + ).to.equal(2); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-05-29T11:44:00.000Z')!, 'quarters'), + ).to.equal(1); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'days'), + ).to.equal(31); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'weeks'), + ).to.equal(4); + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'hours'), + ).to.equal(744); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-09-29T11:44:00.000Z')!, 'minutes'), + ).to.equal(44640); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-10-30T10:44:00.000Z')!, 'seconds'), + ).to.equal(3600); + + expect( + adapter.getDiff(testDateIso, adapter.date('2018-10-30T10:44:00.000Z')!, 'milliseconds'), + ).to.equal(3600000); + }); + }); + it('Method: isEqual', () => { - const anotherDate = adapter.date(TEST_DATE_ISO); + const anotherDate = adapter.date(TEST_DATE_ISO_STRING); - expect(adapter.isEqual(testDate, anotherDate)).to.equal(true); + expect(adapter.isEqual(testDateIso, anotherDate)).to.equal(true); expect(adapter.isEqual(null, null)).to.equal(true); }); + it('Method: isSameYear', () => { + expect(adapter.isSameYear(testDateIso, adapter.date('2018-10-01T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameYear(testDateIso, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, + ); + }); + + it('Method: isSameMonth', () => { + expect(adapter.isSameMonth(testDateIso, adapter.date('2018-11-04T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameMonth(testDateIso, adapter.date('2019-10-01T00:00:00.000Z')!)).to.equal( + false, + ); + }); + + it('Method: isSameDay', () => { + expect(adapter.isSameDay(testDateIso, adapter.date('2018-10-30T00:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameDay(testDateIso, adapter.date('2019-10-30T00:00:00.000Z')!)).to.equal( + false, + ); + }); + + it('Method: isSameHour', () => { + expect(adapter.isSameHour(testDateIso, adapter.date('2018-10-30T11:00:00.000Z')!)).to.equal( + true, + ); + expect(adapter.isSameHour(testDateIso, adapter.date('2018-10-30T12:00:00.000Z')!)).to.equal( + false, + ); + }); + + it('Method: isAfter', () => { + expect(adapter.isAfter(adapter.date()!, testDateIso)).to.equal(true); + expect(adapter.isAfter(testDateIso, adapter.date()!)).to.equal(false); + }); + it('Method: isAfterYear', () => { - const afterYear = adapter.addYears(testDate, 2); - expect(adapter.isAfterYear(afterYear, testDate)).to.equal(true); + const afterYear = adapter.addYears(testDateIso, 2); + expect(adapter.isAfterYear(afterYear, testDateIso)).to.equal(true); + }); + + it('Method: isAfterDay', () => { + const nextDayIso = adapter.addDays(testDateIso, 1); + expect(adapter.isAfterDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isAfterDay(testDateIso, nextDayIso)).to.equal(false); + }); + + it('Method: isBefore', () => { + expect(adapter.isBefore(testDateIso, adapter.date()!)).to.equal(true); + expect(adapter.isBefore(adapter.date()!, testDateIso)).to.equal(false); }); it('Method: isBeforeYear', () => { - const afterYear = adapter.addYears(testDate, 2); - expect(adapter.isBeforeYear(testDate, afterYear)).to.equal(true); + const afterYear = adapter.addYears(testDateIso, 2); + expect(adapter.isBeforeYear(testDateIso, afterYear)).to.equal(true); + }); + + it('Method: isBeforeDay', () => { + const nextDayIso = adapter.addDays(testDateIso, -1); + expect(adapter.isBeforeDay(nextDayIso, testDateIso)).to.equal(true); + expect(adapter.isBeforeDay(testDateIso, nextDayIso)).to.equal(false); + }); + + it('Method: isWithinRange', () => { + expect( + adapter.isWithinRange(adapter.date('2019-10-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-11-01T00:00:00.000Z')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01T00:00:00.000Z')!, [ + adapter.date('2019-09-01T00:00:00.000Z')!, + adapter.date('2019-11-01T00:00:00.000Z')!, + ]), + ).to.equal(false); + + expect( + adapter.isWithinRange(adapter.date('2019-10-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-11-01')!, + ]), + ).to.equal(true); + + expect( + adapter.isWithinRange(adapter.date('2019-12-01')!, [ + adapter.date('2019-09-01')!, + adapter.date('2019-11-01')!, + ]), + ).to.equal(false); }); it('Method: startOfYear', () => { - expect(adapter.startOfYear(testDate)).toEqualDateTime(new Date('2018-03-21T00:00:00.000Z')); + expect(adapter.startOfYear(testDateIso)).toEqualDateTime('2018-03-21T00:00:00.000Z'); }); it('Method: startOfMonth', () => { - expect(adapter.startOfMonth(testDate)).toEqualDateTime(new Date('2018-10-23T00:00:00.000Z')); + expect(adapter.startOfMonth(testDateIso)).toEqualDateTime('2018-10-23T00:00:00.000Z'); + }); + + it('Method: startOfWeek', () => { + expect(adapter.startOfWeek(testDateIso)).toEqualDateTime('2018-10-27T00:00:00.000Z'); + }); + + it('Method: startOfDay', () => { + expect(adapter.startOfDay(testDateIso)).toEqualDateTime('2018-10-30T00:00:00.000Z'); }); it('Method: endOfYear', () => { - expect(adapter.endOfYear(testDate)).toEqualDateTime(new Date('2019-03-20T23:59:59.999Z')); + expect(adapter.endOfYear(testDateIso)).toEqualDateTime('2019-03-20T23:59:59.999Z'); }); it('Method: endOfMonth', () => { - expect(adapter.endOfMonth(testDate)).toEqualDateTime(new Date('2018-11-21T23:59:59.999Z')); + expect(adapter.endOfMonth(testDateIso)).toEqualDateTime('2018-11-21T23:59:59.999Z'); + }); + + it('Method: endOfWeek', () => { + expect(adapter.endOfWeek(testDateIso)).toEqualDateTime('2018-11-02T23:59:59.999Z'); + }); + + it('Method: endOfDay', () => { + expect(adapter.endOfDay(testDateIso)).toEqualDateTime('2018-10-30T23:59:59.999Z'); + }); + + it('Method: addYears', () => { + expect(adapter.addYears(testDateIso, 2)).toEqualDateTime('2020-10-29T11:44:00.000Z'); + expect(adapter.addYears(testDateIso, -2)).toEqualDateTime('2016-10-29T11:44:00.000Z'); + }); + + it('Method: addMonths', () => { + expect(adapter.addMonths(testDateIso, 2)).toEqualDateTime('2018-12-29T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, -2)).toEqualDateTime('2018-08-30T11:44:00.000Z'); + expect(adapter.addMonths(testDateIso, 3)).toEqualDateTime('2019-01-28T11:44:00.000Z'); + }); + + it('Method: addWeeks', () => { + expect(adapter.addWeeks(testDateIso, 2)).toEqualDateTime('2018-11-13T11:44:00.000Z'); + expect(adapter.addWeeks(testDateIso, -2)).toEqualDateTime('2018-10-16T11:44:00.000Z'); + }); + + it('Method: addDays', () => { + expect(adapter.addDays(testDateIso, 2)).toEqualDateTime('2018-11-01T11:44:00.000Z'); + expect(adapter.addDays(testDateIso, -2)).toEqualDateTime('2018-10-28T11:44:00.000Z'); + }); + + it('Method: addHours', () => { + expect(adapter.addHours(testDateIso, 2)).toEqualDateTime('2018-10-30T13:44:00.000Z'); + expect(adapter.addHours(testDateIso, -2)).toEqualDateTime('2018-10-30T09:44:00.000Z'); + expect(adapter.addHours(testDateIso, 15)).toEqualDateTime('2018-10-31T02:44:00.000Z'); + }); + + it('Method: addMinutes', () => { + expect(adapter.addMinutes(testDateIso, 2)).toEqualDateTime('2018-10-30T11:46:00.000Z'); + expect(adapter.addMinutes(testDateIso, -2)).toEqualDateTime('2018-10-30T11:42:00.000Z'); + expect(adapter.addMinutes(testDateIso, 20)).toEqualDateTime('2018-10-30T12:04:00.000Z'); + }); + + it('Method: addSeconds', () => { + expect(adapter.addSeconds(testDateIso, 2)).toEqualDateTime('2018-10-30T11:44:02.000Z'); + expect(adapter.addSeconds(testDateIso, -2)).toEqualDateTime('2018-10-30T11:43:58.000Z'); + expect(adapter.addSeconds(testDateIso, 70)).toEqualDateTime('2018-10-30T11:45:10.000Z'); }); it('Method: getYear', () => { - expect(adapter.getYear(testDate)).to.equal(1397); + expect(adapter.getYear(testDateIso)).to.equal(1397); }); it('Method: getMonth', () => { - expect(adapter.getMonth(testDate)).to.equal(7); + expect(adapter.getMonth(testDateIso)).to.equal(7); }); it('Method: getDate', () => { - expect(adapter.getDate(testDate)).to.equal(8); + expect(adapter.getDate(testDateIso)).to.equal(8); }); it('Method: setYear', () => { - expect(adapter.setYear(testDate, 1398)).toEqualDateTime(new Date('2019-10-30T11:44:00.000Z')); + expect(adapter.setYear(testDateIso, 1398)).toEqualDateTime('2019-10-30T11:44:00.000Z'); + }); + + it('Method: setMonth', () => { + expect(adapter.setMonth(testDateIso, 4)).toEqualDateTime('2018-07-30T11:44:00.000Z'); }); it('Method: setDate', () => { - expect(adapter.setDate(testDate, 9)).toEqualDateTime(new Date('2018-10-31T11:44:00.000Z')); + expect(adapter.setDate(testDateIso, 9)).toEqualDateTime('2018-10-31T11:44:00.000Z'); }); it('Method: getNextMonth', () => { - expect(adapter.getNextMonth(testDate)).toEqualDateTime(new Date('2018-11-29T11:44:00.000Z')); + expect(adapter.getNextMonth(testDateIso)).toEqualDateTime('2018-11-29T11:44:00.000Z'); }); it('Method: getPreviousMonth', () => { - expect(adapter.getPreviousMonth(testDate)).toEqualDateTime( + expect(adapter.getPreviousMonth(testDateIso)).toEqualDateTime( new Date('2018-09-30T11:44:00.000Z'), ); }); + it('Method: getMonthArray', () => { + const monthArray = adapter.getMonthArray(testDateIso); + let expectedDate = adapter.date('2018-03-21T00:00:00.000Z')!; + + monthArray.forEach((month) => { + expect(month).toEqualDateTime(expectedDate); + expectedDate = adapter.addMonths(expectedDate, 1)!; + }); + }); + + it('Method: mergeDateAndTime', () => { + const mergedDate = adapter.mergeDateAndTime( + testDateIso, + adapter.date('2018-01-01T14:15:16.000Z')!, + ); + + expect(adapter.toJsDate(mergedDate)).toEqualDateTime('2018-10-30T14:15:16.000Z'); + }); + it('Method: getWeekdays', () => { // TODO: AdapterDateFnsJalali `getWeekDays` method seems broken // Same behavior with the date-io adapter. @@ -86,7 +321,7 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter, test }); it('Method: getWeekArray', () => { - const weekArray = adapter.getWeekArray(testDate); + const weekArray = adapter.getWeekArray(testDateIso); const expectedDate = new Date('2018-10-20T00:00:00.000Z'); weekArray.forEach((week) => { @@ -98,12 +333,12 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter, test }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDate)).to.equal(33); + expect(adapter.getWeekNumber!(testDateIso)).to.equal(33); }); it('Method: getYearRange', () => { - const anotherDate = adapter.setYear(testDate, 1400); - const yearRange = adapter.getYearRange(testDate, anotherDate); + const anotherDate = adapter.setYear(testDateIso, 1400); + const yearRange = adapter.getYearRange(testDateIso, anotherDate); expect(yearRange).to.have.length(4); }); }; diff --git a/packages/x-date-pickers/src/tests/describeJalaliAdapter/testLocalization.ts b/packages/x-date-pickers/src/tests/describeJalaliAdapter/testLocalization.ts index f790c0eee533a..0930ee5c7a4e7 100644 --- a/packages/x-date-pickers/src/tests/describeJalaliAdapter/testLocalization.ts +++ b/packages/x-date-pickers/src/tests/describeJalaliAdapter/testLocalization.ts @@ -11,4 +11,16 @@ export const testLocalization: DescribeJalaliAdapterTestSuite = ({ adapter }) => expect(adapter.getMeridiemText('am')).to.equal('ق.ظ'); expect(adapter.getMeridiemText('pm')).to.equal('ب.ظ'); }); + + it('Method: getFormatHelperText', () => { + expect(adapter.getFormatHelperText(adapter.formats.keyboardDate)).to.equal('yyyy/mm/dd'); + expect(adapter.getFormatHelperText(adapter.formats.keyboardDateTime12h)).to.equal( + 'yyyy/mm/dd hh:mm (a|p)m', + ); + }); + + it('Method: getCurrentLocaleCode', () => { + // Returns the default locale + expect(adapter.getCurrentLocaleCode()).to.match(/fa/); + }); }; diff --git a/packages/x-date-pickers/tsconfig.json b/packages/x-date-pickers/tsconfig.json index 10d4ab6a916ae..56b1c72c9db97 100644 --- a/packages/x-date-pickers/tsconfig.json +++ b/packages/x-date-pickers/tsconfig.json @@ -5,6 +5,7 @@ }, "include": [ "src/**/*", + "../../test/utils/addChaiAssertions.ts", "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../node_modules/@mui/material/themeCssVarsAugmentation" ] diff --git a/packages/x-license-pro/tsconfig.json b/packages/x-license-pro/tsconfig.json index 09f5a79f1de62..c3ec7a316176f 100644 --- a/packages/x-license-pro/tsconfig.json +++ b/packages/x-license-pro/tsconfig.json @@ -6,6 +6,7 @@ }, "include": [ "src/**/*", + "../../test/utils/addChaiAssertions.ts", "../../node_modules/@mui/monorepo/test/utils/initMatchers.ts", "../../node_modules/@mui/material/themeCssVarsAugmentation" ] diff --git a/test/utils/addChaiAssertions.ts b/test/utils/addChaiAssertions.ts index 06caa0751ac66..2392de41f5a33 100644 --- a/test/utils/addChaiAssertions.ts +++ b/test/utils/addChaiAssertions.ts @@ -7,7 +7,7 @@ declare global { /** * Matcher with useful error messages if the dates don't match. */ - toEqualDateTime(expected: Date): void; + toEqualDateTime(expected: any): void; } } } @@ -20,8 +20,15 @@ chai.use((chaiAPI, utils) => { // Luxon dates don't have a `toISOString` function, we need to convert to the JS date first const cleanActualDate = typeof actualDate.toJSDate === 'function' ? actualDate.toJSDate() : actualDate; - const cleanExpectedDate = - typeof expectedDate.toJSDate === 'function' ? expectedDate.toJSDate() : expectedDate; + + let cleanExpectedDate; + if (typeof expectedDate === 'string') { + cleanExpectedDate = new Date(expectedDate); + } else if (typeof expectedDate.toJSDate === 'function') { + cleanExpectedDate = expectedDate.toJSDate(); + } else { + cleanExpectedDate = expectedDate; + } const assertion = new chai.Assertion(cleanActualDate.toISOString(), message); // TODO: Investigate if `as any` can be removed after https://github.com/DefinitelyTyped/DefinitelyTyped/issues/48634 is resolved. diff --git a/test/utils/pickers-utils.tsx b/test/utils/pickers-utils.tsx index 22f29ba0fca05..b1c32b10384d5 100644 --- a/test/utils/pickers-utils.tsx +++ b/test/utils/pickers-utils.tsx @@ -312,13 +312,14 @@ export const getPickerDay = (name: string, picker = 'January 2018') => getByRole(screen.getByText(picker)?.parentElement?.parentElement!, 'gridcell', { name }); export const cleanText = (text, specialCase?: 'singleDigit' | 'RTL') => { + const clean = text.replace(/\u202f/g, ' '); switch (specialCase) { case 'singleDigit': - return text.replace(/\u200e/g, ''); + return clean.replace(/\u200e/g, ''); case 'RTL': - return text.replace(/\u2066|\u2067|\u2068|\u2069/g, ''); + return clean.replace(/\u2066|\u2067|\u2068|\u2069/g, ''); default: - return text; + return clean; } }; diff --git a/yarn.lock b/yarn.lock index 7806c2ce6aea7..583d3fba08c9e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1846,7 +1846,7 @@ "@mui/monorepo@https://github.com/mui/material-ui.git#master": version "5.12.3" - resolved "https://github.com/mui/material-ui.git#9710ff34b16a0f271107a8619a28546ae3ca7c18" + resolved "https://github.com/mui/material-ui.git#24ce885b274aa3163b1d81162b18f1330d22d76e" "@mui/private-theming@^5.12.3": version "5.12.3"