Skip to content
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

[FlatButton] Add a condition to check for zero in the label warning #4618

Merged
merged 1 commit into from
Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions src/DatePicker/DatePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,24 @@
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';
import TextField from '../TextField';

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.deepEqual(
consoleStub.args[0][0].split('\n'),
[
'Warning: Failed prop type: Invalid prop `value` of type `string` ' +
'supplied to `DatePicker`, expected `object`.',
' in DatePicker',
]
describe('formatDate', () => {
it('should use the default format', () => {
const initialDate = new Date(1448967059892); // Tue, 01 Dec 2015 10:50:59 GMT
const wrapper = shallowWithContext(
<DatePicker value={initialDate} />
);
});

it('should not throw when using a valid properties', () => {
shallowWithContext(
<DatePicker value={new Date()} />
);
assert.strictEqual(consoleStub.callCount, 0);
assert.strictEqual(wrapper.find(TextField).props().value, '2015-12-01',
'should format the date to ISO 8601 (YYYY-MM-DD)');
});
});
});
2 changes: 1 addition & 1 deletion src/FlatButton/FlatButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FlatButtonLabel from './FlatButtonLabel';

function validateLabel(props, propName, componentName) {
if (process.env.NODE_ENV !== 'production') {
if (!props.children && !props.label && !props.icon) {
if (!props.children && (props.label !== 0 && !props.label) && !props.icon) {
return new Error(`Required prop label or children or icon was not specified in ${componentName}.`);
}
}
Expand Down
39 changes: 11 additions & 28 deletions src/FlatButton/FlatButton.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';
import {stub} from 'sinon';

import FlatButton from './FlatButton';
import getMuiTheme from '../styles/getMuiTheme';
import ActionAndroid from '../svg-icons/action/android';
Expand Down Expand Up @@ -150,37 +150,20 @@ describe('<FlatButton />', () => {
assert.strictEqual(wrapper.node.props.touchRippleColor, 'yellow', 'should be yellow');
});

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

beforeEach(() => {
consoleStub = stub(console, 'error');
});
describe('validateLabel', () => {
const validateLabel = FlatButton.propTypes.label;

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

it('should throw when using wrong properties', () => {
shallowWithContext(
<FlatButton />
);
assert.strictEqual(consoleStub.callCount, 1);
assert.deepEqual(
consoleStub.args[0][0].split('\n'),
[
'Warning: Failed prop type: Required prop label or children or ' +
'icon was not specified in FlatButton.',
' in FlatButton',
]
it('should throw when using wrong label', () => {
assert.strictEqual(validateLabel({}, 'label', 'FlatButton').message,
'Required prop label or children or icon was not specified in FlatButton.',
'should return an error'
);
});

it('should not throw when using a valid properties', () => {
shallowWithContext(
<FlatButton label={0} />
);
assert.strictEqual(consoleStub.callCount, 0);
it('should not throw when using a valid label', () => {
assert.strictEqual(validateLabel({
label: 0,
}, 'label', 'FlatButton'), undefined);
});
});
});
2 changes: 1 addition & 1 deletion src/RaisedButton/RaisedButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Paper from '../Paper';

function validateLabel(props, propName, componentName) {
if (process.env.NODE_ENV !== 'production') {
if (!props.children && !props.label && !props.icon) {
if (!props.children && (props.label !== 0 && !props.label) && !props.icon) {
return new Error(`Required prop label or children or icon was not specified in ${componentName}.`);
}
}
Expand Down
39 changes: 11 additions & 28 deletions src/RaisedButton/RaisedButton.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import {mount, shallow} from 'enzyme';
import {stub} from 'sinon';
import {assert} from 'chai';

import RaisedButton from './RaisedButton';
import ActionAndroid from '../svg-icons/action/android';
import getMuiTheme from '../styles/getMuiTheme';
Expand Down Expand Up @@ -115,37 +115,20 @@ describe('<RaisedButton />', () => {
assert.strictEqual(svgIcon.node.props.color, 'red', 'should have color set as the prop');
});

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

beforeEach(() => {
consoleStub = stub(console, 'error');
});
describe('validateLabel', () => {
const validateLabel = RaisedButton.propTypes.label;

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

it('should throw when using wrong properties', () => {
shallowWithContext(
<RaisedButton />
);
assert.strictEqual(consoleStub.callCount, 1);
assert.deepEqual(
consoleStub.args[0][0].split('\n'),
[
'Warning: Failed prop type: Required prop label or children or ' +
'icon was not specified in RaisedButton.',
' in RaisedButton',
]
it('should throw when using wrong label', () => {
assert.strictEqual(validateLabel({}, 'label', 'RaisedButton').message,
'Required prop label or children or icon was not specified in RaisedButton.',
'should return an error'
);
});

it('should not throw when using a valid properties', () => {
shallowWithContext(
<RaisedButton label={0} />
);
assert.strictEqual(consoleStub.callCount, 0);
it('should not throw when using a valid label', () => {
assert.strictEqual(validateLabel({
label: 0,
}, 'label', 'RaisedButton'), undefined);
});
});
});
42 changes: 4 additions & 38 deletions src/TimePicker/TimePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';
import {stub} from 'sinon';

import TimePicker from './TimePicker';
import {addHours, formatTime} from './timeUtils';
import getMuiTheme from '../styles/getMuiTheme';
import TextField from '../TextField';

describe('<TimePicker />', () => {
const muiTheme = getMuiTheme();
Expand All @@ -25,7 +25,7 @@ describe('<TimePicker />', () => {
/>
);

assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(valueTime));
assert.strictEqual(wrapper.find(TextField).props().value, formatTime(valueTime));
});

it('takes defaulTime prop to set first value when value prop is missing', () => {
Expand All @@ -35,7 +35,7 @@ describe('<TimePicker />', () => {
<TimePicker format="ampm" locale="en-US" defaultTime={initialTime} />
);

assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(initialTime));
assert.strictEqual(wrapper.find(TextField).props().value, formatTime(initialTime));
});

it('shows value prop if defaultTime is missing', () => {
Expand All @@ -51,40 +51,6 @@ describe('<TimePicker />', () => {
/>
);

assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(valueTime));
});

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.deepEqual(
consoleStub.args[0][0].split('\n'),
[
'Warning: Failed prop type: Invalid prop `value` of type `string`' +
' supplied to `TimePicker`, expected `object`.',
' in TimePicker',
]
);
});

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