From 6c41ad3add1d15408f552bd3f2157af9f29526c9 Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 24 Jul 2024 15:18:08 +0200 Subject: [PATCH 01/18] [test] Fix adapters code coverage --- codecov.yml | 4 ++-- .../src/AdapterDateFns/AdapterDateFns.test.tsx | 8 ++++++++ .../AdapterDateFnsJalali.test.tsx | 6 ++++++ .../testCalculations.ts | 4 ++++ .../describeJalaliAdapter/testCalculations.ts | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) 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/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/test/utils/pickers/describeGregorianAdapter/testCalculations.ts b/test/utils/pickers/describeGregorianAdapter/testCalculations.ts index 90b8f60581e9..7eade4fda089 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); diff --git a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts index 6fb225977846..ff8c48ad296e 100644 --- a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts +++ b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts @@ -19,6 +19,16 @@ 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); + if (adapter.lib !== 'moment-jalaali') { + // moment-jalaali just throw errors + expect(adapter.isValid(null)).to.equal(false); + expect(adapter.isValid(adapter.date('invalid'))).to.equal(false); + } }); it('Method: isSameYear', () => { @@ -202,6 +212,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 +256,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(31); + }); + it('Method: getWeekArray', () => { const weekArray = adapter.getWeekArray(testDateIso); const expectedDate = new Date('2018-10-20T00:00:00.000Z'); From 09f686380ad38eb43f1e3caeb746ca38d844f388 Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 24 Jul 2024 15:44:10 +0200 Subject: [PATCH 02/18] fix moment-jalaali --- .../src/AdapterMomentJalaali/AdapterMomentJalaali.ts | 4 ++++ test/utils/pickers/describeJalaliAdapter/testCalculations.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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/describeJalaliAdapter/testCalculations.ts b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts index ff8c48ad296e..739b3bb806da 100644 --- a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts +++ b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts @@ -257,7 +257,7 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => }); it('Method: getDaysInMonth', () => { - expect(adapter.getDaysInMonth(testDateIso)).to.equal(31); + expect(adapter.getDaysInMonth(testDateIso)).to.equal(30); }); it('Method: getWeekArray', () => { From ac744d066eaff8453c6ca8d79c16065982b485cc Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 24 Jul 2024 17:17:23 +0200 Subject: [PATCH 03/18] wip --- .../src/AdapterDayjs/AdapterDayjs.test.tsx | 11 +++++++++++ .../describeGregorianAdapter/testCalculations.ts | 2 +- .../pickers/describeHijriAdapter/testCalculations.ts | 2 +- .../pickers/describeJalaliAdapter/testCalculations.ts | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx index 6c9db0b98d11..80c63c5d77d1 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 pluggin 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/test/utils/pickers/describeGregorianAdapter/testCalculations.ts b/test/utils/pickers/describeGregorianAdapter/testCalculations.ts index 7eade4fda089..10ef8845ec08 100644 --- a/test/utils/pickers/describeGregorianAdapter/testCalculations.ts +++ b/test/utils/pickers/describeGregorianAdapter/testCalculations.ts @@ -870,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 739b3bb806da..16a76ec9930f 100644 --- a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts +++ b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts @@ -273,7 +273,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', () => { From e88e7a76a4a3013bf6e93595ce584724a347a290 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 12:16:41 +0300 Subject: [PATCH 04/18] Improve typing and remove redundant `AdapterDateFnsBase` code --- .../AdapterDateFnsBase/AdapterDateFnsBase.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts index 5cd1018dd540..af69101f4c83 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; }; @@ -213,18 +216,13 @@ export class AdapterDateFnsBase }; public getCurrentLocaleCode = () => { - return this.locale?.code || 'en-US'; + return this.locale.code || 'en-US'; }; // 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) => { From cd2412fa18206128ec2a85a175acda1c7bd44128 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 12:32:23 +0300 Subject: [PATCH 05/18] Do not handle `locale` without `code` --- .../src/AdapterDateFnsBase/AdapterDateFnsBase.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts index af69101f4c83..c56e2dfbcb62 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts @@ -216,7 +216,8 @@ export class AdapterDateFnsBase }; public getCurrentLocaleCode = () => { - return this.locale.code || 'en-US'; + // `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 From 1f71c9e1099563bf6648aac5082fdb3bdcf19992 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 12:48:41 +0300 Subject: [PATCH 06/18] Explicit `getCurrentLocaleCode` for jalali are no longer necessary --- .../src/AdapterDateFnsBase/AdapterDateFnsBase.ts | 4 ++-- .../src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts | 4 ---- .../src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts | 4 ---- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts index c56e2dfbcb62..32f9f873d0aa 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts @@ -215,8 +215,8 @@ export class AdapterDateFnsBase return value; }; - public getCurrentLocaleCode = () => { - // `code` is undefined only in `date-fns` types, but all locales have it + public getCurrentLocaleCode = (): string => { + // `code` is undefined only in `date-fns` types, but all locales have it return this.locale.code!; }; diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index 99bd27d11cf1..582fb351cfc5 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -152,10 +152,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..acfe1d02b44d 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts @@ -159,10 +159,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; From 05eeef1b51fe6fad0c620fa163e4fd2daedbe035 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 13:16:18 +0300 Subject: [PATCH 07/18] Try ignoring `date-fns` v2 and v3 major compat checks --- codecov.yml | 2 ++ .../src/AdapterDateFns/AdapterDateFns.ts | 10 ++---- .../AdapterDateFnsJalali.ts | 10 ++---- .../AdapterDateFnsJalaliV3.ts | 15 ++------ .../src/AdapterDateFnsV3/AdapterDateFnsV3.ts | 15 ++------ .../src/internals/utils/assertDateAdapters.ts | 35 +++++++++++++++++++ 6 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts diff --git a/codecov.yml b/codecov.yml index 1a350cc7a0e5..466eb85e1e56 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,6 +1,8 @@ coverage: ignore: - '**/*.test.tsx' + # ignore date adapter parts that are hardly testable + - '**/assertDateAdapters.ts' status: project: default: diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts index 9bc30bf3dddf..a67f6afba696 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts @@ -46,6 +46,7 @@ import defaultLocale from 'date-fns/locale/en-US'; import longFormatters from 'date-fns/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; +import { assertDateFnsAdapter } from '../internals/utils/assertDateAdapters'; type DateFnsLocale = typeof defaultLocale; @@ -85,14 +86,7 @@ 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'), - ); - } + assertDateFnsAdapter(addDays); super({ locale: locale ?? defaultLocale, formats, longFormatters }); } diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index 582fb351cfc5..7add9e9ec4b7 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -46,6 +46,7 @@ import defaultLocale from 'date-fns-jalali/locale/fa-IR'; import longFormatters from 'date-fns-jalali/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; +import { assertDateFnsAdapter } from '../internals/utils/assertDateAdapters'; type DateFnsLocale = typeof defaultLocale; @@ -126,14 +127,7 @@ 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'), - ); - } + assertDateFnsAdapter(addDays, 'date-fns-jalali', 'AdapterDateFnsJalaliV3'); super({ locale: locale ?? defaultLocale, // some formats are different in jalali adapter, diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts index acfe1d02b44d..ecfb10e78d45 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts @@ -49,6 +49,7 @@ import { faIR as defaultLocale } from 'date-fns-jalali/locale/fa-IR'; import { Locale as DateFnsLocale } from 'date-fns-jalali/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; +import { assertDateFnsAdapterV3 } from '../internals/utils/assertDateAdapters'; const defaultFormats: AdapterFormats = { year: 'yyyy', @@ -127,19 +128,7 @@ 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`.', - ); - } + assertDateFnsAdapterV3(addDays, longFormatters, 'date-fns-jalali', 'AdapterDateFnsJalali'); super({ locale: locale ?? defaultLocale, // some formats are different in jalali adapter, diff --git a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts index a47b56c227d4..a059a45f06ea 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts @@ -50,6 +50,7 @@ import { enUS } from 'date-fns/locale/en-US'; import { Locale as DateFnsLocale } from 'date-fns/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; +import { assertDateFnsAdapterV3 } from '../internals/utils/assertDateAdapters'; declare module '@mui/x-date-pickers/models' { interface PickerValidDateLookup { @@ -87,19 +88,7 @@ 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`.', - ); - } + assertDateFnsAdapterV3(addDays, longFormatters); super({ locale: locale ?? enUS, formats, longFormatters }); } diff --git a/packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts b/packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts new file mode 100644 index 000000000000..16cd152d0ead --- /dev/null +++ b/packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts @@ -0,0 +1,35 @@ +export const assertDateFnsAdapter = ( + addDays: any, + lib = 'date-fns', + packageName = 'AdapterDateFnsV3', +) => { + if (typeof addDays !== 'function') { + throw new Error( + [ + `MUI: The \`${lib}\` package v3.x is not compatible with this adapter.`, + `Please, install v2.x of the package or use the \`${packageName}\` instead.`, + ].join('\n'), + ); + } +}; + +export const assertDateFnsAdapterV3 = ( + addDays: any, + longFormatters: any, + lib = 'date-fns', + packageName = 'AdapterDateFns', +) => { + if (typeof addDays !== 'function') { + throw new Error( + [ + `MUI: The \`${lib}\` package v2.x is not compatible with this adapter.`, + `Please, install v3.x of the package or use the \`${packageName}\` instead.`, + ].join('\n'), + ); + } + if (!longFormatters) { + throw new Error( + `MUI: The minimum supported \`${lib}\` package version compatible with this adapter is \`3.2.x\`.`, + ); + } +}; From 1f4888c74e31ce9a330bb1be14912a8347d309ce Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 13:49:11 +0300 Subject: [PATCH 08/18] Rename adapter util file and move `AdapterDayjs` undefined locale warning --- codecov.yml | 2 +- .../src/AdapterDateFns/AdapterDateFns.ts | 2 +- .../AdapterDateFnsJalali/AdapterDateFnsJalali.ts | 2 +- .../AdapterDateFnsJalaliV3.ts | 2 +- .../src/AdapterDateFnsV3/AdapterDateFnsV3.ts | 2 +- .../x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts | 11 ++--------- .../utils/{assertDateAdapters.ts => adapters.ts} | 13 +++++++++++++ 7 files changed, 20 insertions(+), 14 deletions(-) rename packages/x-date-pickers/src/internals/utils/{assertDateAdapters.ts => adapters.ts} (66%) diff --git a/codecov.yml b/codecov.yml index 466eb85e1e56..3fdc7b6c9684 100644 --- a/codecov.yml +++ b/codecov.yml @@ -2,7 +2,7 @@ coverage: ignore: - '**/*.test.tsx' # ignore date adapter parts that are hardly testable - - '**/assertDateAdapters.ts' + - '**/internals/utils/adapters.ts' status: project: default: diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts index a67f6afba696..10d6ef410b81 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts @@ -46,7 +46,7 @@ import defaultLocale from 'date-fns/locale/en-US'; import longFormatters from 'date-fns/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapter } from '../internals/utils/assertDateAdapters'; +import { assertDateFnsAdapter } from '../internals/utils/adapters'; type DateFnsLocale = typeof defaultLocale; diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index 7add9e9ec4b7..c94fb61b7562 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -46,7 +46,7 @@ import defaultLocale from 'date-fns-jalali/locale/fa-IR'; import longFormatters from 'date-fns-jalali/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapter } from '../internals/utils/assertDateAdapters'; +import { assertDateFnsAdapter } from '../internals/utils/adapters'; type DateFnsLocale = typeof defaultLocale; diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts index ecfb10e78d45..fe15f8c1b7f2 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts @@ -49,7 +49,7 @@ import { faIR as defaultLocale } from 'date-fns-jalali/locale/fa-IR'; import { Locale as DateFnsLocale } from 'date-fns-jalali/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapterV3 } from '../internals/utils/assertDateAdapters'; +import { assertDateFnsAdapterV3 } from '../internals/utils/adapters'; const defaultFormats: AdapterFormats = { year: 'yyyy', diff --git a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts index a059a45f06ea..5ed5b6c7e932 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts @@ -50,7 +50,7 @@ import { enUS } from 'date-fns/locale/en-US'; import { Locale as DateFnsLocale } from 'date-fns/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapterV3 } from '../internals/utils/assertDateAdapters'; +import { assertDateFnsAdapterV3 } from '../internals/utils/adapters'; declare module '@mui/x-date-pickers/models' { interface PickerValidDateLookup { diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index 0f76d85ad304..15d732c02903 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -13,7 +13,7 @@ import { PickersTimezone, DateBuilderReturnType, } from '../models'; -import { warnOnce } from '../internals/utils/warning'; +import { warnUndefinedDayjsLocale } from '../internals/utils/adapters'; defaultDayjs.extend(localizedFormatPlugin); defaultDayjs.extend(weekOfYearPlugin); @@ -248,14 +248,7 @@ export class AdapterDayjs implements MuiPickersAdapter { let localeObject = locales[locale]; if (localeObject === undefined) { - if (process.env.NODE_ENV !== 'production') { - warnOnce([ - 'MUI X: Your locale has not been found.', - 'Either the locale key is not a supported one. Locales supported by dayjs are available here: https://github.com/iamkun/dayjs/tree/dev/src/locale.', - "Or you forget to import the locale from 'dayjs/locale/{localeUsed}'", - 'fallback on English locale.', - ]); - } + warnUndefinedDayjsLocale(); localeObject = locales.en; } diff --git a/packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts b/packages/x-date-pickers/src/internals/utils/adapters.ts similarity index 66% rename from packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts rename to packages/x-date-pickers/src/internals/utils/adapters.ts index 16cd152d0ead..3e56ca104b43 100644 --- a/packages/x-date-pickers/src/internals/utils/assertDateAdapters.ts +++ b/packages/x-date-pickers/src/internals/utils/adapters.ts @@ -1,3 +1,5 @@ +import { warnOnce } from './warning'; + export const assertDateFnsAdapter = ( addDays: any, lib = 'date-fns', @@ -33,3 +35,14 @@ export const assertDateFnsAdapterV3 = ( ); } }; + +export const warnUndefinedDayjsLocale = () => { + if (process.env.NODE_ENV !== 'production') { + warnOnce([ + 'MUI X: Your locale has not been found.', + 'Either the locale key is not a supported one. Locales supported by dayjs are available here: https://github.com/iamkun/dayjs/tree/dev/src/locale.', + "Or you forget to import the locale from 'dayjs/locale/{localeUsed}'", + 'fallback on English locale.', + ]); + } +}; From b729ca3c536bcc6e107a441e66fa25bdb8e06404 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 14:36:56 +0300 Subject: [PATCH 09/18] Fix typo --- packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx index 80c63c5d77d1..dda1f5c56ab9 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx @@ -40,7 +40,7 @@ describe('', () => { }); describe('Adapter timezone', () => { - it('setTimezone: should throw warning if no pluggin is available', () => { + it('setTimezone: should throw warning if no plugin is available', () => { const modifiedAdapter = new AdapterDayjs(); // @ts-ignore modifiedAdapter.hasTimezonePlugin = () => false; From 4b6aa9064c5d84bd5c8140fb8d09b205245970eb Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 14:38:15 +0300 Subject: [PATCH 10/18] Slightly reduce the adapters codecov threshold --- codecov.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codecov.yml b/codecov.yml index 3fdc7b6c9684..59afb5419eb0 100644 --- a/codecov.yml +++ b/codecov.yml @@ -9,7 +9,8 @@ coverage: target: auto threshold: 1% adapters: - target: 100% + # leaving some slack for untestable parts + target: 99.5% paths: - 'packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts' - 'packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts' From 46d0e57f95f382309a7453bb6f58a52489774de3 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 15:03:24 +0300 Subject: [PATCH 11/18] Bump monorepo to test if it makes the `docs:build` pass --- package.json | 2 +- pnpm-lock.yaml | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e99f8d204cf6..a6864fa5c1ac 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "@mui/internal-markdown": "^1.0.7", "@mui/internal-test-utils": "^1.0.4", "@mui/material": "^5.16.2", - "@mui/monorepo": "github:mui/material-ui#288863bd2f8681a82c4bfbaf13215b41043bc551", + "@mui/monorepo": "github:mui/material-ui#75041b7bb6e48f1782c147fcbb2ed517baaa6548", "@mui/utils": "^5.16.2", "@next/eslint-plugin-next": "14.2.5", "@octokit/plugin-retry": "^7.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d92db6e480d..ab93e3b85f3f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -96,8 +96,8 @@ importers: specifier: ^5.16.2 version: 5.16.2(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@emotion/styled@11.11.5(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@mui/monorepo': - specifier: github:mui/material-ui#288863bd2f8681a82c4bfbaf13215b41043bc551 - version: https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551(encoding@0.1.13) + specifier: github:mui/material-ui#75041b7bb6e48f1782c147fcbb2ed517baaa6548 + version: https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548(encoding@0.1.13) '@mui/utils': specifier: ^5.16.2 version: 5.16.2(@types/react@18.3.3)(react@18.3.1) @@ -2900,9 +2900,9 @@ packages: '@types/react': optional: true - '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551': - resolution: {tarball: https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551} - version: 6.0.0-beta.2 + '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548': + resolution: {tarball: https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548} + version: 6.0.0-beta.3 engines: {pnpm: 9.5.0} '@mui/private-theming@5.16.2': @@ -11165,7 +11165,7 @@ snapshots: '@emotion/styled': 11.11.5(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react@18.3.1) '@types/react': 18.3.3 - '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551(encoding@0.1.13)': + '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548(encoding@0.1.13)': dependencies: '@googleapis/sheets': 8.0.0(encoding@0.1.13) '@netlify/functions': 2.8.1 From e447a4dc5a2dfe131adf05b3aaf8fce0912183ac Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 15:11:34 +0300 Subject: [PATCH 12/18] Revert monorepo bump --- package.json | 2 +- pnpm-lock.yaml | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index ac5ab66c7349..f3a68f48872f 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "@mui/internal-markdown": "^1.0.7", "@mui/internal-test-utils": "^1.0.4", "@mui/material": "^5.16.5", - "@mui/monorepo": "github:mui/material-ui#75041b7bb6e48f1782c147fcbb2ed517baaa6548", + "@mui/monorepo": "github:mui/material-ui#288863bd2f8681a82c4bfbaf13215b41043bc551", "@mui/utils": "^5.16.5", "@next/eslint-plugin-next": "14.2.5", "@octokit/plugin-retry": "^7.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c0e2e60f95e..8dd28fd44e47 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -96,8 +96,8 @@ importers: specifier: ^5.16.5 version: 5.16.5(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@emotion/styled@11.11.5(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@mui/monorepo': - specifier: github:mui/material-ui#75041b7bb6e48f1782c147fcbb2ed517baaa6548 - version: https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548(encoding@0.1.13) + specifier: github:mui/material-ui#288863bd2f8681a82c4bfbaf13215b41043bc551 + version: https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551(encoding@0.1.13) '@mui/utils': specifier: ^5.16.5 version: 5.16.5(@types/react@18.3.3)(react@18.3.1) @@ -2900,9 +2900,9 @@ packages: '@types/react': optional: true - '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548': - resolution: {tarball: https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548} - version: 6.0.0-beta.3 + '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551': + resolution: {tarball: https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551} + version: 6.0.0-beta.2 engines: {pnpm: 9.5.0} '@mui/private-theming@5.16.5': @@ -11166,7 +11166,7 @@ snapshots: '@emotion/styled': 11.11.5(@emotion/react@11.11.4(@types/react@18.3.3)(react@18.3.1))(@types/react@18.3.3)(react@18.3.1) '@types/react': 18.3.3 - '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/75041b7bb6e48f1782c147fcbb2ed517baaa6548(encoding@0.1.13)': + '@mui/monorepo@https://codeload.github.com/mui/material-ui/tar.gz/288863bd2f8681a82c4bfbaf13215b41043bc551(encoding@0.1.13)': dependencies: '@googleapis/sheets': 8.0.0(encoding@0.1.13) '@netlify/functions': 2.8.1 From 2e74d24e32f9b585b2fa19780e0824fba7491adc Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 16:39:05 +0300 Subject: [PATCH 13/18] Revert to `100%` coverage and istanbul ignore what we don't cover --- codecov.yml | 3 +-- .../x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts | 12 ++++++++++-- .../x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts | 1 + .../x-date-pickers/src/internals/utils/adapters.ts | 13 ------------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/codecov.yml b/codecov.yml index 59afb5419eb0..3fdc7b6c9684 100644 --- a/codecov.yml +++ b/codecov.yml @@ -9,8 +9,7 @@ coverage: target: auto threshold: 1% adapters: - # leaving some slack for untestable parts - target: 99.5% + target: 100% paths: - 'packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts' - 'packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts' diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index 15d732c02903..47794c990b0b 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -13,7 +13,7 @@ import { PickersTimezone, DateBuilderReturnType, } from '../models'; -import { warnUndefinedDayjsLocale } from '../internals/utils/adapters'; +import { warnOnce } from '../internals/utils/warning'; defaultDayjs.extend(localizedFormatPlugin); defaultDayjs.extend(weekOfYearPlugin); @@ -248,7 +248,15 @@ export class AdapterDayjs implements MuiPickersAdapter { let localeObject = locales[locale]; if (localeObject === undefined) { - warnUndefinedDayjsLocale(); + /* istanbul ignore next */ + if (process.env.NODE_ENV !== 'production') { + warnOnce([ + 'MUI X: Your locale has not been found.', + 'Either the locale key is not a supported one. Locales supported by dayjs are available here: https://github.com/iamkun/dayjs/tree/dev/src/locale.', + "Or you forget to import the locale from 'dayjs/locale/{localeUsed}'", + 'fallback on English locale.', + ]); + } localeObject = locales.en; } 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/internals/utils/adapters.ts b/packages/x-date-pickers/src/internals/utils/adapters.ts index 3e56ca104b43..16cd152d0ead 100644 --- a/packages/x-date-pickers/src/internals/utils/adapters.ts +++ b/packages/x-date-pickers/src/internals/utils/adapters.ts @@ -1,5 +1,3 @@ -import { warnOnce } from './warning'; - export const assertDateFnsAdapter = ( addDays: any, lib = 'date-fns', @@ -35,14 +33,3 @@ export const assertDateFnsAdapterV3 = ( ); } }; - -export const warnUndefinedDayjsLocale = () => { - if (process.env.NODE_ENV !== 'production') { - warnOnce([ - 'MUI X: Your locale has not been found.', - 'Either the locale key is not a supported one. Locales supported by dayjs are available here: https://github.com/iamkun/dayjs/tree/dev/src/locale.', - "Or you forget to import the locale from 'dayjs/locale/{localeUsed}'", - 'fallback on English locale.', - ]); - } -}; From 4ba77134c2d3f2925a14511f14df538ec0158840 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 16:57:59 +0300 Subject: [PATCH 14/18] Revert the moving of major version compat and add `istanbul ignore` - also wrap the checks in `process.env.NODE_ENV !== 'production'` to avoid bundling it --- .../src/AdapterDateFns/AdapterDateFns.ts | 13 +++++-- .../AdapterDateFnsJalali.ts | 13 +++++-- .../AdapterDateFnsJalaliV3.ts | 18 ++++++++-- .../src/AdapterDateFnsV3/AdapterDateFnsV3.ts | 18 ++++++++-- .../src/internals/utils/adapters.ts | 35 ------------------- 5 files changed, 54 insertions(+), 43 deletions(-) delete mode 100644 packages/x-date-pickers/src/internals/utils/adapters.ts diff --git a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts index 10d6ef410b81..414637955b35 100644 --- a/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts +++ b/packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts @@ -46,7 +46,6 @@ import defaultLocale from 'date-fns/locale/en-US'; import longFormatters from 'date-fns/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapter } from '../internals/utils/adapters'; type DateFnsLocale = typeof defaultLocale; @@ -86,7 +85,17 @@ export class AdapterDateFns implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - assertDateFnsAdapter(addDays); + /* 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/AdapterDateFnsJalali/AdapterDateFnsJalali.ts b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts index c94fb61b7562..6fc2d35761f9 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts @@ -46,7 +46,6 @@ import defaultLocale from 'date-fns-jalali/locale/fa-IR'; import longFormatters from 'date-fns-jalali/_lib/format/longFormatters'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapter } from '../internals/utils/adapters'; type DateFnsLocale = typeof defaultLocale; @@ -127,7 +126,17 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - assertDateFnsAdapter(addDays, 'date-fns-jalali', 'AdapterDateFnsJalaliV3'); + /* 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, // some formats are different in jalali adapter, diff --git a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts index fe15f8c1b7f2..1dc566b74605 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsJalaliV3/AdapterDateFnsJalaliV3.ts @@ -49,7 +49,6 @@ import { faIR as defaultLocale } from 'date-fns-jalali/locale/fa-IR'; import { Locale as DateFnsLocale } from 'date-fns-jalali/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapterV3 } from '../internals/utils/adapters'; const defaultFormats: AdapterFormats = { year: 'yyyy', @@ -128,7 +127,22 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - assertDateFnsAdapterV3(addDays, longFormatters, 'date-fns-jalali', 'AdapterDateFnsJalali'); + /* 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, // some formats are different in jalali adapter, diff --git a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts index 5ed5b6c7e932..9c5332b9e1fe 100644 --- a/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts +++ b/packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts @@ -50,7 +50,6 @@ import { enUS } from 'date-fns/locale/en-US'; import { Locale as DateFnsLocale } from 'date-fns/locale/types'; import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '../models'; import { AdapterDateFnsBase } from '../AdapterDateFnsBase'; -import { assertDateFnsAdapterV3 } from '../internals/utils/adapters'; declare module '@mui/x-date-pickers/models' { interface PickerValidDateLookup { @@ -88,7 +87,22 @@ export class AdapterDateFns implements MuiPickersAdapter { constructor({ locale, formats }: AdapterOptions = {}) { - assertDateFnsAdapterV3(addDays, longFormatters); + /* 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/internals/utils/adapters.ts b/packages/x-date-pickers/src/internals/utils/adapters.ts deleted file mode 100644 index 16cd152d0ead..000000000000 --- a/packages/x-date-pickers/src/internals/utils/adapters.ts +++ /dev/null @@ -1,35 +0,0 @@ -export const assertDateFnsAdapter = ( - addDays: any, - lib = 'date-fns', - packageName = 'AdapterDateFnsV3', -) => { - if (typeof addDays !== 'function') { - throw new Error( - [ - `MUI: The \`${lib}\` package v3.x is not compatible with this adapter.`, - `Please, install v2.x of the package or use the \`${packageName}\` instead.`, - ].join('\n'), - ); - } -}; - -export const assertDateFnsAdapterV3 = ( - addDays: any, - longFormatters: any, - lib = 'date-fns', - packageName = 'AdapterDateFns', -) => { - if (typeof addDays !== 'function') { - throw new Error( - [ - `MUI: The \`${lib}\` package v2.x is not compatible with this adapter.`, - `Please, install v3.x of the package or use the \`${packageName}\` instead.`, - ].join('\n'), - ); - } - if (!longFormatters) { - throw new Error( - `MUI: The minimum supported \`${lib}\` package version compatible with this adapter is \`3.2.x\`.`, - ); - } -}; From fa9b41ee7505fc853bff22e3cd75893fd097f897 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 16:58:28 +0300 Subject: [PATCH 15/18] Remove irrelevant ignore --- codecov.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/codecov.yml b/codecov.yml index 3fdc7b6c9684..1a350cc7a0e5 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,8 +1,6 @@ coverage: ignore: - '**/*.test.tsx' - # ignore date adapter parts that are hardly testable - - '**/internals/utils/adapters.ts' status: project: default: From 79e1bcf6f7b45f281d0d5b7a759a154db42b83c8 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 17:44:39 +0300 Subject: [PATCH 16/18] Improve `isValid` test for moment-jalaali --- .../pickers/describeJalaliAdapter/testCalculations.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts index 16a76ec9930f..c1c2d7e9f4ea 100644 --- a/test/utils/pickers/describeJalaliAdapter/testCalculations.ts +++ b/test/utils/pickers/describeJalaliAdapter/testCalculations.ts @@ -24,10 +24,13 @@ export const testCalculations: DescribeJalaliAdapterTestSuite = ({ adapter }) => it('Method: isValid', () => { expect(adapter.isValid(testDateIso)).to.equal(true); + expect(adapter.isValid(null)).to.equal(false); if (adapter.lib !== 'moment-jalaali') { - // moment-jalaali just throw errors - expect(adapter.isValid(null)).to.equal(false); 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', + ); } }); From 71559a0c51745ce89762465dbf0a219e9afdaae8 Mon Sep 17 00:00:00 2001 From: Lukas Date: Thu, 25 Jul 2024 18:14:39 +0300 Subject: [PATCH 17/18] Remove `?? 0` as $offset is always present with timezone plugin --- packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index 47794c990b0b..47d20104e789 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -278,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) { return value; } // Change only what is needed to avoid creating a new object with unwanted data From 181ff21f711322839823c8f3378f41632c5a09e4 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 26 Jul 2024 08:09:36 +0300 Subject: [PATCH 18/18] See if we need coverage for this case --- packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts index 47d20104e789..1245f256ea80 100644 --- a/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts +++ b/packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts @@ -278,7 +278,7 @@ export class AdapterDayjs implements MuiPickersAdapter { if (timezone !== 'UTC') { const fixedValue = value.tz(this.cleanTimezone(timezone), true); // @ts-ignore - if (fixedValue.$offset === value.$offset) { + if (fixedValue.$offset === (value.$offset ?? 0)) { return value; } // Change only what is needed to avoid creating a new object with unwanted data