|
| 1 | +type TFunction = (key: string, options?: Record<string, unknown>) => string; |
| 2 | + |
| 3 | +import { errorHandler } from './errorHandler'; |
| 4 | +import { toast } from 'react-toastify'; |
| 5 | +import { describe, it, expect, vi } from 'vitest'; |
| 6 | + |
| 7 | +vi.mock('react-toastify', () => ({ |
| 8 | + toast: { |
| 9 | + error: vi.fn(), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +describe('Test if errorHandler is working properly', () => { |
| 14 | + const t: TFunction = (key: string) => key; |
| 15 | + const tErrors: TFunction = ( |
| 16 | + key: string, |
| 17 | + options?: Record<string, unknown>, |
| 18 | + ) => { |
| 19 | + if (options) { |
| 20 | + console.log(`options are passed, but the function returns only ${key}`); |
| 21 | + } |
| 22 | + return key; |
| 23 | + }; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => { |
| 30 | + const error = new Error('Failed to fetch'); |
| 31 | + errorHandler(t, error); |
| 32 | + |
| 33 | + expect(toast.error).toHaveBeenCalledWith(tErrors('talawaApiUnavailable')); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should call toast.error with the correct message if error message contains this substring "Value is not a valid phone number"', () => { |
| 37 | + const error = new Error('This value is not a valid phone number'); |
| 38 | + errorHandler(t, error); |
| 39 | + expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber')); |
| 40 | + }); |
| 41 | + |
| 42 | + test.each([ |
| 43 | + ['EducationGrade', 'invalidEducationGrade'], |
| 44 | + ['EmploymentStatus', 'invalidEmploymentStatus'], |
| 45 | + ['MaritalStatus', 'invalidMaritalStatus'], |
| 46 | + ])('should handle invalid %s error', (field, expectedKey) => { |
| 47 | + const error = new Error(`This value does not exist in "${field}"`); |
| 48 | + errorHandler(t, error); |
| 49 | + expect(toast.error).toHaveBeenCalledWith(tErrors(expectedKey)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should call toast.error with the correct message if error message contains this substring "status code 400"', () => { |
| 53 | + const error = new Error('Server responded with status code 400'); |
| 54 | + errorHandler(t, error); |
| 55 | + |
| 56 | + expect(toast.error).toHaveBeenCalledWith(tErrors('error400')); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle error messages with different cases', () => { |
| 60 | + errorHandler(t, new Error('VALUE IS NOT A VALID PHONE NUMBER')); |
| 61 | + expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber')); |
| 62 | + |
| 63 | + errorHandler(t, new Error('This Value Does Not Exist in "EducationGrade"')); |
| 64 | + expect(toast.error).toHaveBeenCalledWith(tErrors('invalidEducationGrade')); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should call toast.error with the error message if it is an instance of error but have not matched any error message patterns', () => { |
| 68 | + const error = new Error('Bandhan sent an error message'); |
| 69 | + errorHandler(t, error); |
| 70 | + expect(toast.error).toHaveBeenCalledWith(error.message); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle different types for the first parameter while still showing error messages', () => { |
| 74 | + errorHandler(undefined, new Error('Some error')); |
| 75 | + expect(toast.error).toHaveBeenCalled(); |
| 76 | + |
| 77 | + errorHandler(null, new Error('Some error')); |
| 78 | + expect(toast.error).toHaveBeenCalled(); |
| 79 | + |
| 80 | + errorHandler({}, new Error('Some error')); |
| 81 | + expect(toast.error).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle non-null but non-Error objects for the error parameter', () => { |
| 85 | + errorHandler(t, { message: 'Error message in object' }); |
| 86 | + expect(toast.error).toHaveBeenCalledWith( |
| 87 | + tErrors('unknownError', { msg: { message: 'Error message in object' } }), |
| 88 | + ); |
| 89 | + |
| 90 | + errorHandler(t, 'Direct error message'); |
| 91 | + expect(toast.error).toHaveBeenCalledWith( |
| 92 | + tErrors('unknownError', { msg: 'Direct error message' }), |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call toast.error with the error message if error object is falsy', () => { |
| 97 | + const error = null; |
| 98 | + errorHandler(t, error); |
| 99 | + |
| 100 | + expect(toast.error).toHaveBeenCalledWith( |
| 101 | + tErrors('unknownError', { msg: error }), |
| 102 | + ); |
| 103 | + }); |
| 104 | +}); |
0 commit comments