Skip to content

Commit

Permalink
[test] Fix adapters code coverage (mui#13969)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas <[email protected]>
  • Loading branch information
2 people authored and thomasmoon committed Sep 6, 2024
1 parent bd020d4 commit 7e19d52
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 64 deletions.
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') {
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());
};

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

0 comments on commit 7e19d52

Please sign in to comment.