Skip to content

Commit

Permalink
Merge pull request #4347 from oliviertassinari/picker-improve-proptypes
Browse files Browse the repository at this point in the history
[Pickers] Add some test regarding the expect value property
  • Loading branch information
oliviertassinari committed May 26, 2016
2 parents 66565b9 + beb6f4a commit 6daf7ae
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class DatePicker extends Component {
/**
* Sets the date for the Date Picker programmatically.
*/
value: PropTypes.any,
value: PropTypes.object,
/**
* Wordings used inside the button of the dialog.
*/
Expand All @@ -147,7 +147,6 @@ class DatePicker extends Component {
style: {},
};


static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
Expand Down Expand Up @@ -210,21 +209,28 @@ class DatePicker extends Component {
date: date,
});
}
if (this.props.onChange) this.props.onChange(null, date);
if (this.props.onChange) {
this.props.onChange(null, date);
}
};

handleFocus = (event) => {
event.target.blur();
if (this.props.onFocus) this.props.onFocus(event);
if (this.props.onFocus) {
this.props.onFocus(event);
}
};

handleTouchTap = (event) => {
if (this.props.onTouchTap) this.props.onTouchTap(event);
if (this.props.onTouchTap) {
this.props.onTouchTap(event);
}

if (!this.props.disabled)
if (!this.props.disabled) {
setTimeout(() => {
this.openDialog();
}, 0);
}
};

isControlled() {
Expand Down
42 changes: 42 additions & 0 deletions src/DatePicker/DatePicker.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env mocha */
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';
import {stub} from 'sinon';
import DatePicker from './DatePicker';
import getMuiTheme from '../styles/getMuiTheme';

describe('<DatePicker />', () => {
const muiTheme = getMuiTheme();
const shallowWithContext = (node) => shallow(node, {context: {muiTheme}});

describe('propTypes', () => {
let consoleStub;

beforeEach(() => {
consoleStub = stub(console, 'error');
});

afterEach(() => {
console.error.restore(); // eslint-disable-line no-console
});

it('should throw when using wrong properties', () => {
shallowWithContext(
<DatePicker value="2016-03-21" />
);
assert.strictEqual(consoleStub.callCount, 1);
assert.strictEqual(
consoleStub.args[0][0],
'Warning: Failed propType: Invalid prop `value` of type `string` supplied to `DatePicker`, expected `object`.'
);
});

it('should not throw when using a valid properties', () => {
shallowWithContext(
<DatePicker value={new Date()} />
);
assert.strictEqual(consoleStub.callCount, 0);
});
});
});
13 changes: 9 additions & 4 deletions src/TimePicker/TimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class TimePicker extends Component {
* Sets the time for the Time Picker programmatically.
*/
value: PropTypes.object,

};

static defaultProps = {
Expand Down Expand Up @@ -155,15 +154,21 @@ class TimePicker extends Component {

handleFocusInput = (event) => {
event.target.blur();
if (this.props.onFocus) this.props.onFocus(event);
if (this.props.onFocus) {
this.props.onFocus(event);
}
};

handleTouchTapInput = (event) => {
event.preventDefault();

if (!this.props.disabled) this.openDialog();
if (!this.props.disabled) {
this.openDialog();
}

if (this.props.onTouchTap) this.props.onTouchTap(event);
if (this.props.onTouchTap) {
this.props.onTouchTap(event);
}
};

isControlled() {
Expand Down
42 changes: 42 additions & 0 deletions src/TimePicker/TimePicker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env mocha */
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';
import {stub} from 'sinon';
import TimePicker from './TimePicker';
import getMuiTheme from '../styles/getMuiTheme';

describe('<TimePicker />', () => {
const muiTheme = getMuiTheme();
const shallowWithContext = (node) => shallow(node, {context: {muiTheme}});

describe('propTypes', () => {
let consoleStub;

beforeEach(() => {
consoleStub = stub(console, 'error');
});

afterEach(() => {
console.error.restore(); // eslint-disable-line no-console
});

it('should throw when using wrong properties', () => {
shallowWithContext(
<TimePicker value="2016-03-21" />
);
assert.strictEqual(consoleStub.callCount, 1);
assert.strictEqual(
consoleStub.args[0][0],
'Warning: Failed propType: Invalid prop `value` of type `string` supplied to `TimePicker`, expected `object`.'
);
});

it('should not throw when using a valid properties', () => {
shallowWithContext(
<TimePicker value={new Date()} />
);
assert.strictEqual(consoleStub.callCount, 0);
});
});
});

0 comments on commit 6daf7ae

Please sign in to comment.