Skip to content

Commit 2b0b60d

Browse files
authored
Merge pull request #4618 from oliviertassinari/test-stub-console
[FlatButton] Add a condition to check for zero in the label warning
2 parents b814008 + b63c360 commit 2b0b60d

6 files changed

Lines changed: 37 additions & 126 deletions

File tree

src/DatePicker/DatePicker.spec.js

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,24 @@
22
import React from 'react';
33
import {shallow} from 'enzyme';
44
import {assert} from 'chai';
5-
import {stub} from 'sinon';
5+
66
import DatePicker from './DatePicker';
77
import getMuiTheme from '../styles/getMuiTheme';
8+
import TextField from '../TextField';
89

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

13-
describe('propTypes', () => {
14-
let consoleStub;
15-
16-
beforeEach(() => {
17-
consoleStub = stub(console, 'error');
18-
});
19-
20-
afterEach(() => {
21-
console.error.restore(); // eslint-disable-line no-console
22-
});
23-
24-
it('should throw when using wrong properties', () => {
25-
shallowWithContext(
26-
<DatePicker value="2016-03-21" />
27-
);
28-
assert.strictEqual(consoleStub.callCount, 1);
29-
assert.deepEqual(
30-
consoleStub.args[0][0].split('\n'),
31-
[
32-
'Warning: Failed prop type: Invalid prop `value` of type `string` ' +
33-
'supplied to `DatePicker`, expected `object`.',
34-
' in DatePicker',
35-
]
14+
describe('formatDate', () => {
15+
it('should use the default format', () => {
16+
const initialDate = new Date(1448967059892); // Tue, 01 Dec 2015 10:50:59 GMT
17+
const wrapper = shallowWithContext(
18+
<DatePicker value={initialDate} />
3619
);
37-
});
3820

39-
it('should not throw when using a valid properties', () => {
40-
shallowWithContext(
41-
<DatePicker value={new Date()} />
42-
);
43-
assert.strictEqual(consoleStub.callCount, 0);
21+
assert.strictEqual(wrapper.find(TextField).props().value, '2015-12-01',
22+
'should format the date to ISO 8601 (YYYY-MM-DD)');
4423
});
4524
});
4625
});

src/FlatButton/FlatButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import FlatButtonLabel from './FlatButtonLabel';
77

88
function validateLabel(props, propName, componentName) {
99
if (process.env.NODE_ENV !== 'production') {
10-
if (!props.children && !props.label && !props.icon) {
10+
if (!props.children && (props.label !== 0 && !props.label) && !props.icon) {
1111
return new Error(`Required prop label or children or icon was not specified in ${componentName}.`);
1212
}
1313
}

src/FlatButton/FlatButton.spec.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React from 'react';
33
import {shallow} from 'enzyme';
44
import {assert} from 'chai';
5-
import {stub} from 'sinon';
5+
66
import FlatButton from './FlatButton';
77
import getMuiTheme from '../styles/getMuiTheme';
88
import ActionAndroid from '../svg-icons/action/android';
@@ -150,37 +150,20 @@ describe('<FlatButton />', () => {
150150
assert.strictEqual(wrapper.node.props.touchRippleColor, 'yellow', 'should be yellow');
151151
});
152152

153-
describe('propTypes', () => {
154-
let consoleStub;
155-
156-
beforeEach(() => {
157-
consoleStub = stub(console, 'error');
158-
});
153+
describe('validateLabel', () => {
154+
const validateLabel = FlatButton.propTypes.label;
159155

160-
afterEach(() => {
161-
console.error.restore(); // eslint-disable-line no-console
162-
});
163-
164-
it('should throw when using wrong properties', () => {
165-
shallowWithContext(
166-
<FlatButton />
167-
);
168-
assert.strictEqual(consoleStub.callCount, 1);
169-
assert.deepEqual(
170-
consoleStub.args[0][0].split('\n'),
171-
[
172-
'Warning: Failed prop type: Required prop label or children or ' +
173-
'icon was not specified in FlatButton.',
174-
' in FlatButton',
175-
]
156+
it('should throw when using wrong label', () => {
157+
assert.strictEqual(validateLabel({}, 'label', 'FlatButton').message,
158+
'Required prop label or children or icon was not specified in FlatButton.',
159+
'should return an error'
176160
);
177161
});
178162

179-
it('should not throw when using a valid properties', () => {
180-
shallowWithContext(
181-
<FlatButton label={0} />
182-
);
183-
assert.strictEqual(consoleStub.callCount, 0);
163+
it('should not throw when using a valid label', () => {
164+
assert.strictEqual(validateLabel({
165+
label: 0,
166+
}, 'label', 'FlatButton'), undefined);
184167
});
185168
});
186169
});

src/RaisedButton/RaisedButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Paper from '../Paper';
77

88
function validateLabel(props, propName, componentName) {
99
if (process.env.NODE_ENV !== 'production') {
10-
if (!props.children && !props.label && !props.icon) {
10+
if (!props.children && (props.label !== 0 && !props.label) && !props.icon) {
1111
return new Error(`Required prop label or children or icon was not specified in ${componentName}.`);
1212
}
1313
}

src/RaisedButton/RaisedButton.spec.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44
import TestUtils from 'react-addons-test-utils';
55
import {mount, shallow} from 'enzyme';
6-
import {stub} from 'sinon';
76
import {assert} from 'chai';
7+
88
import RaisedButton from './RaisedButton';
99
import ActionAndroid from '../svg-icons/action/android';
1010
import getMuiTheme from '../styles/getMuiTheme';
@@ -115,37 +115,20 @@ describe('<RaisedButton />', () => {
115115
assert.strictEqual(svgIcon.node.props.color, 'red', 'should have color set as the prop');
116116
});
117117

118-
describe('propTypes', () => {
119-
let consoleStub;
120-
121-
beforeEach(() => {
122-
consoleStub = stub(console, 'error');
123-
});
118+
describe('validateLabel', () => {
119+
const validateLabel = RaisedButton.propTypes.label;
124120

125-
afterEach(() => {
126-
console.error.restore(); // eslint-disable-line no-console
127-
});
128-
129-
it('should throw when using wrong properties', () => {
130-
shallowWithContext(
131-
<RaisedButton />
132-
);
133-
assert.strictEqual(consoleStub.callCount, 1);
134-
assert.deepEqual(
135-
consoleStub.args[0][0].split('\n'),
136-
[
137-
'Warning: Failed prop type: Required prop label or children or ' +
138-
'icon was not specified in RaisedButton.',
139-
' in RaisedButton',
140-
]
121+
it('should throw when using wrong label', () => {
122+
assert.strictEqual(validateLabel({}, 'label', 'RaisedButton').message,
123+
'Required prop label or children or icon was not specified in RaisedButton.',
124+
'should return an error'
141125
);
142126
});
143127

144-
it('should not throw when using a valid properties', () => {
145-
shallowWithContext(
146-
<RaisedButton label={0} />
147-
);
148-
assert.strictEqual(consoleStub.callCount, 0);
128+
it('should not throw when using a valid label', () => {
129+
assert.strictEqual(validateLabel({
130+
label: 0,
131+
}, 'label', 'RaisedButton'), undefined);
149132
});
150133
});
151134
});

src/TimePicker/TimePicker.spec.js

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import React from 'react';
33
import {shallow} from 'enzyme';
44
import {assert} from 'chai';
5-
import {stub} from 'sinon';
65

76
import TimePicker from './TimePicker';
87
import {addHours, formatTime} from './timeUtils';
98
import getMuiTheme from '../styles/getMuiTheme';
9+
import TextField from '../TextField';
1010

1111
describe('<TimePicker />', () => {
1212
const muiTheme = getMuiTheme();
@@ -25,7 +25,7 @@ describe('<TimePicker />', () => {
2525
/>
2626
);
2727

28-
assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(valueTime));
28+
assert.strictEqual(wrapper.find(TextField).props().value, formatTime(valueTime));
2929
});
3030

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

38-
assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(initialTime));
38+
assert.strictEqual(wrapper.find(TextField).props().value, formatTime(initialTime));
3939
});
4040

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

54-
assert.strictEqual(wrapper.find('TextField').prop('value'), formatTime(valueTime));
55-
});
56-
57-
describe('propTypes', () => {
58-
let consoleStub;
59-
60-
beforeEach(() => {
61-
consoleStub = stub(console, 'error');
62-
});
63-
64-
afterEach(() => {
65-
console.error.restore(); // eslint-disable-line no-console
66-
});
67-
68-
it('should throw when using wrong properties', () => {
69-
shallowWithContext(
70-
<TimePicker value="2016-03-21" />
71-
);
72-
assert.strictEqual(consoleStub.callCount, 1);
73-
assert.deepEqual(
74-
consoleStub.args[0][0].split('\n'),
75-
[
76-
'Warning: Failed prop type: Invalid prop `value` of type `string`' +
77-
' supplied to `TimePicker`, expected `object`.',
78-
' in TimePicker',
79-
]
80-
);
81-
});
82-
83-
it('should not throw when using a valid properties', () => {
84-
shallowWithContext(
85-
<TimePicker value={new Date()} />
86-
);
87-
assert.strictEqual(consoleStub.callCount, 0);
88-
});
54+
assert.strictEqual(wrapper.find(TextField).props().value, formatTime(valueTime));
8955
});
9056
});

0 commit comments

Comments
 (0)