Skip to content
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
14 changes: 7 additions & 7 deletions packages/material-ui/src/CardMedia/CardMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions packages/material-ui/src/CardMedia/CardMedia.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.',
);
});
});
});