Skip to content

Commit

Permalink
oops, too level describe convention wasn't applied
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jul 30, 2020
1 parent 5d0b3e4 commit 55976a5
Showing 1 changed file with 130 additions and 125 deletions.
255 changes: 130 additions & 125 deletions lib/src/__tests__/Validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,154 +13,159 @@ const disableWeekends = (date: unknown) => {
return isWeekend(utilsToUse.toJsDate(date));
};

describe('DatePicker validation', () => {
describe('validation', () => {
// Doesn't work
if (process.env.UTILS === 'dayjs') {
return;
}

test.each`
props | input | expectedError
${{}} | ${'invalidText'} | ${'invalidDate'}
${{ disablePast: true }} | ${'01/01/1900'} | ${'disablePast'}
${{ disableFuture: true }} | ${'01/01/2050'} | ${'disableFuture'}
${{ minDate: new Date('01/01/2000') }} | ${'01/01/1990'} | ${'minDate'}
${{ maxDate: new Date('01/01/2000') }} | ${'01/01/2010'} | ${'maxDate'}
${{ shouldDisableDate: disableWeekends }} | ${'04/25/2020'} | ${'shouldDisableDate'}
`('Should dispatch onError $expectedError', ({ props, input, expectedError }) => {
if (process.env.UTILS === 'luxon') {
return;
}

const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopDatePicker {...stateProps} {...props} onError={onErrorMock} />
));

component.find('input').simulate('change', {
target: {
value: input,
},
});

expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
});

it('It should properly annulate the error', () => {
if (process.env.UTILS === 'luxon') {
return;
}

const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopDatePicker {...stateProps} disablePast onError={onErrorMock} />
));
describe('DatePicker', () => {
test.each`
props | input | expectedError
${{}} | ${'invalidText'} | ${'invalidDate'}
${{ disablePast: true }} | ${'01/01/1900'} | ${'disablePast'}
${{ disableFuture: true }} | ${'01/01/2050'} | ${'disableFuture'}
${{ minDate: new Date('01/01/2000') }} | ${'01/01/1990'} | ${'minDate'}
${{ maxDate: new Date('01/01/2000') }} | ${'01/01/2010'} | ${'maxDate'}
${{ shouldDisableDate: disableWeekends }} | ${'04/25/2020'} | ${'shouldDisableDate'}
`('Should dispatch onError $expectedError', ({ props, input, expectedError }) => {
if (process.env.UTILS === 'luxon') {
return;
}

const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopDatePicker {...stateProps} {...props} onError={onErrorMock} />
));

component.find('input').simulate('change', {
target: {
value: input,
},
});

component.find('input').simulate('change', {
target: {
value: '01/01/1900',
},
expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
});

expect(onErrorMock).toHaveBeenCalledWith('disablePast', expect.anything());

component.find('input').simulate('change', {
target: {
value: '01/01/2099',
},
});
it('It should properly annulate the error', () => {
if (process.env.UTILS === 'luxon') {
return;
}

expect(onErrorMock).toHaveBeenCalledWith(null, expect.anything());
});
});
const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopDatePicker {...stateProps} disablePast onError={onErrorMock} />
));

describe('TimePicker validation', () => {
const createTime = (time: string) => new Date(`01/01/2000 ${time}`);
const shouldDisableTime: TimePickerProps['shouldDisableTime'] = (value) => {
return value === 10;
};

test.each`
props | input | expectedError
${{}} | ${'invalidText'} | ${'invalidDate'}
${{ minTime: createTime('08:00') }} | ${'03:00'} | ${'minTime'}
${{ maxTime: createTime('08:00') }} | ${'12:00'} | ${'maxTime'}
${{ shouldDisableTime }} | ${'10:00'} | ${'shouldDisableTime-hours'}
${{ shouldDisableTime }} | ${'00:10'} | ${'shouldDisableTime-minutes'}
`('TimePicker should dispatch onError $expectedError', ({ props, input, expectedError }) => {
const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopTimePicker ampm={false} {...stateProps} {...props} onError={onErrorMock} />
));

component.find('input').simulate('change', {
target: {
value: input,
},
});
component.find('input').simulate('change', {
target: {
value: '01/01/1900',
},
});

expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
});
});
expect(onErrorMock).toHaveBeenCalledWith('disablePast', expect.anything());

