Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Fix adapters code coverage #13969

Merged
merged 20 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6c41ad3
[test] Fix adapters code coverage
alexfauquette Jul 24, 2024
09f6863
fix moment-jalaali
alexfauquette Jul 24, 2024
ac744d0
wip
alexfauquette Jul 24, 2024
e88e7a7
Improve typing and remove redundant `AdapterDateFnsBase` code
LukasTy Jul 25, 2024
cd2412f
Do not handle `locale` without `code`
LukasTy Jul 25, 2024
1f71c9e
Explicit `getCurrentLocaleCode` for jalali are no longer necessary
LukasTy Jul 25, 2024
05eeef1
Try ignoring `date-fns` v2 and v3 major compat checks
LukasTy Jul 25, 2024
a5a4856
Merge remote-tracking branch 'upstream/master' into fix-codecov
LukasTy Jul 25, 2024
1f4888c
Rename adapter util file and move `AdapterDayjs` undefined locale war…
LukasTy Jul 25, 2024
b729ca3
Fix typo
LukasTy Jul 25, 2024
4b6aa90
Slightly reduce the adapters codecov threshold
LukasTy Jul 25, 2024
46d0e57
Bump monorepo to test if it makes the `docs:build` pass
LukasTy Jul 25, 2024
d861429
Merge remote-tracking branch 'upstream/master' into fix-codecov
LukasTy Jul 25, 2024
e447a4d
Revert monorepo bump
LukasTy Jul 25, 2024
2e74d24
Revert to `100%` coverage and istanbul ignore what we don't cover
LukasTy Jul 25, 2024
4ba7713
Revert the moving of major version compat and add `istanbul ignore`
LukasTy Jul 25, 2024
fa9b41e
Remove irrelevant ignore
LukasTy Jul 25, 2024
79e1bcf
Improve `isValid` test for moment-jalaali
LukasTy Jul 25, 2024
71559a0
Remove `?? 0` as $offset is always present with timezone plugin
LukasTy Jul 25, 2024
181ff21
See if we need coverage for this case
LukasTy Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
coverage:
ignore:
- '**/*.test.tsx'
status:
project:
default:
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('<AdapterDateFns />', () => {
});

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)!;
Expand Down
17 changes: 10 additions & 7 deletions packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ export class AdapterDateFns
implements MuiPickersAdapter<Date, DateFnsLocale>
{
constructor({ locale, formats }: AdapterOptions<DateFnsLocale, never> = {}) {
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the previous changes and added this check to avoid bundling these warning in production.
Do you think it is worth it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the warnOnce, it's definitely worth it

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 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { MakeRequired } from '../internals/models/helpers';

type DateFnsLocaleBase = {
formatLong?: {
date?: any;
time?: any;
dateTime?: any;
date: (...args: Array<any>) => any;
time: (...args: Array<any>) => any;
dateTime: (...args: Array<any>) => any;
};
code?: string;
};
Expand Down Expand Up @@ -115,7 +115,10 @@ type DateFnsAdapterBaseOptions<DateFnsLocale extends DateFnsLocaleBase> = MakeRe
AdapterOptions<DateFnsLocale, never>,
'locale'
> & {
longFormatters: Record<'p' | 'P', (token: string, formatLong: any) => string>;
longFormatters: Record<
'p' | 'P',
(token: string, formatLong: DateFnsLocaleBase['formatLong']) => string
>;
lib?: string;
};

Expand Down Expand Up @@ -212,19 +215,15 @@ export class AdapterDateFnsBase<DateFnsLocale extends DateFnsLocaleBase>
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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ describe('<AdapterDateFnsJalali />', () => {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ export class AdapterDateFnsJalali
implements MuiPickersAdapter<Date, DateFnsLocale>
{
constructor({ locale, formats }: AdapterOptions<DateFnsLocale, never> = {}) {
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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,21 @@ export class AdapterDateFnsJalali
implements MuiPickersAdapter<Date, DateFnsLocale>
{
constructor({ locale, formats }: AdapterOptions<DateFnsLocale, never> = {}) {
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,
Expand All @@ -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;
Expand Down
27 changes: 15 additions & 12 deletions packages/x-date-pickers/src/AdapterDateFnsV3/AdapterDateFnsV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,21 @@ export class AdapterDateFns
implements MuiPickersAdapter<Date, DateFnsLocale>
{
constructor({ locale, formats }: AdapterOptions<DateFnsLocale, never> = {}) {
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 });
}
Expand Down
11 changes: 11 additions & 0 deletions packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('<AdapterDayjs />', () => {
},
});

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' });
Expand Down
3 changes: 2 additions & 1 deletion packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export class AdapterDayjs implements MuiPickersAdapter<Dayjs, string> {
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.',
Expand Down Expand Up @@ -277,7 +278,7 @@ export class AdapterDayjs implements MuiPickersAdapter<Dayjs, string> {
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
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ export class AdapterLuxon implements MuiPickersAdapter<DateTime, string> {
};

public getWeekNumber = (value: DateTime) => {
/* istanbul ignore next */
return value.localWeekNumber ?? value.weekNumber;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ export class AdapterMomentJalaali
return value.jDate();
};

public getDaysInMonth = (value: Moment) => {
return this.moment.jDaysInMonth(value.jYear(), value.jMonth());
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
};

public setYear = (value: Moment, year: number) => {
return value.clone().jYear(year);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
23 changes: 22 additions & 1 deletion test/utils/pickers/describeJalaliAdapter/testCalculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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');
Expand All @@ -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', () => {
Expand Down