-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datepicker): fire onChange callback when typed date is invalid (#336
) * test: move setup function to new file * fix(datepicker): fire onChange callback when typed date is invalid * test(datepicker): fix test for usecase "onChange is fired when invalid date is typed"
- Loading branch information
1 parent
9e297b8
commit 6d18c56
Showing
6 changed files
with
64 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { SemanticDatepickerProps } from '../types'; | ||
import DatePicker from '../'; | ||
|
||
export const setup = (props: Partial<SemanticDatepickerProps> = {}) => { | ||
const options = render(<DatePicker onChange={jest.fn()} {...props} />); | ||
const getQuery = props.inline ? options.queryByTestId : options.getByTestId; | ||
const getIcon = () => options.getByTestId('datepicker-icon'); | ||
|
||
return { | ||
...options, | ||
getIcon, | ||
openDatePicker: () => fireEvent.click(getIcon()), | ||
rerender: (newProps?: Partial<SemanticDatepickerProps>) => | ||
options.rerender( | ||
<DatePicker onChange={jest.fn()} {...props} {...newProps} /> | ||
), | ||
datePickerInput: getQuery('datepicker-input') | ||
?.firstChild as HTMLInputElement, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { fireEvent, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { setup } from './_utils'; | ||
|
||
it('onChange is fired when invalid date is typed', () => { | ||
const onBlur = jest.fn(); | ||
const onChange = jest.fn(); | ||
const { datePickerInput, openDatePicker } = setup({ onBlur, onChange }); | ||
|
||
openDatePicker(); | ||
fireEvent.click(screen.getByText('Today')); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onBlur).toHaveBeenCalledTimes(1); | ||
|
||
userEvent.type(datePickerInput, '{backspace}'); | ||
userEvent.type(datePickerInput, '{backspace}'); | ||
fireEvent.keyDown(datePickerInput, { keyCode: 13 }); | ||
|
||
expect(datePickerInput.value).toBe(''); | ||
expect(onBlur).toHaveBeenCalledTimes(2); | ||
expect(onChange).toHaveBeenCalledTimes(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6d18c56
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: