import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import * as accountingPeriodDetail from 'src/__fixtures__/graphql/accounting-period-detail-response.json';
import {
    ACCOUNTING_RUN_ACTIONS as RUN_ACTIONS,
    ACCOUNTING_RUN_STATUSES as RUN_STATUSES,
} from 'src/constants';
import { AccountingRunContext } from 'src/contexts/accounting-run-context';
import {
    AccountingRunActionSection,
    type AccountingRunActionSectionPropTypes,
} from 'src/components/accounting-run-list/accounting-run-action-section';

describe('<AccountingRunActionSection>', () => {
    const contextValues = {
        accountingRuns: {},
        updateRunStatus: jest.fn().mockResolvedValue({}),
        accountingPeriodDetail: accountingPeriodDetail.abacusAccountingPeriod,
    };

    const render = (props: AccountingRunActionSectionPropTypes) =>
        renderInAppContext(
            <AccountingRunContext.Provider value={contextValues}>
                <AccountingRunActionSection {...props} />
            </AccountingRunContext.Provider>
        );

    const { APPROVED, COMPLETE, NO_ACTION_TAKEN } = RUN_STATUSES;

    it(`renders create/skip action buttons when run status is ${NO_ACTION_TAKEN}`, () => {
        const props = { id: 123, status: NO_ACTION_TAKEN };
        render(props);
        expect(
            screen.getByTestId('AccountingRunActionSectionTestId').textContent
        ).toEqual(`${RUN_ACTIONS.CREATE}${RUN_ACTIONS.SKIP}`);
        expect(
            screen.getByTestId(
                `${RUN_ACTIONS.CREATE.toLowerCase()}AccountingRun-123`
            )
        ).toBeTruthy();
        expect(
            screen.getByTestId(
                `${RUN_ACTIONS.SKIP.toLowerCase()}AccountingRun-123`
            )
        ).toBeTruthy();
    });

    it(`renders approve/invalidate action buttons when run status is ${COMPLETE}`, () => {
        const props = { id: 123, status: COMPLETE };
        render(props);
        expect(
            screen.getByTestId('AccountingRunActionSectionTestId').textContent
        ).toEqual(`${RUN_ACTIONS.APPROVE}${RUN_ACTIONS.INVALIDATE}`);
        expect(
            screen.getByTestId(
                `${RUN_ACTIONS.APPROVE.toLowerCase()}AccountingRun-123`
            )
        ).toBeTruthy();
        expect(
            screen.getByTestId(
                `${RUN_ACTIONS.INVALIDATE.toLowerCase()}AccountingRun-123`
            )
        ).toBeTruthy();
    });

    it(`no action button when run status is ${APPROVED}`, () => {
        const props = { id: 123, status: APPROVED };
        render(props);
        expect(
            screen.getByTestId('AccountingRunActionSectionTestId').textContent
        ).toEqual('');
    });

    it(`renders empty when run status is neither ${COMPLETE} nor ${NO_ACTION_TAKEN}`, () => {
        const props = { id: 123, status: RUN_STATUSES.WAITING_TO_RUN };
        render(props);
        expect(
            screen.getByTestId('AccountingRunActionSectionTestId').textContent
        ).toEqual('');
    });
});
