diff --git a/codecov.yml b/codecov.yml index 426667acdf10..1a350cc7a0e5 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,4 +1,6 @@ coverage: + ignore: + - '**/*.test.tsx' status: project: default: @@ -16,8 +18,6 @@ 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/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx index 130c14f28821..6f8301a14602 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.test.tsx @@ -19,6 +19,14 @@ describe('', () => { }); describe('Adapter localization', () => { + describe('Default locale', () => { + const adapter = new AdapterDateFns(); + + it('getCurrentLocaleCode: should return locale code', () => { + expect(adapter.getCurrentLocaleCode()).to.equal('en-US'); + }); + }); + describe('English', () => { const adapter = new AdapterDateFns({ locale: enUS }); const date = adapter.date(TEST_DATE_ISO_STRING)!; diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts index 9bc30bf3dddf..414637955b35 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts @@ -85,13 +85,16 @@ export class AdapterDateFns implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - if (typeof addDays !== 'function') { - throw new Error( - [ - 'MUI: The `date-fns` package v3.x is not compatible with this adapter.', - 'Please, install v2.x of the package or use the `AdapterDateFnsV3` instead.', - ].join('\n'), - ); + /* istanbul ignore next */ + if (process.env.NODE_ENV !== 'production') { + if (typeof addDays !== 'function') { + throw new Error( + [ + 'MUI: The `date-fns` package v3.x is not compatible with this adapter.', + 'Please, install v2.x of the package or use the `AdapterDateFnsV3` instead.', + ].join('\n'), + ); + } } super({ locale: locale ?? defaultLocale, formats, longFormatters }); } diff --git a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts index 5cd1018dd540..32f9f873d0aa 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts @@ -10,9 +10,9 @@ import { MakeRequired } from '../internals/models/helpers'; type DateFnsLocaleBase = { formatLong?: { - date?: any; - time?: any; - dateTime?: any; + date: (...args: Array) => any; + time: (...args: Array) => any; + dateTime: (...args: Array) => any; }; code?: string; }; @@ -115,7 +115,10 @@ type DateFnsAdapterBaseOptions = MakeRe AdapterOptions, 'locale' > & { - longFormatters: Record<'p' | 'P', (token: string, formatLong: any) => string>; + longFormatters: Record< + 'p' | 'P', + (token: string, formatLong: DateFnsLocaleBase['formatLong']) => string + >; lib?: string; }; @@ -212,19 +215,15 @@ export class AdapterDateFnsBase return value; }; - public getCurrentLocaleCode = () => { - return this.locale?.code || 'en-US'; + public getCurrentLocaleCode = (): string => { + // `code` is undefined only in `date-fns` types, but all locales have it + return this.locale.code!; }; // Note: date-fns input types are more lenient than this adapter, so we need to expose our more // strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage. public is12HourCycleInCurrentLocale = () => { - if (this.locale) { - return /a/.test(this.locale.formatLong!.time({ width: 'short' })); - } - - // By default, date-fns is using en-US locale with am/pm enabled - return true; + return /a/.test(this.locale.formatLong!.time({ width: 'short' })); }; public expandFormat = (format: string) => { diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx index 916be6d43e29..b88d9390fced 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.test.tsx @@ -16,6 +16,12 @@ describe('', () => { describeJalaliAdapter(AdapterDateFnsJalali, {}); describe('Adapter localization', () => { + it('getCurrentLocaleCode: should return locale code', () => { + const adapter = new AdapterDateFnsJalali({ locale: enUS }); + + expect(adapter.getCurrentLocaleCode()).to.equal('en-US'); + }); + it('Formatting', () => { const adapter = new AdapterDateFnsJalali(); diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index 99bd27d11cf1..6fc2d35761f9 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -126,13 +126,16 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - if (typeof addDays !== 'function') { - throw new Error( - [ - 'MUI: The `date-fns-jalali` package v3.x is not compatible with this adapter.', - 'Please, install v2.x of the package or use the `AdapterDateFnsJalaliV3` instead.', - ].join('\n'), - ); + /* istanbul ignore next */ + if (process.env.NODE_ENV !== 'production') { + if (typeof addDays !== 'function') { + throw new Error( + [ + 'MUI: The `date-fns-jalali` package v3.x is not compatible with this adapter.', + 'Please, install v2.x of the package or use the `AdapterDateFnsJalaliV3` instead.', + ].join('\n'), + ); + } } super({ locale: locale ?? defaultLocale, @@ -152,10 +155,6 @@ export class AdapterDateFnsJalali return dateFnsParse(value, format, new Date(), { locale: this.locale }); }; - public getCurrentLocaleCode = (): string => { - return this.locale?.code || 'fa-IR'; - }; - public isValid = (value: Date | null): boolean => { if (value == null) { return false; diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts index 2a74aa3c39bf..1dc566b74605 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts @@ -127,18 +127,21 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - if (typeof addDays !== 'function') { - throw new Error( - [ - `MUI: The \`date-fns-jalali\` package v2.x is not compatible with this adapter.`, - 'Please, install v3.x of the package or use the `AdapterDateFnsJalali` instead.', - ].join('\n'), - ); - } - if (!longFormatters) { - throw new Error( - 'MUI: The minimum supported `date-fns-jalali` package version compatible with this adapter is `3.2.x`.', - ); + /* istanbul ignore next */ + if (process.env.NODE_ENV !== 'production') { + if (typeof addDays !== 'function') { + throw new Error( + [ + `MUI: The \`date-fns-jalali\` package v2.x is not compatible with this adapter.`, + 'Please, install v3.x of the package or use the `AdapterDateFnsJalali` instead.', + ].join('\n'), + ); + } + if (!longFormatters) { + throw new Error( + 'MUI: The minimum supported `date-fns-jalali` package version compatible with this adapter is `3.2.x`.', + ); + } } super({ locale: locale ?? defaultLocale, @@ -159,10 +162,6 @@ export class AdapterDateFnsJalali return dateFnsParse(value, format, new Date(), { locale: this.locale }); }; - public getCurrentLocaleCode = (): string => { - return this.locale?.code || 'fa-IR'; - }; - public isValid = (value: Date | null): boolean => { if (value == null) { return false; diff --git a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts index a47b56c227d4..9c5332b9e1fe 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts @@ -87,18 +87,21 @@ export class AdapterDateFns implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - if (typeof addDays !== 'function') { - throw new Error( - [ - `MUI: The \`date-fns\` package v2.x is not compatible with this adapter.`, - 'Please, install v3.x of the package or use the `AdapterDateFns` instead.', - ].join('\n'), - ); - } - if (!longFormatters) { - throw new Error( - 'MUI: The minimum supported `date-fns` package version compatible with this adapter is `3.2.x`.', - ); + /* istanbul ignore next */ + if (process.env.NODE_ENV !== 'production') { + if (typeof addDays !== 'function') { + throw new Error( + [ + `MUI: The \`date-fns\` package v2.x is not compatible with this adapter.`, + 'Please, install v3.x of the package or use the `AdapterDateFns` instead.', + ].join('\n'), + ); + } + if (!longFormatters) { + throw new Error( + 'MUI: The minimum supported `date-fns` package version compatible with this adapter is `3.2.x`.', + ); + } } super({ locale: locale ?? enUS, formats, longFormatters }); } diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx index 6c9db0b98d11..dda1f5c56ab9 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx @@ -39,6 +39,17 @@ describe('', () => { }, }); + describe('Adapter timezone', () => { + it('setTimezone: should throw warning if no plugin is available', () => { + const modifiedAdapter = new AdapterDayjs(); + // @ts-ignore + modifiedAdapter.hasTimezonePlugin = () => false; + + const date = modifiedAdapter.date(TEST_DATE_ISO_STRING)!; + expect(() => modifiedAdapter.setTimezone(date, 'Europe/London')).to.throw(); + }); + }); + describe('Adapter localization', () => { describe('English', () => { const adapter = new AdapterDayjs({ locale: 'en' }); diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index 0f76d85ad304..1245f256ea80 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -248,6 +248,7 @@ export class AdapterDayjs implements MuiPickersAdapter { let localeObject = locales[locale]; if (localeObject === undefined) { + /* istanbul ignore next */ if (process.env.NODE_ENV !== 'production') { warnOnce([ 'MUI X: Your locale has not been found.', @@ -277,7 +278,7 @@ export class AdapterDayjs implements MuiPickersAdapter { if (timezone !== 'UTC') { const fixedValue = value.tz(this.cleanTimezone(timezone), true); // @ts-ignore - if ((fixedValue.$offset ?? 0) === (value.$offset ?? 0)) { + if (fixedValue.$offset === (value.$offset ?? 0)) { return value; } // Change only what is needed to avoid creating a new object with unwanted data diff --git a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts index 066a0ab572b6..3fd21f704315 100644 --- a/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts +++ b/packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts @@ -485,6 +485,7 @@ export class AdapterLuxon implements MuiPickersAdapter { }; public getWeekNumber = (value: DateTime) => { + /* istanbul ignore next */ return value.localWeekNumber ?? value.weekNumber; }; diff --git a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts index 497cd0044f1c..435022269f28 100644 --- a/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts +++ b/packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts @@ -228,6 +228,10 @@ export class AdapterMomentJalaali return value.jDate(); }; + public getDaysInMonth = (value: Moment) => { + return this.moment.jDaysInMonth(value.jYear(), value.jMonth()); + }; + public setYear = (value: Moment, year: number) => { return value.clone().jYear(year); }; diff --git a/test/utils/pickers/describeGregorianAdapter/testCalculations.ts b/test/utils/pickers/describeGregorianAdapter/testCalculations.ts index 90b8f60581e9..10ef8845ec08 100644 --- a/test/utils/pickers/describeGregorianAdapter/testCalculations.ts +++ b/test/utils/pickers/describeGregorianAdapter/testCalculations.ts @@ -814,6 +814,10 @@ export const testCalculations: DescribeGregorianAdapterTestSuite = ({ expect(adapter.getDaysInMonth(adapter.addMonths(testDateIso, 1))).to.equal(30); }); + it('Method: getDayOfWeek', () => { + expect(adapter.getDayOfWeek(testDateIso)).to.equal(adapter.lib === 'luxon' ? 2 : 3); + }); + describe('Method: getWeekArray', () => { it('should work without timezones', () => { const weekArray = adapter.getWeekArray(testDateIso); @@ -866,7 +870,7 @@ export const testCalculations: DescribeGregorianAdapterTestSuite = ({ }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDateIso)).to.equal(44); + expect(adapter.getWeekNumber(testDateIso)).to.equal(44); }); it('Method: getYearRange', () => { diff --git a/test/utils/pickers/describeHijriAdapter/testCalculations.ts b/test/utils/pickers/describeHijriAdapter/testCalculations.ts index f6346f26adbf..5a797868ff06 100644 --- a/test/utils/pickers/describeHijriAdapter/testCalculations.ts +++ b/test/utils/pickers/describeHijriAdapter/testCalculations.ts @@ -161,7 +161,7 @@ export const testCalculations: DescribeHijriAdapterTestSuite = ({ adapter }) => }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDateIso)).to.equal(8); + expect(adapter.getWeekNumber(testDateIso)).to.equal(8); }); describe('Method: getYearRange', () => { diff --git a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts index 6fb225977846..c1c2d7e9f4ea 100644 --- a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts +++ b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts @@ -19,6 +19,19 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => expect(adapter.isEqual(testDateIso, anotherDate)).to.equal(true); expect(adapter.isEqual(null, null)).to.equal(true); + expect(adapter.isEqual(testDateIso, null)).to.equal(false); + }); + + it('Method: isValid', () => { + expect(adapter.isValid(testDateIso)).to.equal(true); + expect(adapter.isValid(null)).to.equal(false); + if (adapter.lib !== 'moment-jalaali') { + expect(adapter.isValid(adapter.date('invalid'))).to.equal(false); + } else { + expect(() => adapter.isValid(adapter.date('invalid'))).toWarnDev( + 'Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not rel', + ); + } }); it('Method: isSameYear', () => { @@ -202,6 +215,10 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => expect(adapter.getDate(testDateIso)).to.equal(8); }); + it('Method: getHours', () => { + expect(adapter.getHours(testDateIso)).to.equal(11); + }); + it('Method: getMinutes', () => { expect(adapter.getMinutes(testDateIso)).to.equal(44); }); @@ -242,6 +259,10 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => expect(adapter.setMilliseconds(testDateIso, 11)).toEqualDateTime('2018-10-30T11:44:00.011Z'); }); + it('Method: getDaysInMonth', () => { + expect(adapter.getDaysInMonth(testDateIso)).to.equal(30); + }); + it('Method: getWeekArray', () => { const weekArray = adapter.getWeekArray(testDateIso); const expectedDate = new Date('2018-10-20T00:00:00.000Z'); @@ -255,7 +276,7 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => }); it('Method: getWeekNumber', () => { - expect(adapter.getWeekNumber!(testDateIso)).to.equal(33); + expect(adapter.getWeekNumber(testDateIso)).to.equal(33); }); it('Method: getYearRange', () => {