import { mockWindow, newSpecPage } from "@stencil/core/testing";
import { BpSdkQuoteRequestButtonContainer } from "./bp-sdk-quote-request-button-container";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-quote-request-button-container></bp-sdk-quote-request-button-container>',
        components: [BpSdkQuoteRequestButtonContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-quote-request-button-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('div');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-quote-request-button-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-quote-request-button-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('on button click', () => {
        it('should call handleBind with the parentQuoteRequestId', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.appFormId = 'appFormId';
            instance.quote = {
                parentQuoteRequestId: 'parentQuoteRequestId',
                id: 'id',
            };
            mockWindow();
            instance.handleBind = jest.fn();
            const fn = jest.fn();
            instance.handleAfterBindResponse = jest.fn(() => fn);
            await page.waitForChanges();
            page.root.querySelector('bp-sdk-quote-request-button').click();
            expect(instance.handleBind).toBeCalledTimes(1);
            expect(instance.handleBind).toBeCalledWith(instance.quote.parentQuoteRequestId, instance.appFormId, fn);
        });
        it('should call handleBind falling back to quote.id', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.appFormId = 'appFormId';
            instance.quote = {
                parentQuoteRequestId: undefined,
                id: 'id',
            };
            mockWindow();
            instance.handleBind = jest.fn();
            const fn = jest.fn();
            instance.handleAfterBindResponse = jest.fn(() => fn);
            await page.waitForChanges();
            page.root.querySelector('bp-sdk-quote-request-button').click();
            expect(instance.handleBind).toBeCalledTimes(1);
            expect(instance.handleBind).toBeCalledWith(instance.quote.id, instance.appFormId, fn);
        });
        it('should not trigger click events if disabled', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isQuoteExpired = true;
            instance.onBind = jest.fn();
            await page.waitForChanges();
            page.root.querySelector('bp-sdk-quote-request-button').click();
            expect(page.rootInstance.onBind).not.toHaveBeenCalled();
        });
    });
    describe('handleAfterBindResponse', () => {
        it('should make expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.appFormId = 'appFormId';
            instance.quoteId = 'quoteId';
            instance.quoteWindow = {
                location: {},
            };
            await page.waitForChanges();
            instance.handleAfterBindResponse(instance.quoteWindow)('response');
            expect(instance.quoteWindow.location.href).toEqual('response');
        });
    });
});
