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