Skip to content

Commit

Permalink
[core] Improve testing of the adapters (#8789)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored May 12, 2023
1 parent 5146ec7 commit 9483d3a
Show file tree
Hide file tree
Showing 38 changed files with 1,020 additions and 501 deletions.
4 changes: 3 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ coverage:
target: auto
threshold: 1%
adapters:
target: 94%
target: 100%
paths:
- 'packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts'
- 'packages/x-date-pickers/src/AdapterDateFnsJalali/AdapterDateFnsJalali.ts'
Expand All @@ -14,6 +14,8 @@ coverage:
- 'packages/x-date-pickers/src/AdapterMoment/AdapterMoment.ts'
- 'packages/x-date-pickers/src/AdapterMomentHijri/AdapterMomentHijri.ts'
- 'packages/x-date-pickers/src/AdapterMomentJalaali/AdapterMomentJalaali.ts'
ignore:
- '**/*.test.tsx'
patch: off

comment: false
3 changes: 2 additions & 1 deletion packages/grid/x-data-grid-premium/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"include": [
"src/**/*",
"../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../test/utils/addChaiAssertions.ts",
"../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../../node_modules/@mui/material/themeCssVarsAugmentation"
]
}
3 changes: 2 additions & 1 deletion packages/grid/x-data-grid-pro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"include": [
"src/**/*",
"../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../test/utils/addChaiAssertions.ts",
"../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../../node_modules/@mui/material/themeCssVarsAugmentation"
]
}
3 changes: 2 additions & 1 deletion packages/grid/x-data-grid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"include": [
"src/**/*",
"../../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../test/utils/addChaiAssertions.ts",
"../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../../node_modules/@mui/material/themeCssVarsAugmentation"
]
}
1 change: 1 addition & 0 deletions packages/x-charts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"include": [
"src/**/*",
"../../test/utils/addChaiAssertions.ts",
"../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../node_modules/@mui/material/themeCssVarsAugmentation"
]
Expand Down
1 change: 1 addition & 0 deletions packages/x-date-pickers-pro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"include": [
"src/**/*",
"../../test/utils/addChaiAssertions.ts",
"../../node_modules/@mui/monorepo/test/utils/initMatchers.ts",
"../../node_modules/@mui/material/themeCssVarsAugmentation"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import de from 'date-fns/locale/de';
import ru from 'date-fns/locale/ru';
import {
describeGregorianAdapter,
TEST_DATE_ISO,
TEST_DATE_ISO_STRING,
} from '@mui/x-date-pickers/tests/describeGregorianAdapter';

describe('<AdapterDateFns />', () => {
describeGregorianAdapter(AdapterDateFns, { formatDateTime: 'yyyy-MM-dd HH:mm:ss', locale: enUS });
describeGregorianAdapter(AdapterDateFns, { formatDateTime: 'yyyy-MM-dd HH:mm:ss' });

describe('Adapter localization', () => {
describe('English', () => {
const adapter = new AdapterDateFns({ locale: enUS });
const date = adapter.date(TEST_DATE_ISO)!;
const date = adapter.date(TEST_DATE_ISO_STRING)!;

it('getWeekdays: should start on Sunday', () => {
const result = adapter.getWeekdays();
Expand All @@ -50,7 +50,7 @@ describe('<AdapterDateFns />', () => {
});

it('getWeekArray: should start on Monday', () => {
const date = adapter.date(TEST_DATE_ISO)!;
const date = adapter.date(TEST_DATE_ISO_STRING)!;
const result = adapter.getWeekArray(date);
expect(adapter.formatByString(result[0][0], 'EEEEEE')).to.equal('пн');
});
Expand Down
13 changes: 5 additions & 8 deletions packages/x-date-pickers/src/AdapterDateFns/AdapterDateFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import endOfWeek from 'date-fns/endOfWeek';
import endOfYear from 'date-fns/endOfYear';
import dateFnsFormat from 'date-fns/format';
import getDate from 'date-fns/getDate';
import getDay from 'date-fns/getDay';
import getDaysInMonth from 'date-fns/getDaysInMonth';
import getHours from 'date-fns/getHours';
import getMinutes from 'date-fns/getMinutes';
Expand Down Expand Up @@ -527,18 +526,16 @@ export class AdapterDateFns implements MuiPickersAdapter<Date, DateFnsLocale> {
let count = 0;
let current = start;
const nestedWeeks: Date[][] = [];
let lastDay: number | null = null;

while (isBefore(current, end)) {
const weekNumber = Math.floor(count / 7);
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
const day = getDay(current);
if (lastDay !== day) {
lastDay = day;
nestedWeeks[weekNumber].push(current);
count += 1;
}
nestedWeeks[weekNumber].push(current);

current = addDays(current, 1);
count += 1;
}

return nestedWeeks;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AdapterFormats } from '@mui/x-date-pickers';
import { expect } from 'chai';

describe('<AdapterDateFnsJalali />', () => {
describeJalaliAdapter(AdapterDateFnsJalali, { locale: faJalaliIR });
describeJalaliAdapter(AdapterDateFnsJalali, {});

describe('Adapter localization', () => {
it('Formatting', () => {
Expand Down Expand Up @@ -59,17 +59,13 @@ describe('<AdapterDateFnsJalali />', () => {
};

Object.keys(localizedTexts).forEach((localeKey) => {
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`;
const localeObject =
localeKey === 'undefined'
? undefined
: {
faIR,
faJalaliIR,
enUS,
}[localeKey];
const localeObject = {
faIR,
faJalaliIR,
enUS,
}[localeKey];

describe(`test with the ${localeName} locale`, () => {
describe(`test with the "${localeKey}" locale`, () => {
const { render, adapter } = createPickerRenderer({
clock: 'fake',
adapterName: 'date-fns-jalali',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import getWeek from 'date-fns-jalali/getWeek';
import getYear from 'date-fns-jalali/getYear';
import getMonth from 'date-fns-jalali/getMonth';
import getDate from 'date-fns-jalali/getDate';
import getDay from 'date-fns-jalali/getDay';
import getDaysInMonth from 'date-fns-jalali/getDaysInMonth';
import getMinutes from 'date-fns-jalali/getMinutes';
import isAfter from 'date-fns-jalali/isAfter';
Expand Down Expand Up @@ -541,18 +540,16 @@ export class AdapterDateFnsJalali implements MuiPickersAdapter<Date, DateFnsLoca
let count = 0;
let current = start;
const nestedWeeks: Date[][] = [];
let lastDay: number | null = null;

while (isBefore(current, end)) {
const weekNumber = Math.floor(count / 7);
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
const day = getDay(current);
if (lastDay !== day) {
lastDay = day;
nestedWeeks[weekNumber].push(current);
count += 1;
}
nestedWeeks[weekNumber].push(current);

current = addDays(current, 1);
count += 1;
}

return nestedWeeks;
};

Expand Down
56 changes: 32 additions & 24 deletions packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { spy } from 'sinon';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
Expand All @@ -15,39 +14,25 @@ import {
expectInputValue,
MockedDataTransfer,
} from 'test/utils/pickers-utils';
import 'dayjs/locale/fr';
import 'dayjs/locale/de';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
import {
describeGregorianAdapter,
TEST_DATE_ISO,
TEST_DATE_ISO_STRING,
} from 'packages/x-date-pickers/src/tests/describeGregorianAdapter';

dayjs.extend(utc);

const testDate = new Date(2018, 4, 15, 9, 35);
const localizedTexts = {
undefined: {
placeholder: 'MM/DD/YYYY hh:mm aa',
value: '05/15/2018 09:35 AM',
},
fr: {
placeholder: 'DD/MM/YYYY hh:mm',
value: '15/05/2018 09:35',
},
de: {
placeholder: 'DD.MM.YYYY hh:mm',
value: '15.05.2018 09:35',
},
};
import 'dayjs/locale/fr';
import 'dayjs/locale/de';
// We import it here just to have the typing
import 'dayjs/plugin/utc';

describe('<AdapterDayjs />', () => {
describeGregorianAdapter(AdapterDayjs, { formatDateTime: 'YYYY-MM-DD HH:mm:ss', locale: 'en' });
describeGregorianAdapter(AdapterDayjs, { formatDateTime: 'YYYY-MM-DD HH:mm:ss' });

describe('Adapter localization', () => {
describe('English', () => {
const adapter = new AdapterDayjs({ instance: dayjs, locale: 'en' });
const date = adapter.date(TEST_DATE_ISO)!;
const date = adapter.date(TEST_DATE_ISO_STRING)!;

it('getWeekdays: should start on Sunday', () => {
const result = adapter.getWeekdays();
Expand All @@ -73,7 +58,7 @@ describe('<AdapterDayjs />', () => {
});

it('getWeekArray: should start on Monday', () => {
const date = adapter.date(TEST_DATE_ISO)!;
const date = adapter.date(TEST_DATE_ISO_STRING)!;
const result = adapter.getWeekArray(date);
expect(result[0][0].format('dd')).to.equal('пн');
});
Expand Down Expand Up @@ -112,9 +97,32 @@ describe('<AdapterDayjs />', () => {
expectDate('keyboardDateTime12h', '02/01/2020 11:44 PM', '01.02.2020 11:44 вечера');
expectDate('keyboardDateTime24h', '02/01/2020 23:44', '01.02.2020 23:44');
});

it('should warn when trying to use a non-loaded locale', () => {
const adapter = new AdapterDayjs({ locale: 'pl' });
expect(() => adapter.is12HourCycleInCurrentLocale()).toWarnDev(
'Your locale has not been found.',
);
});
});

describe('Picker localization', () => {
const testDate = new Date(2018, 4, 15, 9, 35);
const localizedTexts = {
undefined: {
placeholder: 'MM/DD/YYYY hh:mm aa',
value: '05/15/2018 09:35 AM',
},
fr: {
placeholder: 'DD/MM/YYYY hh:mm',
value: '15/05/2018 09:35',
},
de: {
placeholder: 'DD.MM.YYYY hh:mm',
value: '15.05.2018 09:35',
},
};

Object.keys(localizedTexts).forEach((localeKey) => {
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`;
const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey };
Expand Down
2 changes: 1 addition & 1 deletion packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class AdapterDayjs implements MuiPickersAdapter<Dayjs, string> {
}

private getLocaleFormats = () => {
const locales = this.rawDayJsInstance.Ls ?? defaultDayjs.Ls;
const locales = defaultDayjs.Ls;
const locale = this.locale || 'en';

let localeObject = locales[locale];
Expand Down
42 changes: 21 additions & 21 deletions packages/x-date-pickers/src/AdapterLuxon/AdapterLuxon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,19 @@ import { AdapterFormats } from '@mui/x-date-pickers/models';
import { screen } from '@mui/monorepo/test/utils/createRenderer';
import { expect } from 'chai';
import {
cleanText,
createPickerRenderer,
expectInputPlaceholder,
expectInputValue,
} from 'test/utils/pickers-utils';
import {
describeGregorianAdapter,
TEST_DATE_ISO,
TEST_DATE_ISO_STRING,
} from '@mui/x-date-pickers/tests/describeGregorianAdapter';

const testDate = new Date(2018, 4, 15, 9, 35);
const localizedTexts = {
undefined: {
placeholder: 'MM/DD/YYYY hh:mm aa',
value: '05/15/2018 09:35 AM',
},
fr: {
placeholder: 'DD/MM/YYYY hh:mm',
value: '15/05/2018 09:35',
},
de: {
placeholder: 'DD.MM.YYYY hh:mm',
value: '15.05.2018 09:35',
},
};

describe('<AdapterLuxon />', () => {
describeGregorianAdapter(AdapterLuxon, {
formatDateTime: 'yyyy-MM-dd HH:mm:ss',
locale: 'en-US',
});

describe('Adapter localization', () => {
Expand All @@ -54,7 +38,7 @@ describe('<AdapterLuxon />', () => {
});

it('getWeekArray: should start on Monday', () => {
const date = adapter.date(TEST_DATE_ISO)!;
const date = adapter.date(TEST_DATE_ISO_STRING)!;
const result = adapter.getWeekArray(date);
expect(result[0][0].toFormat('ccc')).to.equal('пн');
});
Expand All @@ -68,7 +52,7 @@ describe('<AdapterLuxon />', () => {
});
});

describe('Formatting', () => {
it('Formatting', () => {
const adapter = new AdapterLuxon({ locale: 'en-US' });
const adapterRu = new AdapterLuxon({ locale: 'ru' });

Expand All @@ -80,7 +64,7 @@ describe('<AdapterLuxon />', () => {
const date = adapter.date('2020-02-01T23:44:00.000Z')!;

expect(adapter.format(date, format)).to.equal(expectedWithEn);
expect(adapterRu.format(date, format)).to.equal(expectedWithRu);
expect(cleanText(adapterRu.format(date, format))).to.equal(expectedWithRu);
};

expectDate('fullDate', 'Feb 1, 2020', '1 февр. 2020 г.');
Expand All @@ -96,6 +80,22 @@ describe('<AdapterLuxon />', () => {
});

describe('Picker localization', () => {
const testDate = new Date(2018, 4, 15, 9, 35);
const localizedTexts = {
undefined: {
placeholder: 'MM/DD/YYYY hh:mm aa',
value: '05/15/2018 09:35 AM',
},
fr: {
placeholder: 'DD/MM/YYYY hh:mm',
value: '15/05/2018 09:35',
},
de: {
placeholder: 'DD.MM.YYYY hh:mm',
value: '15.05.2018 09:35',
},
};

Object.keys(localizedTexts).forEach((localeKey) => {
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`;
const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey };
Expand Down
Loading

0 comments on commit 9483d3a

Please sign in to comment.