Skip to content

Commit

Permalink
[DatePicker] Add test for textbox aria-invalid (#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSavignano authored Jul 31, 2020
1 parent 719f860 commit ff33683
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 128 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ workflows:
requires:
- 'Build and analyze bundlesize'

- jest_tests:
name: 'Dayjs jest tests'
lib: dayjs
requires:
- 'Build and analyze bundlesize'

- jest_tests:
name: 'Moment jest tests'
lib: moment
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"test": "jest",
"test:typescript": "yarn rimraf src/__tests__/dist && tsc -p src/__tests__/tsconfig.json",
"test:date-fns": "UTILS=date-fns yarn test",
"test:dayjs": "UTILS=datejs yarn test",
"test:dayjs": "UTILS=dayjs yarn test",
"test:luxon": "UTILS=luxon yarn test",
"test:moment": "UTILS=moment yarn test",
"start": "rollup --config rollup.config.dev.js --watch & npx tsc --watch",
Expand Down
88 changes: 86 additions & 2 deletions lib/src/__tests__/DatePickerTestingLib.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as React from 'react';
import deLocale from 'date-fns/locale/de';
import enLocale from 'date-fns/locale/en-US';
import 'dayjs/locale/de';
import TextField from '@material-ui/core/TextField';
import { screen, waitFor } from '@testing-library/react';
import { getByMuiTest, utilsToUse, FakeTransitionComponent } from './test-utils';
import { UtilClassToUse, getByMuiTest, utilsToUse, FakeTransitionComponent } from './test-utils';
import { createClientRender, fireEvent } from './createClientRender';
import { DatePicker, MobileDatePicker, DesktopDatePicker } from '../index';
import {
DatePicker,
DatePickerProps,
DesktopDatePicker,
LocalizationProvider,
MobileDatePicker,
} from '../index';

describe('<DatePicker />', () => {
const render = createClientRender({ strict: false });
Expand Down Expand Up @@ -96,4 +105,79 @@ describe('<DatePicker />', () => {
fireEvent.click(screen.getByLabelText('Jan 1, 2018'));
expect(onChangeMock).not.toHaveBeenCalled();
});

describe('input validation', () => {
interface FormProps {
locale: any;
Picker: React.ElementType<DatePickerProps>;
PickerProps: Partial<DatePickerProps>;
}
const Form = (props: FormProps) => {
const { locale, Picker, PickerProps } = props;
const [value, setValue] = React.useState<unknown>(new Date('01/01/2020'));

return (
<LocalizationProvider dateAdapter={UtilClassToUse} locale={locale}>
<Picker
onChange={setValue}
renderInput={(props2) => <TextField {...props2} />}
value={value}
{...PickerProps}
/>
</LocalizationProvider>
);
};

const tests = [
{
locale: 'en',
valid: 'January 2020',
invalid: 'Januar 2020',
dateFns: enLocale,
},
{
locale: 'de',
valid: 'Januar 2020',
invalid: 'Janua 2020',
dateFns: deLocale,
},
];

tests.forEach((test) => {
const { valid, invalid } = test;
const locale = process.env.UTILS === 'date-fns' ? test.dateFns : test.locale;

it(`${test.locale}: should set invalid`, () => {
render(
<Form
locale={locale}
Picker={DesktopDatePicker}
PickerProps={{ views: ['month', 'year'] }}
/>
);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: invalid } });
expect(input).toBeInvalid();
});

// Need to run with ICU loaded https://moment.github.io/luxon/docs/manual/install.html#node
if (process.env.UTILS === 'luxon') {
return;
}

it(`${test.locale}: should set to valid when was invalid`, () => {
render(
<Form
locale={locale}
Picker={DesktopDatePicker}
PickerProps={{ views: ['month', 'year'] }}
/>
);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: invalid } });
fireEvent.change(input, { target: { value: valid } });
expect(input).toBeValid();
});
});
});
});
6 changes: 6 additions & 0 deletions lib/src/__tests__/DateTimePickerTestingLib.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { createClientRender } from './createClientRender';
import { DesktopDateTimePicker, StaticDateTimePicker } from '../DateTimePicker';

describe('<DateTimePicker />', () => {
// Doesn't work
if (process.env.UTILS === 'dayjs') {
it('noop', () => {});
return;
}

const render = createClientRender({ strict: false });

it('prop: mask – should take the mask prop into account', () => {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/__tests__/KeyboardDatePicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { mount } from './test-utils';
import { DesktopDatePicker, DatePickerProps } from '../DatePicker/DatePicker';

describe('e2e -- DatePicker keyboard input', () => {
// Doesn't work
if (process.env.UTILS === 'dayjs') {
it('noop', () => {});
return;
}

const onChangeMock = jest.fn();
let component: ReactWrapper<DatePickerProps>;

Expand Down
Loading

1 comment on commit ff33683

@vercel
Copy link

@vercel vercel bot commented on ff33683 Jul 31, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.