Skip to content

Commit

Permalink
fix(datepicker): fix date prop change not invoke state change bug
Browse files Browse the repository at this point in the history
  • Loading branch information
feyy committed Mar 21, 2017
1 parent 7d78554 commit 3c8099b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const FORMATS = {
'time': 'HH:mm'
};

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

class DatePicker extends Component {
constructor(props) {
Expand Down Expand Up @@ -63,15 +63,15 @@ class DatePicker extends Component {
// slide animation
if (visible) {
this.setState({modalVisible: visible});
Animated.timing(
return Animated.timing(
this.state.animatedHeight,
{
toValue: height,
duration: duration
}
).start();
} else {
Animated.timing(
return Animated.timing(
this.state.animatedHeight,
{
toValue: 0,
Expand Down
24 changes: 21 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,22 @@ describe('DatePicker:', () => {

it('onPressCancel', () => {
const setModalVisible = sinon.spy();
const wrapper = shallow(<DatePicker />);
const onCloseModal = sinon.spy();
const wrapper = shallow(<DatePicker onCloseModal={onCloseModal}/>);
const datePicker = wrapper.instance();
datePicker.setModalVisible = setModalVisible;

datePicker.onPressCancel();

expect(setModalVisible.calledWith(false)).to.equal(true);
expect(onCloseModal.callCount).to.equal(1);
});

it('onPressConfirm', () => {
const setModalVisible = sinon.spy();
const datePicked = sinon.spy();
const wrapper = shallow(<DatePicker />);
const onCloseModal = sinon.spy();
const wrapper = shallow(<DatePicker onCloseModal={onCloseModal}/>);
const datePicker = wrapper.instance();
datePicker.setModalVisible = setModalVisible;
datePicker.datePicked = datePicked;
Expand All @@ -194,6 +197,7 @@ describe('DatePicker:', () => {

expect(setModalVisible.calledWith(false)).to.equal(true);
expect(datePicked.callCount).to.equal(1);
expect(onCloseModal.callCount).to.equal(1);
});

it('getDate', () => {
Expand Down Expand Up @@ -277,7 +281,10 @@ describe('DatePicker:', () => {
it('onPressDate', () => {
Platform.OS = 'ios';
const setModalVisible = sinon.spy();
const wrapper = shallow(<DatePicker date="2016-05-06" minDate="2016-04-01" maxDate="2016-06-01"/>);
const onOpenModal = sinon.spy();
const wrapper = shallow(
<DatePicker date="2016-05-06" minDate="2016-04-01" maxDate="2016-06-01" onOpenModal={onOpenModal}/>
);
const datePicker = wrapper.instance();
datePicker.setModalVisible = setModalVisible;

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

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

expect(datePicker.getTitleElement().props.children).to.equal(datePicker.getDateStr());
});

it('`date` prop changes', () => {
const wrapper = mount(<DatePicker date="2016-06-04" />);

expect(wrapper.state('date')).to.deep.equal(new Date(2016, 5, 4));

wrapper.setProps({date: '2016-06-05'});

expect(wrapper.state('date')).to.deep.equal(new Date(2016, 5, 5));
});
});


Expand Down

0 comments on commit 3c8099b

Please sign in to comment.