import { newSpecPage } from "@stencil/core/testing";
import { BpSdkDriverQuestionPoolContainer } from "./bp-sdk-driver-question-pool-container";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-driver-question-pool-container></bp-sdk-driver-question-pool-container>',
        components: [BpSdkDriverQuestionPoolContainer],
    });
};
const setupInstance = (page) => {
    return page.rootInstance;
};
describe('bp-sdk-driver-question-pool-container', () => {
    it('hides if no pool id', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-driver-group');
        expect(element).toBeFalsy();
        expect(page.root.querySelector('bp-sdk-external-driver-question-pool-container')).toBeFalsy();
    });
    it('renders', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = {
            poolId: 'poolId',
        };
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-driver-group');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-driver-question-pool-container')).toBeFalsy();
    });
    it('renders external component if defined', async () => {
        const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
        const page = await setupPage();
        await page.waitForChanges();
        expect(page.root.querySelector('bp-sdk-external-driver-question-pool-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('handleRemoveItem', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'removeQuestionPool').mockImplementation(jest.fn().mockResolvedValueOnce(undefined));
            instance.isDeleting = false;
            instance.handleDeleteItem();
            expect(instance.removeQuestionPool).toBeCalledTimes(1);
            expect(instance.isDeleting).toBeTruthy();
        });
    });
    describe('mapStateToProps', () => {
        it('returns expected result', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            expect(instance.mapStateToProps({
                application: {
                    form: {
                        id: 'bob',
                    },
                    questionSets: {
                        list: [],
                    },
                },
                auth: {},
                config: {},
            })).toEqual({
                applicationId: 'bob',
                config: {},
                questionSets: [],
            });
        });
    });
    describe('disconnectedCallback', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.storeUnsubscribe = jest.fn();
            instance.disconnectedCallback();
            expect(instance.storeUnsubscribe).toBeCalledTimes(1);
        });
    });
    describe('handleSubscriptions', () => {
        it('calls unsubscribe if subscribed', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(unsubscribe).toHaveBeenCalled();
        });
        it('overwrites an existing subscribe function', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toEqual(unsubscribe);
            expect(instance.storeUnsubscribe).toBeDefined();
        });
        it('subscribes if not subscribed', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.storeUnsubscribe = null;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toBeNull();
        });
        it('subscribes updates mapStateToProps if already has storeUnsubscribe', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toEqual(unsubscribe);
        });
    });
    describe('connectedCallback', () => {
        it('makes expected call if root available', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.handleSubscriptions = jest.fn();
            jest.spyOn(instance.el, 'querySelector').mockImplementationOnce(() => ({
                componentOnReady: () => Promise.resolve(true),
            }));
            instance.connectedCallback();
            await page.waitForChanges();
            expect(instance.handleSubscriptions).toBeCalledTimes(1);
        });
    });
});
