Skip to content

Commit

Permalink
Media Utils: Add tests for new error behavior (#69215)
Browse files Browse the repository at this point in the history
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
3 people authored Feb 17, 2025
1 parent eeb0d1b commit abeb109
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
64 changes: 44 additions & 20 deletions packages/media-utils/src/utils/test/upload-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ describe( 'uploadMedia', () => {
jest.clearAllMocks();
} );

it( 'should do nothing on no files', async () => {
it( 'should do nothing on no files', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
filesList: [],
onError,
onFileChange,
Expand All @@ -39,10 +39,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should error if allowedTypes contains a partial mime type and the validation fails', async () => {
it( 'should error if allowedTypes contains a partial mime type and the validation fails', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image' ],
filesList: [ xmlFile ],
onError,
Expand All @@ -60,10 +60,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should error if allowedTypes contains a complete mime type and the validation fails', async () => {
it( 'should error if allowedTypes contains a complete mime type and the validation fails', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image/gif' ],
filesList: [ imageFile ],
onError,
Expand All @@ -81,10 +81,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should work if allowedTypes contains a complete mime type and the validation succeeds', async () => {
it( 'should work if allowedTypes contains a complete mime type and the validation succeeds', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image/jpeg' ],
filesList: [ imageFile ],
onError,
Expand All @@ -96,10 +96,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).toHaveBeenCalled();
} );

it( 'should error if allowedTypes contains multiple types and the validation fails', async () => {
it( 'should error if allowedTypes contains multiple types and the validation fails', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'video', 'image' ],
filesList: [ xmlFile ],
onError,
Expand All @@ -117,10 +117,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should work if allowedTypes contains multiple types and the validation succeeds', async () => {
it( 'should work if allowedTypes contains multiple types and the validation succeeds', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'video', 'image' ],
filesList: [ imageFile ],
onError,
Expand All @@ -132,10 +132,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).toHaveBeenCalled();
} );

it( 'should only fail the invalid file and still allow others to succeed when uploading multiple files', async () => {
it( 'should only fail the invalid file and still allow others to succeed when uploading multiple files', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image' ],
filesList: [ imageFile, xmlFile ],
onError,
Expand All @@ -154,10 +154,10 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).toHaveBeenCalledTimes( 1 );
} );

it( 'should error if the file size is greater than the maximum', async () => {
it( 'should error if the file size is greater than the maximum', () => {
const onError = jest.fn();
const onFileChange = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image' ],
filesList: [ imageFile ],
maxUploadFileSize: 1,
Expand All @@ -177,9 +177,9 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should call error handler with the correct error object if file type is not allowed for user', async () => {
it( 'should call error handler with the correct error object if file type is not allowed for user', () => {
const onError = jest.fn();
await uploadMedia( {
uploadMedia( {
allowedTypes: [ 'image' ],
filesList: [ imageFile ],
onError,
Expand All @@ -197,9 +197,9 @@ describe( 'uploadMedia', () => {
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should throw error when multiple files are selected in single file upload mode', async () => {
it( 'should throw error when multiple files are selected in single file upload mode', () => {
const onError = jest.fn();
await uploadMedia( {
uploadMedia( {
filesList: [ imageFile, xmlFile ],
onError,
multiple: false,
Expand All @@ -210,4 +210,28 @@ describe( 'uploadMedia', () => {
);
expect( uploadToServer ).not.toHaveBeenCalled();
} );

it( 'should return error that is not an Error object', () => {
( uploadToServer as jest.Mock ).mockImplementation( () => {
throw {
code: 'fetch_error',
message: 'You are probably offline.',
};
} );

const onError = jest.fn();
uploadMedia( {
filesList: [ imageFile ],
onError,
multiple: false,
} );

expect( onError ).toHaveBeenCalledWith(
new UploadError( {
code: 'GENERAL',
message: 'You are probably offline.',
file: imageFile,
} )
);
} );
} );
1 change: 1 addition & 0 deletions packages/media-utils/src/utils/upload-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function uploadMedia( {
// Reset to empty on failure.
setAndUpdateFiles( index, null );

// @wordpress/api-fetch throws any response that isn't in the 200 range as-is.
let message: string;
if (
typeof error === 'object' &&
Expand Down

0 comments on commit abeb109

Please sign in to comment.