|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +import { databaseManager, loggerService } from '../../src/'; |
| 3 | +import { unwrap } from '@frmscoe/frms-coe-lib/lib/helpers/unwrap'; |
| 4 | +import { handleGetReportRequestByMsgId } from '../../src/logic.service'; |
| 5 | + |
| 6 | +// Mock the module |
| 7 | +jest.mock('../../src/', () => ({ |
| 8 | + databaseManager: { |
| 9 | + getReportByMessageId: jest.fn(), // Ensure the mock function is typed correctly |
| 10 | + }, |
| 11 | + loggerService: { |
| 12 | + log: jest.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('@frmscoe/frms-coe-lib/lib/helpers/unwrap', () => ({ |
| 17 | + unwrap: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('handleGetReportRequestByMsgId', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should successfully retrieve and unwrap the report', async () => { |
| 26 | + const mockReport = [ |
| 27 | + { |
| 28 | + /* mock report data */ |
| 29 | + }, |
| 30 | + ]; |
| 31 | + // Ensure getReportByMessageId is typed as a Jest mock function |
| 32 | + (databaseManager.getReportByMessageId as jest.Mock).mockResolvedValue(mockReport); |
| 33 | + (unwrap as jest.Mock).mockReturnValue(mockReport); |
| 34 | + |
| 35 | + const msgid = 'test-msg-id'; |
| 36 | + const result = await handleGetReportRequestByMsgId(msgid); |
| 37 | + |
| 38 | + expect(databaseManager.getReportByMessageId).toHaveBeenCalledWith('transactions', msgid); |
| 39 | + expect(unwrap).toHaveBeenCalledWith(mockReport); |
| 40 | + expect(result).toBe(mockReport); |
| 41 | + expect(loggerService.log).toHaveBeenCalledWith(`Started handling get request by message id the message id is ${msgid}`); |
| 42 | + expect(loggerService.log).toHaveBeenCalledWith('Completed handling get report by message id'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should log and throw an error if the database query fails', async () => { |
| 46 | + const errorMessage = 'Database error'; |
| 47 | + (databaseManager.getReportByMessageId as jest.Mock).mockRejectedValue(new Error(errorMessage)); |
| 48 | + |
| 49 | + const msgid = 'test-msg-id'; |
| 50 | + await expect(handleGetReportRequestByMsgId(msgid)).rejects.toThrow(errorMessage); |
| 51 | + |
| 52 | + expect(databaseManager.getReportByMessageId).toHaveBeenCalledWith('transactions', msgid); |
| 53 | + expect(loggerService.log).toHaveBeenCalledWith( |
| 54 | + `Failed fetching report from database service with error message: ${errorMessage}`, |
| 55 | + 'handleGetReportRequestByMsgId()', |
| 56 | + ); |
| 57 | + expect(loggerService.log).toHaveBeenCalledWith('Completed handling get report by message id'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should log "Completed handling get report by message id" when the operation is successful', async () => { |
| 61 | + const mockReport = [ |
| 62 | + { |
| 63 | + /* mock report data */ |
| 64 | + }, |
| 65 | + ]; |
| 66 | + (databaseManager.getReportByMessageId as jest.Mock).mockResolvedValue(mockReport); |
| 67 | + (unwrap as jest.Mock).mockReturnValue(mockReport); |
| 68 | + |
| 69 | + const msgid = 'test-msg-id'; |
| 70 | + await handleGetReportRequestByMsgId(msgid); |
| 71 | + |
| 72 | + expect(loggerService.log).toHaveBeenCalledWith('Completed handling get report by message id'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should log "Completed handling get report by message id" even when an error occurs', async () => { |
| 76 | + const errorMessage = 'Database error'; |
| 77 | + (databaseManager.getReportByMessageId as jest.Mock).mockRejectedValue(new Error(errorMessage)); |
| 78 | + |
| 79 | + const msgid = 'test-msg-id'; |
| 80 | + try { |
| 81 | + await handleGetReportRequestByMsgId(msgid); |
| 82 | + } catch (e) { |
| 83 | + // Expected to throw an error |
| 84 | + } |
| 85 | + |
| 86 | + expect(loggerService.log).toHaveBeenCalledWith('Completed handling get report by message id'); |
| 87 | + }); |
| 88 | +}); |
0 commit comments