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
19 changes: 17 additions & 2 deletions packages/block-library/src/playlist-track/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const PlaylistTrackEdit = ( {
const showImages = context?.showImages ?? true;
const imageButton = useRef();
const blockProps = useBlockProps();
const { currentTrackClientId, setCurrentTrackClientId } =
const { currentTrackClientId, setCurrentTrackClientId, addTracks } =
useContext( PlaylistContext );
const { createErrorNotice } = useDispatch( noticesStore );
function onUploadError( message ) {
Expand All @@ -78,7 +78,7 @@ const PlaylistTrackEdit = ( {
] );

useUploadMediaFromBlobURL( {
src: temporaryURL,
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectTrack,
onError: onUploadError,
Expand Down Expand Up @@ -158,8 +158,23 @@ const PlaylistTrackEdit = ( {
mediaURL={ src }
allowedTypes={ ALLOWED_MEDIA_TYPES }
onError={ onUploadError }
variant="toolbar"
/>
</BlockControls>
{ !! addTracks && (
<BlockControls group="block">
<MediaReplaceFlow
name={ __( 'Add' ) }
onSelect={ addTracks }
accept="audio/*"
multiple
handleUpload={ false }
allowedTypes={ ALLOWED_MEDIA_TYPES }
onError={ onUploadError }
variant="toolbar"
/>
</BlockControls>
) }
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<TextControl
Expand Down
55 changes: 52 additions & 3 deletions packages/block-library/src/playlist-track/test/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { fireEvent, render, screen } from '@testing-library/react';
import { fireEvent, render, screen, within } from '@testing-library/react';

/**
* WordPress dependencies
Expand All @@ -13,13 +13,18 @@ import { useDispatch } from '@wordpress/data';
*/
import PlaylistTrackEdit from '../edit';
import { PlaylistContext } from '../../playlist/context';
import { useUploadMediaFromBlobURL } from '../../utils/hooks';

jest.mock( '@wordpress/block-editor', () => ( {
BlockControls: ( { children } ) => <div>{ children }</div>,
BlockControls: ( { children, group = 'default' } ) => (
<div data-testid={ `block-controls-${ group }` }>{ children }</div>
),
BlockIcon: () => <span />,
InspectorControls: ( { children } ) => <div>{ children }</div>,
MediaPlaceholder: () => <div />,
MediaReplaceFlow: () => <div />,
MediaReplaceFlow: ( { name, onSelect } ) => (
<button onClick={ () => onSelect( {} ) }>{ name }</button>
),
MediaUpload: ( { render: renderMediaUpload } ) =>
renderMediaUpload( { open: jest.fn() } ),
MediaUploadCheck: ( { children } ) => <div>{ children }</div>,
Expand Down Expand Up @@ -71,12 +76,14 @@ const defaultAttributes = {
function renderEdit( props = {} ) {
const setAttributes = jest.fn();
const setCurrentTrackClientId = props.setCurrentTrackClientId || jest.fn();
const addTracks = props.addTracks;

render(
<PlaylistContext.Provider
value={ {
currentTrackClientId: props.currentTrackClientId ?? null,
setCurrentTrackClientId,
addTracks,
} }
>
<PlaylistTrackEdit
Expand Down Expand Up @@ -104,6 +111,7 @@ describe( 'PlaylistTrackEdit', () => {
useDispatch.mockReturnValue( {
createErrorNotice: jest.fn(),
} );
useUploadMediaFromBlobURL.mockClear();
} );

it( 'allows the track image alternative text to be edited', () => {
Expand Down Expand Up @@ -166,4 +174,45 @@ describe( 'PlaylistTrackEdit', () => {

expect( setCurrentTrackClientId ).not.toHaveBeenCalled();
} );

it( 'uploads temporary blob tracks', () => {
renderEdit( {
attributes: {
blob: 'blob:https://example.com/temporary-track',
src: undefined,
},
} );

expect( useUploadMediaFromBlobURL ).toHaveBeenCalledWith(
expect.objectContaining( {
url: 'blob:https://example.com/temporary-track',
} )
);
} );

it( 'allows tracks to be added from the track toolbar', () => {
const addTracks = jest.fn();
renderEdit( { addTracks } );

fireEvent.click( screen.getByRole( 'button', { name: 'Add' } ) );

expect( addTracks ).toHaveBeenCalledWith( {} );
} );

it( 'renders the add track control in a different toolbar group from replace', () => {
renderEdit( { addTracks: jest.fn() } );

expect(
within( screen.getByTestId( 'block-controls-other' ) ).getByRole(
'button',
{ name: 'Replace' }
)
).toBeInTheDocument();
expect(
within( screen.getByTestId( 'block-controls-block' ) ).getByRole(
'button',
{ name: 'Add' }
)
).toBeInTheDocument();
} );
} );
1 change: 1 addition & 0 deletions packages/block-library/src/playlist/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import { createContext } from '@wordpress/element';
export const PlaylistContext = createContext( {
currentTrackClientId: null,
setCurrentTrackClientId: () => {},
addTracks: undefined,
} );
Loading
Loading