import { newSpecPage } from "@stencil/core/testing";
import { BpSdkDeleteQuestionPoolItemContainer } from "./bp-sdk-delete-question-pool-item-container";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-delete-question-pool-item-container></bp-sdk-delete-question-pool-item-container>',
        components: [BpSdkDeleteQuestionPoolItemContainer],
    });
};
const setupInstance = (page) => {
    return page.rootInstance;
};
describe('bp-sdk-delete-question-pool-item-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        expect(page.root.querySelector('div')).toBeTruthy();
        const element = page.root.querySelector('bp-sdk-delete-question-pool-item');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-delete-question-pool-item-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-delete-question-pool-item-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('handleRemoveItem', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'removeQuestionPool').mockImplementationOnce(jest.fn());
            instance.handleRemoveItem();
            expect(instance.removeQuestionPool).toBeCalledTimes(1);
        });
    });
    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: {},
            });
        });
    });
    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);
        });
    });
});
