Skip to content

Commit ff33683

Browse files
[DatePicker] Add test for textbox aria-invalid (#1955)
1 parent 719f860 commit ff33683

File tree

6 files changed

+241
-128
lines changed

6 files changed

+241
-128
lines changed

.circleci/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ workflows:
156156
requires:
157157
- 'Build and analyze bundlesize'
158158

159+
- jest_tests:
160+
name: 'Dayjs jest tests'
161+
lib: dayjs
162+
requires:
163+
- 'Build and analyze bundlesize'
164+
159165
- jest_tests:
160166
name: 'Moment jest tests'
161167
lib: moment

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"test": "jest",
6161
"test:typescript": "yarn rimraf src/__tests__/dist && tsc -p src/__tests__/tsconfig.json",
6262
"test:date-fns": "UTILS=date-fns yarn test",
63-
"test:dayjs": "UTILS=datejs yarn test",
63+
"test:dayjs": "UTILS=dayjs yarn test",
6464
"test:luxon": "UTILS=luxon yarn test",
6565
"test:moment": "UTILS=moment yarn test",
6666
"start": "rollup --config rollup.config.dev.js --watch & npx tsc --watch",

lib/src/__tests__/DatePickerTestingLib.test.tsx

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import * as React from 'react';
2+
import deLocale from 'date-fns/locale/de';
3+
import enLocale from 'date-fns/locale/en-US';
4+
import 'dayjs/locale/de';
25
import TextField from '@material-ui/core/TextField';
36
import { screen, waitFor } from '@testing-library/react';
4-
import { getByMuiTest, utilsToUse, FakeTransitionComponent } from './test-utils';
7+
import { UtilClassToUse, getByMuiTest, utilsToUse, FakeTransitionComponent } from './test-utils';
58
import { createClientRender, fireEvent } from './createClientRender';
6-
import { DatePicker, MobileDatePicker, DesktopDatePicker } from '../index';
9+
import {
10+
DatePicker,
11+
DatePickerProps,
12+
DesktopDatePicker,
13+
LocalizationProvider,
14+
MobileDatePicker,
15+
} from '../index';
716

817
describe('<DatePicker />', () => {
918
const render = createClientRender({ strict: false });
@@ -96,4 +105,79 @@ describe('<DatePicker />', () => {
96105
fireEvent.click(screen.getByLabelText('Jan 1, 2018'));
97106
expect(onChangeMock).not.toHaveBeenCalled();
98107
});
108+
109+
describe('input validation', () => {
110+
interface FormProps {
111+
locale: any;
112+
Picker: React.ElementType<DatePickerProps>;
113+
PickerProps: Partial<DatePickerProps>;
114+
}
115+
const Form = (props: FormProps) => {
116+
const { locale, Picker, PickerProps } = props;
117+
const [value, setValue] = React.useState<unknown>(new Date('01/01/2020'));
118+
119+
return (
120+
<LocalizationProvider dateAdapter={UtilClassToUse} locale={locale}>
121+
<Picker
122+
onChange={setValue}
123+
renderInput={(props2) => <TextField {...props2} />}
124+
value={value}
125+
{...PickerProps}
126+
/>
127+
</LocalizationProvider>
128+
);
129+
};
130+
131+
const tests = [
132+
{
133+
locale: 'en',
134+
valid: 'January 2020',
135+
invalid: 'Januar 2020',
136+
dateFns: enLocale,
137+
},
138+
{
139+
locale: 'de',
140+
valid: 'Januar 2020',
141+
invalid: 'Janua 2020',
142+
dateFns: deLocale,
143+
},
144+
];
145+
146+
tests.forEach((test) => {
147+
const { valid, invalid } = test;
148+
const locale = process.env.UTILS === 'date-fns' ? test.dateFns : test.locale;
149+
150+
it(`${test.locale}: should set invalid`, () => {
151+
render(
152+
<Form
153+
locale={locale}
154+
Picker={DesktopDatePicker}
155+
PickerProps={{ views: ['month', 'year'] }}
156+
/>
157+
);
158+
const input = screen.getByRole('textbox');
159+
fireEvent.change(input, { target: { value: invalid } });
160+
expect(input).toBeInvalid();
161+
});
162+
163+
// Need to run with ICU loaded https://moment.github.io/luxon/docs/manual/install.html#node
164+
if (process.env.UTILS === 'luxon') {
165+
return;
166+
}
167+
168+
it(`${test.locale}: should set to valid when was invalid`, () => {
169+
render(
170+
<Form
171+
locale={locale}
172+
Picker={DesktopDatePicker}
173+
PickerProps={{ views: ['month', 'year'] }}
174+
/>
175+
);
176+
const input = screen.getByRole('textbox');
177+
fireEvent.change(input, { target: { value: invalid } });
178+
fireEvent.change(input, { target: { value: valid } });
179+
expect(input).toBeValid();
180+
});
181+
});
182+
});
99183
});

lib/src/__tests__/DateTimePickerTestingLib.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { createClientRender } from './createClientRender';
66
import { DesktopDateTimePicker, StaticDateTimePicker } from '../DateTimePicker';
77

88
describe('<DateTimePicker />', () => {
9+
// Doesn't work
10+
if (process.env.UTILS === 'dayjs') {
11+
it('noop', () => {});
12+
return;
13+
}
14+
915
const render = createClientRender({ strict: false });
1016

1117
it('prop: mask – should take the mask prop into account', () => {

lib/src/__tests__/KeyboardDatePicker.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { mount } from './test-utils';
55
import { DesktopDatePicker, DatePickerProps } from '../DatePicker/DatePicker';
66

77
describe('e2e -- DatePicker keyboard input', () => {
8+
// Doesn't work
9+
if (process.env.UTILS === 'dayjs') {
10+
it('noop', () => {});
11+
return;
12+
}
13+
814
const onChangeMock = jest.fn();
915
let component: ReactWrapper<DatePickerProps>;
1016

0 commit comments

Comments
 (0)