Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
azizjonnurov committed Dec 25, 2024
1 parent a285482 commit edc3eaf
Showing 1 changed file with 109 additions and 10 deletions.
119 changes: 109 additions & 10 deletions lib/utils/invoiceExport/getInvoiceExportData.test.js
Original file line number Diff line number Diff line change
@@ -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' },
},
}),
);
});
});

0 comments on commit edc3eaf

Please sign in to comment.