import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import { AccountingRunsTable } from 'src/components/accounting-run-list/accounting-runs-table';
import { ACCOUNTING_RUN_STATUSES as RUN_STATUS } from 'src/constants';
import { AccountingRunContext } from 'src/contexts/accounting-run-context';

const accountingRunsData = {
    23: {
        id: 23,
        name: 'Priority 1 - Monthly',
        contractCount: null,
        date: null,
        status: RUN_STATUS.NO_ACTION_TAKEN,
    },
    17: {
        id: 17,
        name: 'Priority 2 - Monthly',
        contractCount: null,
        date: '01-15-2019',
        status: RUN_STATUS.WAITING_TO_RUN,
    },
    19: {
        id: 19,
        name: 'Priority 3 - Monthly',
        contractCount: null,
        date: null,
        status: RUN_STATUS.NO_ACTION_TAKEN,
    },
    29: {
        id: 29,
        name: 'Priority 4 - Quarterly',
        contractCount: 18,
        date: null,
        status: RUN_STATUS.COMPLETE,
    },
};

describe('<AccountingRunsTable>', () => {
    const context = {
        accountingRuns: accountingRunsData,
        accountingPeriodDetail: { accountingPeriodStatus: 'open' },
    };

    const renderAccountingRuns = (contextValues: any) =>
        renderInAppContext(
            <AccountingRunContext.Provider value={contextValues}>
                <AccountingRunsTable />
            </AccountingRunContext.Provider>
        );

    it('shows the accounting runs sorted by controller name', () => {
        renderAccountingRuns(context);

        const bodyText: any = [];
        document
            .querySelectorAll('div.GridTable-cell')
            .forEach((body: any) => bodyText.push(body.textContent));
        expect(bodyText).toEqual([
            '',
            'Priority 1 - Monthly',
            '',
            RUN_STATUS.NO_ACTION_TAKEN,
            'CreateSkip',
            '',
            'Priority 2 - Monthly',
            '01-15-2019',
            RUN_STATUS.WAITING_TO_RUN,
            '',
            '',
            'Priority 3 - Monthly',
            '',
            RUN_STATUS.NO_ACTION_TAKEN,
            'CreateSkip',
            '18',
            'Priority 4 - Quarterly',
            '',
            RUN_STATUS.COMPLETE,
            'ApproveInvalidate',
        ]);
    });

    it("doesn't show actions if runs are marked as complete", () => {
        const completedContext = {
            accountingRuns: accountingRunsData,
            areRunsComplete: true,
        };
        renderAccountingRuns(completedContext);

        const bodyText: any = [];
        document
            .querySelectorAll('div.GridTable-cell')
            .forEach((body: any) => bodyText.push(body.textContent));
        expect(bodyText).toEqual([
            '',
            'Priority 1 - Monthly',
            '',
            RUN_STATUS.NO_ACTION_TAKEN,
            '-',
            '',
            'Priority 2 - Monthly',
            '01-15-2019',
            RUN_STATUS.WAITING_TO_RUN,
            '-',
            '',
            'Priority 3 - Monthly',
            '',
            RUN_STATUS.NO_ACTION_TAKEN,
            '-',
            '18',
            'Priority 4 - Quarterly',
            '',
            RUN_STATUS.COMPLETE,
            '-',
        ]);
        expect(
            screen.getByTestId('AccountingRunsCompleteIconTestId')
        ).toBeTruthy();
    });
});
