-
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 7 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,31 @@ | ||
import {fireEvent} from '@testing-library/react-native'; | ||
import { | ||
ComponentProps, | ||
ExpandableOverlayDriver, | ||
ButtonDriver | ||
} from '../../testkit'; | ||
|
||
export const DateTimePickerDriver = (props: ComponentProps) => { | ||
const {renderTree, testID} = props; | ||
const expandableOverlayDriver = ExpandableOverlayDriver({...props, testID: `${testID}.overlay`}); | ||
const openPicker = () => { | ||
expandableOverlayDriver.open(); | ||
}; | ||
const discardPicker = () => { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ButtonDriver({...props, testID: `${testID}.cancel`}).press(); | ||
}; | ||
const submitSelection = () => { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ButtonDriver({...props, testID: `${testID}.done`}).press(); | ||
}; | ||
const changeDateTo = (date: Date) => { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pickerElement = renderTree.getByTestId(`${testID}.picker`); | ||
fireEvent(pickerElement, 'onChange', {type: 'set'}, date); | ||
}; | ||
|
||
return { | ||
openPicker, | ||
discardPicker, | ||
submitSelection, | ||
changeDateTo | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
import {render} from '@testing-library/react-native'; | ||
import DateTimePicker, {DateTimePickerProps} from '../index'; | ||
import {DateTimePickerDriver} from '../DateTimePicker.driver'; | ||
|
||
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); | ||
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. Maybe: 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. Wdym here? 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, sorry I was not clear - this is about naming. 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. Why would you call it sameDate? What is it similar to? 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. Because it's the same date as the date of the 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. I moved the date objects to the top of the file and used them in the default props. |
||
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 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(); | ||
const driver = DateTimePickerDriver({renderTree, testID}); | ||
driver.openPicker(); | ||
driver.changeDateTo(mode === 'time' ? someDateNextHour : someDateNextDay); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
driver.submitSelection(); | ||
expect(onChange).toHaveBeenCalled(); | ||
}); | ||
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(); | ||
const driver = DateTimePickerDriver({renderTree, testID}); | ||
driver.openPicker(); | ||
driver.submitSelection(); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function isSameDate(date1?: Date, date2?: Date) { | ||
return ( | ||
date1?.getFullYear() === date2?.getFullYear() && | ||
date1?.getMonth() === date2?.getMonth() && | ||
date1?.getDate() === date2?.getDate() | ||
); | ||
} | ||
export function isSameHourAndMinute(date1?: Date, date2?: Date) { | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return date1?.getHours() === date2?.getHours() && date1?.getMinutes() === date2?.getMinutes(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as TextUtils from './textUtils'; | ||
import * as StyleUtils from './styleUtils'; | ||
import * as DateUtils from './dateUtils'; | ||
|
||
export {TextUtils, StyleUtils}; | ||
export {TextUtils, StyleUtils, DateUtils}; |
Uh oh!
There was an error while loading. Please reload this page.