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

[CardMedia] Use propTypes for "at least one"-check #18384

Merged
merged 2 commits into from
Nov 15, 2019
Merged
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
14 changes: 7 additions & 7 deletions packages/material-ui/src/CardMedia/CardMedia.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import { chainPropTypes } from '@material-ui/utils';

export const styles = {
/* Styles applied to the root element. */
@@ -36,12 +37,6 @@ const CardMedia = React.forwardRef(function CardMedia(props, ref) {
...other
} = props;

if (process.env.NODE_ENV !== 'production') {
if (!children && !image && !src) {
console.error('Material-UI: either `children`, `image` or `src` prop must be specified.');
}
}

const isMediaComponent = MEDIA_COMPONENTS.indexOf(Component) !== -1;
const composedStyle =
!isMediaComponent && image ? { backgroundImage: `url("${image}")`, ...style } : style;
@@ -70,7 +65,12 @@ CardMedia.propTypes = {
/**
* The content of the component.
*/
children: PropTypes.node,
children: chainPropTypes(PropTypes.node, props => {
if (!props.children && !props.image && !props.src) {
return new Error('Material-UI: either `children`, `image` or `src` prop must be specified.');
}
return null;
}),
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
23 changes: 23 additions & 0 deletions packages/material-ui/src/CardMedia/CardMedia.test.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ import { assert } from 'chai';
import { createMount, findOutermostIntrinsic, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '../test-utils/describeConformance';
import CardMedia from './CardMedia';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import PropTypes from 'prop-types';

describe('<CardMedia />', () => {
let mount;
@@ -78,4 +80,25 @@ describe('<CardMedia />', () => {
assert.strictEqual(findOutermostIntrinsic(wrapper).props().src, undefined);
});
});

describe('warnings', () => {
before(() => {
consoleErrorMock.spy();
});

after(() => {
consoleErrorMock.reset();
PropTypes.resetWarningCache();
});

it('warns when neither `children`, nor `image`, nor `src` are provided', () => {
mount(<CardMedia />);

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.args()[0][0],
'Material-UI: either `children`, `image` or `src` prop must be specified.',
);
});
});
});