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
10 changes: 8 additions & 2 deletions packages/block-library/src/playlist-track/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,7 +140,7 @@ const PlaylistTrackEdit = ( {
imageButton.current.focus();
}

if ( ! src && ! temporaryURL ) {
if ( ! hasTrackSource ) {
return (
<div { ...blockProps }>
<MediaPlaceholder
Expand Down
61 changes: 47 additions & 14 deletions packages/block-library/src/playlist-track/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useDispatch } from '@wordpress/data';
* Internal dependencies
*/
import PlaylistTrackEdit from '../edit';
import { PlaylistContext } from '../../playlist/context';

jest.mock( '@wordpress/block-editor', () => ( {
BlockControls: ( { children } ) => <div>{ children }</div>,
Expand Down Expand Up @@ -70,25 +71,33 @@ const defaultAttributes = {

function renderEdit( props = {} ) {
const setAttributes = jest.fn();
const setCurrentTrackClientId = props.setCurrentTrackClientId || jest.fn();

render(
<PlaylistTrackEdit
attributes={ {
...defaultAttributes,
...props.attributes,
<PlaylistContext.Provider
value={ {
currentTrackClientId: props.currentTrackClientId ?? null,
setCurrentTrackClientId,
} }
setAttributes={ setAttributes }
context={ {
showArtists: true,
showImages: true,
...props.context,
} }
clientId="playlist-track-client-id"
isSelected={ false }
/>
>
<PlaylistTrackEdit
attributes={ {
...defaultAttributes,
...props.attributes,
} }
setAttributes={ setAttributes }
context={ {
showArtists: true,
showImages: true,
...props.context,
} }
clientId={ props.clientId || 'playlist-track-client-id' }
isSelected={ props.isSelected ?? false }
/>
</PlaylistContext.Provider>
);

return { setAttributes };
return { setAttributes, setCurrentTrackClientId };
}

describe( 'PlaylistTrackEdit', () => {
Expand Down Expand Up @@ -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();
} );
} );
Loading