Skip to content

Commit 69f7046

Browse files
authored
[test] Use .checkPropTypes instead of render + propTypes (#20451)
1 parent c776d34 commit 69f7046

File tree

21 files changed

+137
-93
lines changed

21 files changed

+137
-93
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ module.exports = {
129129
// test to `beforeEach`.
130130
// `beforeEach`+`afterEach` also means that the `beforeEach`
131131
// is cleaned up in `afterEach` if the test causes a crash
132-
'mocha/no-hooks-for-single-case': 'off'
132+
'mocha/no-hooks-for-single-case': 'off',
133+
134+
// They are accessed to test custom validator implementation with PropTypes.checkPropTypes
135+
'react/forbid-foreign-prop-types': 'off',
133136
},
134137
},
135138
{

packages/material-ui-styles/src/styled/styled.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,21 @@ describe('styled', () => {
9393
describe('warnings', () => {
9494
beforeEach(() => {
9595
consoleErrorMock.spy();
96+
PropTypes.resetWarningCache();
9697
});
9798

9899
afterEach(() => {
99100
consoleErrorMock.reset();
100-
PropTypes.resetWarningCache();
101101
});
102102

103103
it('warns if it cant detect the secondary action properly', () => {
104-
mount(
105-
<StyledButton clone component="div">
106-
<div>Styled Components</div>
107-
</StyledButton>,
104+
PropTypes.checkPropTypes(
105+
StyledButton.propTypes,
106+
{ clone: true, component: 'div' },
107+
'prop',
108+
'StyledButton',
108109
);
110+
109111
assert.strictEqual(consoleErrorMock.callCount(), 1);
110112
assert.include(
111113
consoleErrorMock.messages()[0],

packages/material-ui-styles/src/withStyles/withStyles.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ describe('withStyles', () => {
7373
// describe('innerRef', () => {
7474
// beforeEach(() => {
7575
// consoleErrorMock.spy();
76+
// PropTypes.resetWarningCache();
7677
// });
7778

7879
// afterEach(() => {
7980
// consoleErrorMock.reset();
80-
// PropTypes.resetWarningCache();
8181
// });
8282

8383
// it('is deprecated', () => {

packages/material-ui-styles/src/withTheme/withTheme.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,21 @@ describe('withTheme', () => {
8585
describe('innerRef', () => {
8686
beforeEach(() => {
8787
consoleErrorMock.spy();
88+
PropTypes.resetWarningCache();
8889
});
8990

9091
afterEach(() => {
9192
consoleErrorMock.reset();
92-
PropTypes.resetWarningCache();
9393
});
9494

9595
it('is deprecated', () => {
9696
const ThemedDiv = withTheme('div');
97-
98-
mount(<ThemedDiv innerRef={React.createRef()} />);
97+
PropTypes.checkPropTypes(
98+
ThemedDiv.propTypes,
99+
{ innerRef: React.createRef() },
100+
'prop',
101+
'ThemedDiv',
102+
);
99103

100104
assert.strictEqual(consoleErrorMock.callCount(), 1);
101105
assert.include(

packages/material-ui-utils/src/chainPropTypes.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('chainPropTypes', () => {
1111

1212
beforeEach(() => {
1313
consoleErrorMock.spy();
14+
PropTypes.resetWarningCache();
1415
});
1516

1617
afterEach(() => {

packages/material-ui-utils/src/elementAcceptingRef.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ describe('elementAcceptingRef', () => {
2929

3030
beforeEach(() => {
3131
consoleErrorMock.spy();
32+
PropTypes.resetWarningCache();
3233
});
3334

3435
afterEach(() => {
3536
consoleErrorMock.reset();
36-
PropTypes.resetWarningCache();
3737
});
3838

3939
describe('acceptance when not required', () => {

packages/material-ui-utils/src/elementTypeAcceptingRef.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ describe('elementTypeAcceptingRef', () => {
2929

3030
beforeEach(() => {
3131
consoleErrorMock.spy();
32+
PropTypes.resetWarningCache();
3233
});
3334

3435
afterEach(() => {
3536
consoleErrorMock.reset();
36-
PropTypes.resetWarningCache();
3737
});
3838

3939
describe('acceptance', () => {

packages/material-ui/src/ButtonBase/ButtonBase.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,11 @@ describe('<ButtonBase />', () => {
908908
describe('warnings', () => {
909909
beforeEach(() => {
910910
consoleErrorMock.spy();
911+
PropTypes.resetWarningCache();
911912
});
912913

913914
afterEach(() => {
914915
consoleErrorMock.reset();
915-
PropTypes.resetWarningCache();
916916
});
917917

918918
it('warns on invalid `component` prop: ref forward', () => {
@@ -929,11 +929,16 @@ describe('<ButtonBase />', () => {
929929
return <button type="button" {...props} />;
930930
}
931931

932-
// cant match the error message here because flakiness with mocha watchmode
933-
render(<ButtonBase component={Component} />);
932+
PropTypes.checkPropTypes(
933+
// @ts-ignore `Naked` is internal
934+
ButtonBase.Naked.propTypes,
935+
{ classes: {}, component: Component },
936+
'prop',
937+
'MockedName',
938+
);
934939

935940
expect(consoleErrorMock.messages()[0]).to.include(
936-
'Invalid prop `component` supplied to `ForwardRef(ButtonBase)`. Expected an element type that can hold a ref',
941+
'Invalid prop `component` supplied to `MockedName`. Expected an element type that can hold a ref',
937942
);
938943
});
939944

packages/material-ui/src/CardMedia/CardMedia.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ describe('<CardMedia />', () => {
8484
describe('warnings', () => {
8585
before(() => {
8686
consoleErrorMock.spy();
87+
PropTypes.resetWarningCache();
8788
});
8889

8990
after(() => {
9091
consoleErrorMock.reset();
91-
PropTypes.resetWarningCache();
9292
});
9393

9494
it('warns when neither `children`, nor `image`, nor `src`, nor `component` are provided', () => {
95-
mount(<CardMedia />);
95+
PropTypes.checkPropTypes(CardMedia.Naked.propTypes, { classes: {} }, 'prop', 'MockedName');
9696

9797
assert.strictEqual(consoleErrorMock.callCount(), 1);
9898
assert.include(

packages/material-ui/src/ExpansionPanel/ExpansionPanel.test.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,33 @@ describe('<ExpansionPanel />', () => {
137137
describe('first child', () => {
138138
beforeEach(() => {
139139
consoleErrorMock.spy();
140+
PropTypes.resetWarningCache();
140141
});
141142

142143
afterEach(() => {
143144
consoleErrorMock.reset();
144-
PropTypes.resetWarningCache();
145145
});
146146

147147
it('requires at least one child', () => {
148-
if (!/jsdom/.test(window.navigator.userAgent)) {
149-
// errors during mount are not caught by try-catch in the browser
150-
// can't use skip since this causes multiple errors related to cleanup of the console mock
151-
return;
152-
}
153-
154-
assert.throws(() => mount(<ExpansionPanel>{[]}</ExpansionPanel>));
155-
assert.strictEqual(consoleErrorMock.callCount(), 3);
148+
PropTypes.checkPropTypes(
149+
ExpansionPanel.Naked.propTypes,
150+
{ classes: {}, children: [] },
151+
'prop',
152+
'MockedName',
153+
);
154+
155+
assert.strictEqual(consoleErrorMock.callCount(), 1);
156156
assert.include(consoleErrorMock.messages()[0], 'Material-UI: expected the first child');
157157
});
158158

159159
it('needs a valid element as the first child', () => {
160-
mount(
161-
<ExpansionPanel>
162-
<React.Fragment />
163-
</ExpansionPanel>,
160+
PropTypes.checkPropTypes(
161+
ExpansionPanel.Naked.propTypes,
162+
{ classes: {}, children: <React.Fragment /> },
163+
'prop',
164+
'MockedName',
164165
);
166+
165167
assert.strictEqual(consoleErrorMock.callCount(), 1);
166168
assert.include(
167169
consoleErrorMock.messages()[0],

0 commit comments

Comments
 (0)