Skip to content

Commit 3c8099b

Browse files
committed
fix(datepicker): fix date prop change not invoke state change bug
1 parent 7d78554 commit 3c8099b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const FORMATS = {
2020
'time': 'HH:mm'
2121
};
2222

23-
const SUPPORTED_ORIENTATIONS = ["portrait", "portrait-upside-down", "landscape", "landscape-left", "landscape-right"];
23+
const SUPPORTED_ORIENTATIONS = ['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right'];
2424

2525
class DatePicker extends Component {
2626
constructor(props) {
@@ -63,15 +63,15 @@ class DatePicker extends Component {
6363
// slide animation
6464
if (visible) {
6565
this.setState({modalVisible: visible});
66-
Animated.timing(
66+
return Animated.timing(
6767
this.state.animatedHeight,
6868
{
6969
toValue: height,
7070
duration: duration
7171
}
7272
).start();
7373
} else {
74-
Animated.timing(
74+
return Animated.timing(
7575
this.state.animatedHeight,
7676
{
7777
toValue: 0,

test/index.test.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,22 @@ describe('DatePicker:', () => {
173173

174174
it('onPressCancel', () => {
175175
const setModalVisible = sinon.spy();
176-
const wrapper = shallow(<DatePicker />);
176+
const onCloseModal = sinon.spy();
177+
const wrapper = shallow(<DatePicker onCloseModal={onCloseModal}/>);
177178
const datePicker = wrapper.instance();
178179
datePicker.setModalVisible = setModalVisible;
179180

180181
datePicker.onPressCancel();
181182

182183
expect(setModalVisible.calledWith(false)).to.equal(true);
184+
expect(onCloseModal.callCount).to.equal(1);
183185
});
184186

185187
it('onPressConfirm', () => {
186188
const setModalVisible = sinon.spy();
187189
const datePicked = sinon.spy();
188-
const wrapper = shallow(<DatePicker />);
190+
const onCloseModal = sinon.spy();
191+
const wrapper = shallow(<DatePicker onCloseModal={onCloseModal}/>);
189192
const datePicker = wrapper.instance();
190193
datePicker.setModalVisible = setModalVisible;
191194
datePicker.datePicked = datePicked;
@@ -194,6 +197,7 @@ describe('DatePicker:', () => {
194197

195198
expect(setModalVisible.calledWith(false)).to.equal(true);
196199
expect(datePicked.callCount).to.equal(1);
200+
expect(onCloseModal.callCount).to.equal(1);
197201
});
198202

199203
it('getDate', () => {
@@ -277,7 +281,10 @@ describe('DatePicker:', () => {
277281
it('onPressDate', () => {
278282
Platform.OS = 'ios';
279283
const setModalVisible = sinon.spy();
280-
const wrapper = shallow(<DatePicker date="2016-05-06" minDate="2016-04-01" maxDate="2016-06-01"/>);
284+
const onOpenModal = sinon.spy();
285+
const wrapper = shallow(
286+
<DatePicker date="2016-05-06" minDate="2016-04-01" maxDate="2016-06-01" onOpenModal={onOpenModal}/>
287+
);
281288
const datePicker = wrapper.instance();
282289
datePicker.setModalVisible = setModalVisible;
283290

@@ -290,6 +297,7 @@ describe('DatePicker:', () => {
290297
datePicker.onPressDate();
291298
expect(wrapper.state('date')).to.deep.equal(datePicker.getDate());
292299
expect(setModalVisible.callCount).to.equal(1);
300+
expect(onOpenModal.callCount).to.equal(1);
293301

294302
Platform.OS = 'android';
295303
expect(datePicker.onPressDate).to.not.throw(Error);
@@ -331,6 +339,16 @@ describe('DatePicker:', () => {
331339

332340
expect(datePicker.getTitleElement().props.children).to.equal(datePicker.getDateStr());
333341
});
342+
343+
it('`date` prop changes', () => {
344+
const wrapper = mount(<DatePicker date="2016-06-04" />);
345+
346+
expect(wrapper.state('date')).to.deep.equal(new Date(2016, 5, 4));
347+
348+
wrapper.setProps({date: '2016-06-05'});
349+
350+
expect(wrapper.state('date')).to.deep.equal(new Date(2016, 5, 5));
351+
});
334352
});
335353

336354

0 commit comments

Comments
 (0)