import "@boldpenguin/sdk-utility";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkSelectInputContainer } from "./bp-sdk-select-input-container";
const fakeStore = {
    config: {
        options: {
            enableAutocomplete: true,
        },
    },
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-select-input-container></bp-sdk-select-input-container>',
        components: [BpSdkSelectInputContainer],
    });
};
const setupInstance = (page) => {
    return page.rootInstance;
};
describe('bp-sdk-select-input-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-select-input');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-select-input-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-select-input-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    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('mapStateToProps', () => {
        it('returns the Google API token', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(fakeStore);
            expect(result.isAutocompleteEnabled).toEqual(fakeStore.config.options.enableAutocomplete);
        });
    });
    describe('updateAutocomplete', () => {
        const fieldType = 'text';
        const referenceType = 'street_1';
        const code = 'ia_address_1';
        const isAutocompleteEnabled = true;
        const autocomplete = 'address-line1 work';
        it('onCodeChange calls it', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.fieldType = fieldType;
            instance.referenceType = referenceType;
            instance.isAutocompleteEnabled = isAutocompleteEnabled;
            jest.spyOn(instance, 'updateAutocomplete');
            instance.onCodeChange(code);
            expect(instance.updateAutocomplete).toBeCalledWith({ fieldType, code, referenceType, isAutocompleteEnabled });
            expect(instance.updateAutocomplete).toBeCalledTimes(1);
            expect(instance.autoComplete).toEqual(autocomplete);
        });
        it('onFieldTypeChange calls it', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.referenceType = referenceType;
            instance.code = code;
            instance.isAutocompleteEnabled = isAutocompleteEnabled;
            jest.spyOn(instance, 'updateAutocomplete');
            instance.onFieldTypeChange(fieldType);
            expect(instance.updateAutocomplete).toBeCalledWith({ fieldType, code, referenceType, isAutocompleteEnabled });
            expect(instance.updateAutocomplete).toBeCalledTimes(1);
            expect(instance.autoComplete).toEqual(autocomplete);
        });
        it('onReferenceTypeChange calls it', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.fieldType = fieldType;
            instance.code = code;
            instance.isAutocompleteEnabled = isAutocompleteEnabled;
            jest.spyOn(instance, 'updateAutocomplete');
            instance.onReferenceTypeChange(referenceType);
            expect(instance.updateAutocomplete).toBeCalledWith({ fieldType, code, referenceType, isAutocompleteEnabled });
            expect(instance.updateAutocomplete).toBeCalledTimes(1);
            expect(instance.autoComplete).toEqual(autocomplete);
        });
        it('onIsAutocompleteEnabledChange calls it', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.referenceType = referenceType;
            instance.fieldType = fieldType;
            instance.code = code;
            jest.spyOn(instance, 'updateAutocomplete');
            instance.onIsAutocompleteEnabledChange(isAutocompleteEnabled);
            expect(instance.updateAutocomplete).toBeCalledWith({ fieldType, code, referenceType, isAutocompleteEnabled });
            expect(instance.updateAutocomplete).toBeCalledTimes(1);
            expect(instance.autoComplete).toEqual(autocomplete);
        });
        it('returns undefined if isAutocompleteEnabled is false', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.referenceType = referenceType;
            instance.fieldType = fieldType;
            instance.code = code;
            instance.onIsAutocompleteEnabledChange(false);
            expect(instance.autoComplete).toBeUndefined();
        });
    });
    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);
        });
    });
});