describe('DateRangePicker validation', () => {
test.each`
props | startInput | endInput | expectedError
${{}} | ${'99/99/9999'} | ${'99/99/9999'} | ${['invalidDate', 'invalidDate']}
${{}} | ${'01/01/2020'} | ${'01/01/1920'} | ${['invalidRange', 'invalidRange']}
${{ minDate: new Date('01/01/2000') }} | ${'01/01/1990'} | ${'01/01/1990'} | ${['minDate', 'minDate']}
${{ maxDate: new Date('01/01/2000') }} | ${'01/01/2010'} | ${'01/01/2010'} | ${['maxDate', 'maxDate']}
${{ disablePast: true }} | ${'01/01/1800'} | ${'01/01/2120'} | ${['disablePast', 'maxDate']}
${{ disablePast: true }} | ${'01/01/1800'} | ${'01/01/2090'} | ${['disablePast', null]}
${{ disableFuture: true }} | ${'01/01/2010'} | ${'01/01/2050'} | ${[null, 'disableFuture']}
${{ shouldDisableDate: disableWeekends }} | ${'04/25/2020'} | ${'06/25/2020'} | ${['shouldDisableDate', null]}
`('Should dispatch onError $expectedError', ({ props, startInput, endInput, expectedError }) => {
if (process.env.UTILS === 'luxon') {
return;
}

const onErrorMock = jest.fn();

const component = mountPickerWithState([null, null], (stateProps) => (
<DesktopDateRangePicker
{...stateProps}
{...props}
onError={onErrorMock}
renderInput={(startProps, endProps) => (
<React.Fragment>
<TextField {...startProps} />
<TextField {...endProps} />
</React.Fragment>
)}
/>
));

component
.find('input')
.at(0)
.simulate('change', {
component.find('input').simulate('change', {
target: {
value: startInput,
value: '01/01/2099',
},
});

act(() => {
jest.runAllTimers();
expect(onErrorMock).toHaveBeenCalledWith(null, expect.anything());
});
});

component
.find('input')
.at(1)
.simulate('change', {
describe('TimePicker', () => {
const createTime = (time: string) => new Date(`01/01/2000 ${time}`);
const shouldDisableTime: TimePickerProps['shouldDisableTime'] = (value) => {
return value === 10;
};

test.each`
props | input | expectedError
${{}} | ${'invalidText'} | ${'invalidDate'}
${{ minTime: createTime('08:00') }} | ${'03:00'} | ${'minTime'}
${{ maxTime: createTime('08:00') }} | ${'12:00'} | ${'maxTime'}
${{ shouldDisableTime }} | ${'10:00'} | ${'shouldDisableTime-hours'}
${{ shouldDisableTime }} | ${'00:10'} | ${'shouldDisableTime-minutes'}
`('TimePicker should dispatch onError $expectedError', ({ props, input, expectedError }) => {
const onErrorMock = jest.fn();
const component = mountPickerWithState(utilsToUse.date(), (stateProps) => (
<DesktopTimePicker ampm={false} {...stateProps} {...props} onError={onErrorMock} />
));

component.find('input').simulate('change', {
target: {
value: endInput,
value: input,
},
});

act(() => {
jest.runAllTimers();
expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
});
});

expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
describe('DateRangePicker', () => {
test.each`
props | startInput | endInput | expectedError
${{}} | ${'99/99/9999'} | ${'99/99/9999'} | ${['invalidDate', 'invalidDate']}
${{}} | ${'01/01/2020'} | ${'01/01/1920'} | ${['invalidRange', 'invalidRange']}
${{ minDate: new Date('01/01/2000') }} | ${'01/01/1990'} | ${'01/01/1990'} | ${['minDate', 'minDate']}
${{ maxDate: new Date('01/01/2000') }} | ${'01/01/2010'} | ${'01/01/2010'} | ${['maxDate', 'maxDate']}
${{ disablePast: true }} | ${'01/01/1800'} | ${'01/01/2120'} | ${['disablePast', 'maxDate']}
${{ disablePast: true }} | ${'01/01/1800'} | ${'01/01/2090'} | ${['disablePast', null]}
${{ disableFuture: true }} | ${'01/01/2010'} | ${'01/01/2050'} | ${[null, 'disableFuture']}
${{ shouldDisableDate: disableWeekends }} | ${'04/25/2020'} | ${'06/25/2020'} | ${['shouldDisableDate', null]}
`(
'Should dispatch onError $expectedError',
({ props, startInput, endInput, expectedError }) => {
if (process.env.UTILS === 'luxon') {
return;
}

const onErrorMock = jest.fn();

const component = mountPickerWithState([null, null], (stateProps) => (
<DesktopDateRangePicker
{...stateProps}
{...props}
onError={onErrorMock}
renderInput={(startProps, endProps) => (
<React.Fragment>
<TextField {...startProps} />
<TextField {...endProps} />
</React.Fragment>
)}
/>
));

component
.find('input')
.at(0)
.simulate('change', {
target: {
value: startInput,
},
});

act(() => {
jest.runAllTimers();
});

component
.find('input')
.at(1)
.simulate('change', {
target: {
value: endInput,
},
});

act(() => {
jest.runAllTimers();
});

expect(onErrorMock).toBeCalledWith(expectedError, expect.anything());
}
);
});
});

0 comments on commit 55976a5

Please sign in to comment.