@@ -36,7 +36,7 @@ describe('GovukNotifyService', () => {
36
36
} ) ;
37
37
38
38
describe ( 'sendEmail' , ( ) => {
39
- const generateNotifyError = ( status : number , message : string ) => {
39
+ const generateNotifyError = ( status : number , message ? : string ) => {
40
40
const response = {
41
41
data : {
42
42
status_code : status ,
@@ -96,8 +96,15 @@ describe('GovukNotifyService', () => {
96
96
error : 'Internal Server Error' ,
97
97
status : 500 ,
98
98
} ,
99
+ {
100
+ exceptionClass : InternalServerErrorException ,
101
+ exceptionName : 'Internal Server Error Exception' ,
102
+ error : 'Internal Server Error' ,
103
+ status : 500 ,
104
+ } ,
99
105
] ) ( 'throws exception $exceptionName for unexpected $status' , async ( { exceptionClass, exceptionName, error, status } ) => {
100
- jest . mocked ( sendEmailMethodMock ) . mockImplementation ( ( ) => Promise . reject ( generateNotifyError ( status , errorMessage ) ) ) ;
106
+ const notifyError = generateNotifyError ( status , errorMessage ) ;
107
+ jest . mocked ( sendEmailMethodMock ) . mockImplementation ( ( ) => Promise . reject ( notifyError ) ) ;
101
108
102
109
const resultPromise = service . sendEmail ( govUkNotifyKey , { sendToEmailAddress, templateId, personalisation } ) ;
103
110
@@ -106,18 +113,37 @@ describe('GovukNotifyService', () => {
106
113
await expect ( resultPromise ) . rejects . toThrow ( exceptionName ) ;
107
114
await expect ( resultPromise ) . rejects . toHaveProperty ( 'status' , status ) ;
108
115
await expect ( resultPromise ) . rejects . toHaveProperty ( 'response' , { message : [ errorMessage ] , error, statusCode : status } ) ;
116
+ expect ( loggerError ) . toHaveBeenCalledTimes ( 1 ) ;
117
+ expect ( loggerError ) . toHaveBeenCalledWith ( notifyError ) ;
109
118
} ) ;
110
119
111
120
it ( 'throws generic Error exception for unexpected status' , async ( ) => {
112
121
const unexpectedStatus = valueGenerator . integer ( { min : 900 , max : 999 } ) ;
113
- jest . mocked ( sendEmailMethodMock ) . mockImplementation ( ( ) => Promise . reject ( generateNotifyError ( unexpectedStatus , errorMessage ) ) ) ;
122
+ const notifyError = generateNotifyError ( unexpectedStatus , errorMessage ) ;
123
+ jest . mocked ( sendEmailMethodMock ) . mockImplementation ( ( ) => Promise . reject ( notifyError ) ) ;
114
124
115
125
const resultPromise = service . sendEmail ( govUkNotifyKey , { sendToEmailAddress, templateId, personalisation } ) ;
116
126
117
127
expect ( sendEmailMethodMock ) . toHaveBeenCalledTimes ( 1 ) ;
118
128
await expect ( resultPromise ) . rejects . toBeInstanceOf ( Error ) ;
119
129
await expect ( resultPromise ) . rejects . toThrow ( errorMessage ) ;
120
130
await expect ( resultPromise ) . rejects . toHaveProperty ( 'message' , errorMessage ) ;
131
+ expect ( loggerError ) . toHaveBeenCalledTimes ( 1 ) ;
132
+ expect ( loggerError ) . toHaveBeenCalledWith ( notifyError ) ;
133
+ } ) ;
134
+
135
+ it ( 'throws generic Error exception for error without error message' , async ( ) => {
136
+ const notifyError = generateNotifyError ( 400 ) ;
137
+ jest . mocked ( sendEmailMethodMock ) . mockImplementation ( ( ) => Promise . reject ( notifyError ) ) ;
138
+
139
+ const resultPromise = service . sendEmail ( govUkNotifyKey , { sendToEmailAddress, templateId, personalisation } ) ;
140
+
141
+ expect ( sendEmailMethodMock ) . toHaveBeenCalledTimes ( 1 ) ;
142
+ await expect ( resultPromise ) . rejects . toBeInstanceOf ( Error ) ;
143
+ await expect ( resultPromise ) . rejects . toThrow ( 'NotifyClient failed with unexpected error %o' ) ;
144
+ await expect ( resultPromise ) . rejects . toHaveProperty ( 'message' , 'NotifyClient failed with unexpected error %o' ) ;
145
+ expect ( loggerError ) . toHaveBeenCalledTimes ( 1 ) ;
146
+ expect ( loggerError ) . toHaveBeenCalledWith ( notifyError ) ;
121
147
} ) ;
122
148
123
149
it ( 'throws exception UnprocessableEntityException for empty response from GOV.UK Notify client' , async ( ) => {
@@ -128,6 +154,8 @@ describe('GovukNotifyService', () => {
128
154
expect ( sendEmailMethodMock ) . toHaveBeenCalledTimes ( 1 ) ;
129
155
await expect ( resultPromise ) . rejects . toBeInstanceOf ( UnprocessableEntityException ) ;
130
156
await expect ( resultPromise ) . rejects . toThrow ( 'No GOV.UK Notify response' ) ;
157
+ expect ( loggerError ) . toHaveBeenCalledTimes ( 1 ) ;
158
+ expect ( loggerError ) . toHaveBeenCalledWith ( 'Empty response from GOV.UK Notify' ) ;
131
159
} ) ;
132
160
} ) ;
133
161
} ) ;
0 commit comments