import { Storefront } from "@boldpenguin/sdk-requests";
import { BpEnvironmentName } from "@boldpenguin/sdk-types";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkCreateAppFormContainer } from "./bp-sdk-create-app-form-container";
jest.mock('@boldpenguin/sdk-utility', () => ({
    getCoreOptions: jest.fn(),
}));
jest.mock('@boldpenguin/sdk-auth', () => ({
    getAuthConfig: jest.fn(),
}));
jest.mock('@boldpenguin/sdk-requests', () => ({
    Storefront: {
        getStorefrontByCode: jest.fn(),
    },
}));
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-create-app-form-container></bp-sdk-create-app-form-container>',
        components: [BpSdkCreateAppFormContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.storeUnsubscribe = jest.fn();
    jest.spyOn(instance, 'loadGuestApplicationForm').mockImplementation(jest.fn());
    instance.storefrontCode = 'geico1';
    instance.peConfig = {
        uri: 'http://test.local',
        env: BpEnvironmentName.Alpha,
    };
    return instance;
};
describe('bp-sdk-create-app-form-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        const element = page.root.querySelector('bp-sdk-create-app-form');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-create-app-form-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-create-app-form-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('createAppFormHandler', () => {
        it('makes expected calls', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            jest.spyOn(Storefront, 'getStorefrontByCode').mockReturnValueOnce(Promise.resolve({
                parsedBody: {
                    tenant_id: 'tid',
                    code: 'sfcode',
                    agency_name: 'agencyname',
                },
            }));
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.createApp();
            expect(Storefront.getStorefrontByCode).toBeCalledTimes(1);
            whenDefinedMock.mockRestore();
        });
    });
});
