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

Media Utils: Add tests for new error behavior #69215

Merged
merged 2 commits into from
Feb 17, 2025
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
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
Loading