import { newSpecPage } from "@stencil/core/testing";
import { BpSdkTakeOverContainer } from "./bp-sdk-take-over-container";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-take-over-container></bp-sdk-take-over-container>',
        components: [BpSdkTakeOverContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.storeUnsubscribe = jest.fn();
    jest.spyOn(instance, 'cloneApplicationForm').mockImplementation(jest.fn());
    instance.appFormId = 'abc123';
    return instance;
};
describe('bp-sdk-take-over-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        const element = page.root.querySelector('bp-sdk-take-over');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-take-over-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-take-over-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('disconnectedCallback', () => {
        it('makes expected calls', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.disconnectedCallback();
            expect(instance.storeUnsubscribe).toBeCalledTimes(1);
            whenDefinedMock.mockRestore();
        });
    });
    describe('takeOverHandler', () => {
        it('makes expected calls', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.takeOverHandler();
            expect(instance.cloneApplicationForm).toBeCalledTimes(1);
            expect(instance.cloneApplicationForm).toBeCalledWith({
                config: instance.config,
                appFormId: instance.appFormId,
            });
            whenDefinedMock.mockRestore();
        });
    });
});
