|
| 1 | +import { connectionErrorHandler } from '../src/lib/connection-error-handler'; |
| 2 | + |
| 3 | +describe('connectionErrorHandler', () => { |
| 4 | + test('should return correct error message for EDNS error code', () => { |
| 5 | + const error = { code: 'EDNS' }; |
| 6 | + const result = connectionErrorHandler(error); |
| 7 | + expect(result).toEqual({ |
| 8 | + valid: false, |
| 9 | + error: 'SMTP server not found or unreachable. Error Code: EDNS', |
| 10 | + }); |
| 11 | + }); |
| 12 | + |
| 13 | + test('should return correct error message for CONN error code', () => { |
| 14 | + const error = { code: 'CONN' }; |
| 15 | + const result = connectionErrorHandler(error); |
| 16 | + expect(result).toEqual({ |
| 17 | + valid: false, |
| 18 | + error: |
| 19 | + 'Could not establish a connection to the SMTP server. Error Code: CONN', |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should return correct error message for ETIMEDOUT error code', () => { |
| 24 | + const error = { code: 'ETIMEDOUT' }; |
| 25 | + const result = connectionErrorHandler(error); |
| 26 | + expect(result).toEqual({ |
| 27 | + valid: false, |
| 28 | + error: 'Connection to the SMTP server timed out. Error Code: ETIMEDOUT', |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + test('should return stringified error for error code not defined', () => { |
| 33 | + const error = { code: undefined }; |
| 34 | + const result = connectionErrorHandler(error); |
| 35 | + expect(result).toEqual({ |
| 36 | + valid: false, |
| 37 | + error: JSON.stringify(error), |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + test('should return default message with unknown error code', () => { |
| 42 | + const error = { code: 'UNKNOWN_CODE' }; |
| 43 | + const result = connectionErrorHandler(error); |
| 44 | + expect(result).toEqual({ |
| 45 | + valid: false, |
| 46 | + error: `SMTP connection failed with error code: UNKNOWN_CODE`, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should return stringified error when error object has no code', () => { |
| 51 | + const error = { message: 'Some error' }; |
| 52 | + const result = connectionErrorHandler(error); |
| 53 | + expect(result).toEqual({ |
| 54 | + valid: false, |
| 55 | + error: JSON.stringify(error), |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should return stringified error for non-object errors', () => { |
| 60 | + const error = 'some string error'; |
| 61 | + const result = connectionErrorHandler(error); |
| 62 | + expect(result).toEqual({ |
| 63 | + valid: false, |
| 64 | + error: JSON.stringify(error), |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should return stringified null for null error', () => { |
| 69 | + const error = null; |
| 70 | + const result = connectionErrorHandler(error); |
| 71 | + expect(result).toEqual({ |
| 72 | + valid: false, |
| 73 | + error: JSON.stringify(error), |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments