import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import {
    ROUTE_BULK_CREATE_DIGITAL_AUDIO,
    ROUTE_BULK_CREATE_GETTING_STARTED,
} from 'src/constants';
import * as featureFlags from 'src/utils/features';
import AccountSelectionPage, { getNextRoute } from '..';

jest.mock('src/data/queries/accountsXpp', () => ({
    useAccountsXppQuery: () => ({
        data: [],
        loading: false,
        error: undefined,
    }),
    useAccountXppSearchQuery: () => [
        jest.fn().mockResolvedValue([]),
        { data: [], loading: false, error: undefined },
    ],
}));

describe('AccountSelectionPage', () => {
    afterEach(() => {
        jest.restoreAllMocks();
    });

    test('renders page', async () => {
        const component = renderInAppContext(<AccountSelectionPage />);
        expect(component).toMatchSnapshot();
    });

    test('renders page with dropdown menu open', async () => {
        const component = renderInAppContext(<AccountSelectionPage />);

        const button = component.getByTestId('SuiteListViewExpandButton');
        await waitFor(async () => {
            await fireEvent.click(button);
        });

        expect(component).toMatchSnapshot();
    });

    test('renders PpAccountSearchDropdown when isPpAccountSelectorEnabled is true', async () => {
        jest.spyOn(
            featureFlags,
            'usePpBulkUploadAccountSelector'
        ).mockReturnValue(true);

        const { container } = renderInAppContext(<AccountSelectionPage />);

        expect(
            container.querySelector('.pp-account-selection-page-dropdown')
        ).toBeInTheDocument();
        expect(container).toMatchSnapshot();
    });

    describe('getNextRoute', () => {
        test('routes to bulk create when spotify feature is disabled', () => {
            expect(getNextRoute(false, false)).toBe(
                ROUTE_BULK_CREATE_DIGITAL_AUDIO
            );
        });

        test('routes to getting started when spotify feature is enabled and dont-show is unchecked', () => {
            expect(getNextRoute(true, false)).toBe(
                ROUTE_BULK_CREATE_GETTING_STARTED
            );
        });

        test('skips route adjustment and routes to bulk create when dont-show is checked', () => {
            expect(getNextRoute(true, true)).toBe(
                ROUTE_BULK_CREATE_DIGITAL_AUDIO
            );
        });
    });
});
