import { newSpecPage } from "@stencil/core/testing";
import { BpSdkMaskedInputContainer } from "./bp-sdk-masked-input-container";
const fakeStore = {
    config: {
        options: {
            enableAutocomplete: true,
        },
    },
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-masked-input-container></bp-sdk-masked-input-container>',
        components: [BpSdkMaskedInputContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-masked-input-container', () => {
    describe('renders', () => {
        it('vanilla', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            expect(page.body.querySelector('bp-sdk-masked-input')).toBeTruthy();
        });
        it('renders external component if defined', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await setupPage();
            await page.waitForChanges();
            expect(page.body.querySelector('bp-sdk-masked-input')).toBeFalsy();
            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('onFieldTypeChange', () => {
        it('phone', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.onFieldTypeChange('phone');
            expect(instance.mask).toEqual('(999) 999-9999');
            expect(instance.options).toEqual({
                placeholder: '#',
            });
            expect(instance.canHide).toBeFalsy();
            expect(instance.useComplexMaskLogic).toBeFalsy();
        });
        it('currency', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.onFieldTypeChange('currency');
            expect(instance.mask).toEqual('integer');
            expect(instance.options).toEqual({
                placeholder: '',
                prefix: '$',
            });
            expect(instance.canHide).toBeFalsy();
            expect(instance.useComplexMaskLogic).toBeFalsy();
        });
        it('date', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.onFieldTypeChange('date');
            expect(instance.mask).toEqual('datetime');
            expect(instance.options).toEqual({
                placeholder: '#',
                inputFormat: 'mm/dd/yyyy',
            });
            expect(instance.canHide).toBeTruthy();
            expect(instance.useComplexMaskLogic).toBeTruthy();
        });
        it('percentage', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.onFieldTypeChange('percentage');
            expect(instance.mask).toEqual('999%');
            expect(instance.options).toEqual({
                placeholder: ' ',
            });
            expect(instance.canHide).toBeFalsy();
            expect(instance.useComplexMaskLogic).toBeFalsy();
        });
        it('fein', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = setupInstance(page);
            instance.onFieldTypeChange('fein');
            expect(instance.mask).toEqual('99-9999999');
            expect(instance.options).toEqual({
                placeholder: '#',
            });
            expect(instance.canHide).toBeTruthy();
            expect(instance.useComplexMaskLogic).toBeTruthy();
        });
        it('ssn', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = page.rootInstance;
            instance.onFieldTypeChange('ssn');
            expect(instance.mask).toEqual('999-99-9999');
            expect(instance.options).toEqual({
                placeholder: '#',
            });
            expect(instance.canHide).toBeTruthy();
            expect(instance.useComplexMaskLogic).toBeTruthy();
        });
    });
    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);
        });
    });
});
