Files
iNaturalistReactNative/tests/unit/uploaders/utils/errorHandling.test.js
Ryan Stelly b78be9243d lint rule & autofix for "trailing comma" (#3299)
* (lint) MOB-1063 enforce trailing commas

* autofix trailing commas

* manually fix newly introduced maxlen violations

* add trailing comma convention to i18n build
2025-12-22 20:17:13 -06:00

105 lines
3.2 KiB
JavaScript

import { handleUploadError } from "uploaders";
import { RecoverableError, RECOVERY_BY } from "uploaders/utils/errorHandling";
describe( "errorHandling", ( ) => {
// Mock translation function since we're passing that into the error handler
const t = jest.fn( key => key );
describe( "handleUploadError", ( ) => {
beforeEach( ( ) => {
jest.clearAllMocks( );
} );
test( "should throw basic errors without special handling", ( ) => {
const error = new Error( "Basic error" );
expect( ( ) => {
handleUploadError( error, t );
} ).toThrow( "Basic error" );
} );
test( "should return 'Connection problem' message for network errors", ( ) => {
const networkError = new Error( "Network request failed" );
const { message } = handleUploadError( networkError, t );
expect( message ).toBe( "Connection-problem-Please-try-again-later" );
} );
test( "should return recoveryPossible=true for network errors", () => {
const networkError = new Error( "Network request failed" );
const { recoveryPossible } = handleUploadError( networkError, t );
expect( recoveryPossible ).toBe( true );
} );
test( "should return recovery options for RecoverableErrors", () => {
const recoverableError = new RecoverableError( "Recoverable error" );
recoverableError.recoveryBy = RECOVERY_BY.LOGIN_AGAIN;
const { recoveryPossible, recoveryBy } = handleUploadError(
recoverableError,
t,
);
expect( recoveryPossible ).toBe( true );
expect( recoveryBy ).toBe( RECOVERY_BY.LOGIN_AGAIN );
} );
test( "should return a string from an array of iNaturalist API errors", ( ) => {
const apiError = new Error( "API Upload Error" );
apiError.json = {
errors: [
{ message: "First error" },
{ message: "Second error" },
],
};
const { message } = handleUploadError( apiError, t );
expect( message ).toBe( "First error, Second error" );
} );
test( "should return a string from a nested array of iNaturalist API errors", ( ) => {
const apiError = new Error( "API Error" );
apiError.json = {
errors: [
{ message: { errors: ["Error 1", "Error 2"] } },
],
};
const { message } = handleUploadError( apiError, t );
expect( message ).toBe( "Error 1, Error 2" );
} );
test( "should return a string from a nested string "
+ "within iNaturalist API error messages", ( ) => {
const apiError = new Error( "API Error" );
apiError.json = {
errors: [
{ message: { errors: "Nested error message string" } },
],
};
const { message } = handleUploadError( apiError, t );
expect( message ).toBe( "Nested error message string" );
} );
test( "should handle 410 errors for previously deleted observations or evidence", ( ) => {
const apiError = new Error( "API Error" );
apiError.json = {
errors: [
{ message: { error: "Resource was previously deleted" } },
],
};
const { message } = handleUploadError( apiError, t );
expect( message ).toBe( "Resource was previously deleted" );
} );
} );
} );