diff --git a/packages/block-library/src/playlist-track/edit.js b/packages/block-library/src/playlist-track/edit.js
index 35d425a6afc528..042ced34944589 100644
--- a/packages/block-library/src/playlist-track/edit.js
+++ b/packages/block-library/src/playlist-track/edit.js
@@ -59,13 +59,19 @@ const PlaylistTrackEdit = ( {
function onUploadError( message ) {
createErrorNotice( message, { type: 'snackbar' } );
}
+ const hasTrackSource = !! src || !! temporaryURL;
useEffect( () => {
- if ( isSelected && currentTrackClientId !== clientId ) {
+ if (
+ isSelected &&
+ hasTrackSource &&
+ currentTrackClientId !== clientId
+ ) {
setCurrentTrackClientId( clientId );
}
}, [
isSelected,
+ hasTrackSource,
clientId,
currentTrackClientId,
setCurrentTrackClientId,
@@ -134,7 +140,7 @@ const PlaylistTrackEdit = ( {
imageButton.current.focus();
}
- if ( ! src && ! temporaryURL ) {
+ if ( ! hasTrackSource ) {
return (
( {
BlockControls: ( { children } ) => { children }
,
@@ -70,25 +71,33 @@ const defaultAttributes = {
function renderEdit( props = {} ) {
const setAttributes = jest.fn();
+ const setCurrentTrackClientId = props.setCurrentTrackClientId || jest.fn();
render(
-
+ >
+
+
);
- return { setAttributes };
+ return { setAttributes, setCurrentTrackClientId };
}
describe( 'PlaylistTrackEdit', () => {
@@ -134,4 +143,28 @@ describe( 'PlaylistTrackEdit', () => {
screen.queryByLabelText( 'Alternative text' )
).not.toBeInTheDocument();
} );
+
+ it( 'sets the selected track as the current track', () => {
+ const { setCurrentTrackClientId } = renderEdit( {
+ currentTrackClientId: 'another-track-client-id',
+ isSelected: true,
+ } );
+
+ expect( setCurrentTrackClientId ).toHaveBeenCalledWith(
+ 'playlist-track-client-id'
+ );
+ } );
+
+ it( 'does not set a selected placeholder track as the current track', () => {
+ const { setCurrentTrackClientId } = renderEdit( {
+ attributes: {
+ blob: undefined,
+ src: undefined,
+ },
+ currentTrackClientId: 'another-track-client-id',
+ isSelected: true,
+ } );
+
+ expect( setCurrentTrackClientId ).not.toHaveBeenCalled();
+ } );
} );