-
Notifications
You must be signed in to change notification settings - Fork 734
DateTimePicker - invoke value change only on real change #3227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0ca4c5d
4cf72a7
5a0617d
0101068
da6a085
05c8303
b373c1e
87b0bcf
6ee6f8d
30f990b
79bb57b
6452a93
c440d93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import {fireEvent, render} from '@testing-library/react-native'; | ||
import DateTimePicker, {DateTimePickerProps} from '../index'; | ||
import {act} from 'react-test-renderer'; | ||
|
||
const testID = 'dateTimePicker'; | ||
const TestCase = (props: Partial<Omit<DateTimePickerProps, 'dialogProps'>>) => { | ||
const defaultProps: DateTimePickerProps = { | ||
value: new Date('2021-04-04T00:00:00Z'), | ||
migrateDialog: true, | ||
testID | ||
}; | ||
return <DateTimePicker {...defaultProps} {...props}/>; | ||
}; | ||
|
||
const someDate = new Date('2021-04-04T00:00:00Z'); | ||
const someDateNextDay = new Date(someDate.getTime() + 24 * 60 * 60 * 1000); | ||
const someDateNextHour = new Date(someDate.getTime() + 60 * 60 * 1000); | ||
jest.mock('../../../optionalDependencies', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. jest-setup will mock this for every test file we have. I only see external modules in the jest-mock file i think its more organized this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think that's the proper place for it because if we'll have an issue in private we'll look there first for a mock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of issue will possibly have? When I you the jest mock here I don't think it has any effect on other test files. If I put it in the jest-setup one it does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we'll need to mock the |
||
const actualOptional = jest.requireActual('../../../optionalDependencies'); | ||
const view = jest.requireActual('../../view').default; | ||
return { | ||
...actualOptional, | ||
DateTimePickerPackage: view | ||
}; | ||
}); | ||
|
||
describe('DateTimePicker', () => { | ||
test.each(['time', 'date'] as const)('should not invoke onChange when value is not changed - mode %s', mode => { | ||
const onChange = jest.fn(); | ||
const renderTree = render(<TestCase onChange={onChange} mode={mode}/>); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.press(renderTree.getByTestId(`${testID}.overlay`)); | ||
fireEvent.press(renderTree.getByTestId(`${testID}.done`)); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
}); | ||
test.each(['time', 'date'] as const)('should invoke onChange when value is changed - mode %s', async mode => { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const onChange = jest.fn(); | ||
const renderTree = render(<TestCase onChange={onChange} mode={mode} value={someDate}/>); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
act(() => { | ||
fireEvent.press(renderTree.getByTestId(`${testID}.overlay`)); | ||
}); | ||
fireEvent(renderTree.getByTestId(`${testID}.picker`), | ||
'onChange', | ||
{type: 'set'}, | ||
mode === 'time' ? someDateNextHour : someDateNextDay); | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fireEvent.press(renderTree.getByTestId(`${testID}.done`)); | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(onChange).toHaveBeenCalled(); | ||
}); | ||
}); |
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.
Maybe:
sameTime
\sameDate
nextDay
nextHour
Uh oh!
There was an error while loading. Please reload this page.
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.
Wdym here?
Do you think
date
,nextHour
,nextDay
is better?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.
Yes, sorry I was not clear - this is about naming.
Personally I think
sameTime
,nextDay
andnextHour
.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.
Why would you call it sameDate? What is it similar to?
I feel like its pretty clear, it also states that the date its self is not important.
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.
Because it's the same date as the date of the
value
, it's not a random "some date" it's the same date and the next day after the value and so on.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.
I moved the date objects to the top of the file and used them in the default props.