import React from 'react';
import { screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import * as accountingPeriodDetailResponse from 'src/__fixtures__/graphql/accounting-period-detail-response.json';
import * as accountingPeriodMutations from 'src/apollo/mutations/accounting-period';
import * as accountingRunMutations from 'src/apollo/mutations/accounting-run';
import * as accountingPeriodQuery from 'src/apollo/queries/accounting-period';
import { AccountingPeriodDetailWrapper } from 'src/components/accounting-period/accounting-period-detail-wrapper';
import { ABACUS_PROFILE, USER_FEATURES } from 'src/constants';

describe('<AccountingPeriodDetailWrapper>', () => {
    const updateAccountingPeriod = jest.fn().mockResolvedValue({});
    const updateAccountingPeriodState = jest.fn().mockResolvedValue({});
    const updateAccountingRun = jest.fn().mockResolvedValue({});

    beforeEach(() => {
        jest.spyOn(
            accountingPeriodQuery,
            'useAccountingPeriodFullDetail'
        ).mockReturnValue({
            data: accountingPeriodDetailResponse,
            loading: false,
            refetch: jest.fn(),
        });
        jest.spyOn(
            accountingPeriodMutations,
            'useUpdateAccountingPeriod'
        ).mockReturnValue(updateAccountingPeriod);
        jest.spyOn(
            accountingPeriodMutations,
            'useUpdateAccountingPeriodState'
        ).mockReturnValue(updateAccountingPeriodState);
        jest.spyOn(
            accountingRunMutations,
            'useUpdateAccountingRun'
        ).mockReturnValue(updateAccountingRun);
    });

    afterEach(jest.restoreAllMocks);

    let mockFeatures = {
        [USER_FEATURES.ABACUS_ACCOUNTING_PERIOD_DETAIL_MIGRATE_TO_TSX]: false,
    };

    const render = (features: { [x: number]: boolean } = mockFeatures) =>
        renderInAppContext(<AccountingPeriodDetailWrapper />, {
            identity: createIdentity({
                features,
                profileType: ABACUS_PROFILE,
                id: 'Jane User',
            }),
        });

    it('renders old component when FF is disabled', () => {
        render();
        expect(screen.queryByTestId('AccountingPeriodDetail')).toBeNull();
    });

    it('renders new component when FF is enabled', () => {
        mockFeatures = {
            [USER_FEATURES.ABACUS_ACCOUNTING_PERIOD_DETAIL_MIGRATE_TO_TSX]: true,
        };
        render(mockFeatures);
        expect(screen.getByTestId('AccountingPeriodDetail')).toBeDefined();
    });
});
