Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebenezr committed Apr 1, 2024
1 parent c4295e9 commit d92c454
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
72 changes: 72 additions & 0 deletions customer/__tests__/logMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { test, vi } from 'vitest';
import LogMessages from '../src/utils/logMessages';
import { Logger } from '../src/utils/logging';
import ErrorHandler from '../src/utils/errorHandler';
import convertKeys from '../src/utils/convertKeys';

vi.mock('../src/utils/logging');
vi.mock('../src/utils/errorHandler');
vi.mock('../src/utils/convertKeys');

test.skip('logs a success message for response codes 200 and 1000', ({
expect,
}) => {
const mobileNumber = '1234567890';
const request = {};
const apiUrl = 'http://example.com';
const payload = {};

const response = {
header: {
responseCode: 200,
responseMessage: 'Success',
customerMessage: 'Success',
},
};

LogMessages(response, mobileNumber, request, apiUrl, payload);

expect(Logger.log).toHaveBeenCalledWith('info', 'Success: ', {
message: 'Request Sucessful',
request,
payload,
msisdn: mobileNumber,
response: convertKeys(response),
url: apiUrl,
});
});

// expect(log).toHaveBeenCalledWith('info', 'Success: ', {
// message: 'Request Sucessful',
// request,
// payload,
// msisdn: mobileNumber,
// response: convertKeys(response),
// url: apiUrl,
// });

// test('logs an error message for other response codes', ({ expect }) => {
// const response = {
// header: {
// response_code: 500,
// response_message: 'Error',
// customer_message: 'Error',
// },
// };
// const mobileNumber = '1234567890';
// const request = {};
// const apiUrl = 'http://example.com';
// const payload = {};

// LogMessages(response, mobileNumber, request, apiUrl, payload);

// expect(log).toHaveBeenCalledWith('error', 'Error: ', {
// fullError: response,
// msisdn: mobileNumber,
// request,
// payload,
// customError: `Got (500) while hitting ${apiUrl}`,
// actualError: 'Error',
// customerMessage: ErrorHandler('Error'),
// });
// });
26 changes: 20 additions & 6 deletions customer/src/utils/logMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,28 @@ const LogMessages = (
'Sorry we are experiencing a technical problem. Please try again later',
});
} else {
const {
header: { responseCode, responseMessage, customerMessage },
} = convertKeys(response);
const { responseCode, responseMessage, customerMessage } =
convertKeys(response)?.header || {};

console.log('response', response);
if (responseCode === 200 || responseCode === 1000) {
Logger.log('info', 'Success: ', {
message: 'Request Successful',

// ... rest of the code
message: 'Request Sucessful',
request,
payload,
msisdn: mobileNumber,
response: convertKeys(response),
url: apiUrl,
});
} else {
Logger.log('error', 'Error: ', {
fullError: response,
msisdn: mobileNumber,
request,
payload,
customError: `Got (${responseCode}) while hitting ${apiUrl}`,
actualError: responseMessage,
customerMessage: ErrorHandler(customerMessage),
});
}
}
Expand Down

0 comments on commit d92c454

Please sign in to comment.