import { newSpecPage } from "@stencil/core/testing";
import { BpSdkGooglePlacesAutocompleteContainer } from "./bp-sdk-google-places-autocomplete-container";
const fakeStore = {
    config: {
        googleApiToken: 'testToken',
        options: {
            enableAutocomplete: true,
        },
    },
};
const setupPage = async () => await newSpecPage({
    html: '<bp-sdk-google-places-autocomplete-container></bp-sdk-google-places-autocomplete-container>',
    components: [BpSdkGooglePlacesAutocompleteContainer],
});
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-google-places-autocomplete-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        expect(page.root.querySelector('bp-sdk-google-places-autocomplete')).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-google-places-autocomplete-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-google-places-autocomplete')).toBeFalsy();
        expect(page.root.querySelector('bp-sdk-external-google-places-autocomplete-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('onPlacesUpdate', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'removeActiveAnswerId').mockImplementation(jest.fn());
            instance.useExternal = false;
            instance.onPlacesUpdate();
            expect(instance.removeActiveAnswerId).toHaveBeenCalledTimes(1);
        });
        it('does nothing if external', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'removeActiveAnswerId').mockImplementation(jest.fn());
            instance.useExternal = true;
            instance.onPlacesUpdate();
            expect(instance.removeActiveAnswerId).toHaveBeenCalledTimes(0);
        });
    });
    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.googleApiToken).toEqual(fakeStore.config.googleApiToken);
            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);
        });
    });
});
