import * as AmountHelpers from 'src/utils/amount-helpers';
import { getFormattedAmount, mapAccountingRuns } from '../accounting-run';

describe('Accounting Run Utils', () => {
    beforeAll(() => {
        jest.spyOn(AmountHelpers, 'amountFormatter').mockReturnValue(
            'mocked accountFormatterValue'
        );
    });

    describe('getFormattedAmount', () => {
        it('calls amountFormatter when amount and code are provided', () => {
            const result: string | null = getFormattedAmount('100', 'USD');
            expect(AmountHelpers.amountFormatter).toHaveBeenCalled();
            expect(result).toBe('mocked accountFormatterValue');
        });
        it('returns null when amount or code is not provided', () => {
            expect(getFormattedAmount(null, 'USD')).toBeNull();
            expect(getFormattedAmount('100', null)).toBeNull();
            expect(getFormattedAmount(null, null)).toBeNull();
        });
    });

    describe('mapAccountingRuns', () => {
        it('returns a formatted list of accounting runs', () => {
            const input = [
                {
                    accountingRunId: 1,
                    accountingRunStatus: 'Running',
                    contractCount: 3,
                    runControllerName: 'the boss',
                    startDate: '2022-05-25',
                },
            ];
            expect(mapAccountingRuns(input)).toEqual({
                1: {
                    id: 1,
                    date: '2022-05-25',
                    name: 'the boss',
                    contractCount: 3,
                    status: 'Running',
                },
            });
        });
        it('returns mapped run status when for approving and approved', () => {
            const input = [
                {
                    accountingRunId: 1,
                    accountingRunStatus: 'Committing',
                    contractCount: 3,
                    runControllerName: 'the boss',
                    startDate: '2022-05-25',
                },
                {
                    accountingRunId: 2,
                    accountingRunStatus: 'Committed',
                    contractCount: 7,
                    runControllerName: 'the boss2',
                    startDate: '2022-05-25',
                },
            ];
            expect(mapAccountingRuns(input)).toEqual({
                1: {
                    id: 1,
                    date: '2022-05-25',
                    name: 'the boss',
                    contractCount: 3,
                    status: 'Approving',
                },
                2: {
                    id: 2,
                    date: '2022-05-25',
                    name: 'the boss2',
                    contractCount: 7,
                    status: 'Approved',
                },
            });
        });
    });
});
