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

Only check MediaPlaceholder allowedTypes array length if prop exists #11694

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class MediaPlaceholder extends Component {
let instructions = labels.instructions || '';
let title = labels.title || '';
if ( ! instructions || ! title ) {
const isOneType = 1 === allowedTypes.length;
const isOneType = 1 === allowedTypes && allowedTypes.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably wanted to do this:

Suggested change
const isOneType = 1 === allowedTypes && allowedTypes.length;
const isOneType = allowedTypes && 1 === allowedTypes.length;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about defining allowedTypes as an empty array by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you are on the boolean 🤦‍♂️ And I considered initializing allowedTypes to [] in the destructuring assignment, but then backed off because we do a if ( ! this.props.allowedTypes ) check elsewhere. Upon reflection, since the default initialization won't mutate the value of the prop, the two approaches aren't incompatible and I think your suggestion's the way to go. Will push a commit momentarily.

const isAudio = isOneType && 'audio' === allowedTypes[ 0 ];
const isImage = isOneType && 'image' === allowedTypes[ 0 ];
const isVideo = isOneType && 'video' === allowedTypes[ 0 ];
Expand Down
17 changes: 17 additions & 0 deletions packages/editor/src/components/media-placeholder/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';

/**
* Internal dependencies
*/
import MediaPlaceholder from '../';

describe( 'MediaPlaceholder', () => {
it( 'renders successfully when allowedTypes property is not specified', () => {
expect( () => mount(
<MediaPlaceholder />
) ).not.toThrow();
} );
} );