diff --git a/src/utils/errorHandler.test.tsx b/src/utils/errorHandler.test.tsx index ae45a8a41f..00e5adc537 100644 --- a/src/utils/errorHandler.test.tsx +++ b/src/utils/errorHandler.test.tsx @@ -21,7 +21,11 @@ describe('Test if errorHandler is working properly', () => { return key; }; - it('should call toast.error with the correct message if error message is "Failed to fetch"', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => { const error = new Error('Failed to fetch'); errorHandler(t, error); @@ -31,7 +35,7 @@ describe('Test if errorHandler is working properly', () => { it('should call toast.error with the correct message if error message contains this substring "Value is not a valid phone number"', () => { const error = new Error('This value is not a valid phone number'); errorHandler(t, error); - + console.log(toast.error); expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber')); }); diff --git a/src/utils/errorHandler.tsx b/src/utils/errorHandler.tsx index ec6bbfe174..e4e543e940 100644 --- a/src/utils/errorHandler.tsx +++ b/src/utils/errorHandler.tsx @@ -5,7 +5,7 @@ import i18n from './i18n'; /** This function is used to handle api errors in the application. It takes in the error object and displays the error message to the user. - If the error is due to the Talawa API being unavailable, it displays a custom message. + If the error is due to the Talawa API being unavailable, it displays a custom message. And for other error cases, it is using regular expression (case-insensitive) to match and show valid messages */ export const errorHandler = (a: unknown, error: unknown): void => { const tErrors: TFunction = i18n.getFixedT(null, 'errors'); @@ -13,15 +13,15 @@ export const errorHandler = (a: unknown, error: unknown): void => { const errorMessage = error.message; if (errorMessage === 'Failed to fetch') { toast.error(tErrors('talawaApiUnavailable')); - } else if (errorMessage.includes('Value is not a valid phone number')) { + } else if (errorMessage.match(/value is not a valid phone number/i)) { toast.error(tErrors('invalidPhoneNumber')); - } else if (errorMessage.includes('does not exist in "EducationGrade"')) { + } else if (errorMessage.match(/does not exist in "EducationGrade"/i)) { toast.error(tErrors('invalidEducationGrade')); - } else if (errorMessage.includes('does not exist in "EmploymentStatus"')) { + } else if (errorMessage.match(/does not exist in "EmploymentStatus"/i)) { toast.error(tErrors('invalidEmploymentStatus')); - } else if (errorMessage.includes('does not exist in "MaritalStatus"')) { + } else if (errorMessage.match(/does not exist in "MaritalStatus"/i)) { toast.error(tErrors('invalidMaritalStatus')); - } else if (errorMessage.includes('status code 400')) { + } else if (errorMessage.match(/status code 400/i)) { toast.error(tErrors('error400')); } else { toast.error(errorMessage);