From edc3eaf167fe5cdbd352596e9d3e2cdfb8650f60 Mon Sep 17 00:00:00 2001 From: Azizjon Nurov Date: Wed, 25 Dec 2024 22:15:22 +0500 Subject: [PATCH] Add test case --- .../getInvoiceExportData.test.js | 119 ++++++++++++++++-- 1 file changed, 109 insertions(+), 10 deletions(-) diff --git a/lib/utils/invoiceExport/getInvoiceExportData.test.js b/lib/utils/invoiceExport/getInvoiceExportData.test.js index b492c2e8..2a3e132e 100644 --- a/lib/utils/invoiceExport/getInvoiceExportData.test.js +++ b/lib/utils/invoiceExport/getInvoiceExportData.test.js @@ -1,17 +1,116 @@ -import { renderHook } from '@testing-library/react-hooks'; -import { useIntl } from 'react-intl'; +/* Developed collaboratively using AI (ChatGPT) */ import { getInvoiceExportData } from './getInvoiceExportData'; +import { fetchAllRecords } from '../fetchAllRecords'; +import { fetchExportDataByIds } from '../fetchExportDataByIds'; +import { getAddresses } from '../getAddresses'; +import { createInvoiceExportReport } from './createInvoiceExportReport'; -jest.mock('./createInvoiceExportReport', () => ({ - createInvoiceExportReport: jest.fn().mockReturnValue('test report'), -})); +jest.mock('../fetchAllRecords'); +jest.mock('../fetchExportDataByIds'); +jest.mock('../getAddresses'); +jest.mock('./createInvoiceExportReport'); -test('should create export report', async () => { - const { result } = renderHook(() => useIntl()); - const intl = result.current; +describe('getInvoiceExportData', () => { + const mockKy = { + get: jest.fn().mockImplementation(() => ({ + json: jest.fn().mockResolvedValue({ from: 'USD', to: 'EUR', rate: 0.85 }), + })), + }; - const report = await getInvoiceExportData({ intl }); + const mockIntl = {}; + const mockQuery = {}; - expect(report).toEqual('test report'); + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should fetch and process invoice export data correctly', async () => { + fetchAllRecords.mockResolvedValue([{ id: '1', vendorId: 'v1', currency: 'USD' }]); + fetchExportDataByIds.mockResolvedValue([]); + getAddresses.mockReturnValue([]); + createInvoiceExportReport.mockReturnValue('Export Report'); + + const result = await getInvoiceExportData({ + ky: mockKy, + intl: mockIntl, + query: mockQuery, + currency: 'USD', + }); + + expect(fetchAllRecords).toHaveBeenCalled(); + expect(fetchExportDataByIds).toHaveBeenCalled(); + expect(createInvoiceExportReport).toHaveBeenCalledWith( + expect.objectContaining({ + invoiceMap: { '1': { id: '1', vendorId: 'v1', currency: 'USD' } }, + }), + ); + expect(result).toBe('Export Report'); + }); + + it('should handle exchange rate fetching correctly', async () => { + const mockExportInvoices = [ + { id: '1', vendorId: 'v1', currency: 'GBP' }, + { id: '2', vendorId: 'v2', currency: 'PLN' }, + ]; + + fetchAllRecords.mockResolvedValue(mockExportInvoices); + + mockKy.get + .mockImplementationOnce(() => ({ + json: jest.fn().mockResolvedValue({ from: 'GBP', to: 'USD', rate: 1.26 }), + })) + .mockImplementationOnce(() => ({ + json: jest.fn().mockResolvedValue({ from: 'PLN', to: 'USD', rate: 0.24 }), + })); + + const result = await getInvoiceExportData({ + ky: mockKy, + intl: mockIntl, + query: mockQuery, + currency: 'USD', + }); + + expect(result).toBe('Export Report'); + expect(createInvoiceExportReport).toHaveBeenCalledWith( + expect.objectContaining({ + exchangeRateMap: { + GBP: { from: 'GBP', rate: 1.26, to: 'USD' }, + PLN: { from: 'PLN', rate: 0.24, to: 'USD' }, + }, + }), + ); + }); + + it('should filter out failed exchange rate requests', async () => { + const mockExportInvoices = [ + { id: '1', vendorId: 'v1', currency: 'USD' }, + { id: '2', vendorId: 'v2', currency: 'TJS' }, + ]; + + fetchAllRecords.mockResolvedValue(mockExportInvoices); + + mockKy.get.mockImplementationOnce(() => ({ + json: jest.fn().mockResolvedValue({ from: 'USD', to: 'USD', rate: 1 }), + })); + mockKy.get.mockImplementationOnce(() => ({ + json: jest.fn().mockRejectedValue(new Error('Cannot convert TJS into USD: Rate Provider did not return data though at check before data was flagged as available, provider=ECB, query=ConversionQuery (\n{Query.termCurrency=USD, Query.baseCurrency=TJS})')), + })); + + const result = await getInvoiceExportData({ + ky: mockKy, + intl: mockIntl, + query: mockQuery, + currency: 'USD', + }); + + expect(result).toBe('Export Report'); + expect(createInvoiceExportReport).toHaveBeenCalledWith( + expect.objectContaining({ + exchangeRateMap: { + USD: { from: 'USD', rate: 1, to: 'USD' }, + }, + }), + ); + }); });