import { newSpecPage } from "@stencil/core/testing";
import { BpSdkDriverInputContainer } from "./bp-sdk-driver-input-container";
const fakeStore = {
    application: {
        questionSets: {
            list: [{ question_set_id: '', answers: [{ question: { reference_type: 'drivers_license_state' } }] }],
            active: '',
        },
    },
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-driver-input-container></bp-sdk-driver-input-container>',
        components: [BpSdkDriverInputContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-driver-input-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-text-input-container');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-driver-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-driver-input-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('onSaveAnswer', () => { });
    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('subscriptions resubscribes if previous subscription exists', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(unsubscribe).toHaveBeenCalledTimes(1);
            expect(instance.storeUnsubscribe).not.toBe(unsubscribe);
        });
    });
    describe('onSaveAnswer', () => {
        it('does nothing if external', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.useExternal = true;
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'something' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeNull();
        });
        it('sets to undefined valid', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.licenseError = null;
            instance.driverLicenseStateAnswer = {
                question: {
                    choices: [{ alias: 'NY' }],
                },
            };
            const event = new CustomEvent('valueUpdate', { detail: 'A1234567' });
            event.stopPropagation = jest.fn();
            instance.fakeValueUpdate.emit = jest.fn();
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
            expect(event.stopPropagation).not.toBeCalled();
            expect(instance.fakeValueUpdate.emit).not.toBeCalled();
        });
        it('makes expected calls if in license error', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.driverLicenseStateAnswer = {
                question: {
                    choices: [{ alias: 'NY' }],
                },
            };
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'A123456' });
            event.stopPropagation = jest.fn();
            instance.fakeValueUpdate.emit = jest.fn();
            instance.onSaveAnswer(event);
            await page.waitForChanges();
            expect(instance.licenseError).toEqual(BpSdkDriverInputContainer.ERROR_MESSAGE);
            expect(event.stopPropagation).toBeCalledTimes(1);
            expect(instance.fakeValueUpdate.emit).toBeCalledTimes(1);
        });
        it('sets to undefined error if no state', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'A1234567' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
        });
        it('sets to undefined error if no detail', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.driverLicenseStateAnswer = {
                question: {
                    choices: [{ alias: 'NY' }],
                },
            };
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: '' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
        });
        it('sets to any state if no alias', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.driverLicenseStateAnswer = {
                question: {
                    choices: [{}],
                },
            };
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'A1234567' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
        });
        it('handles no driver answer', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.driverLicenseStateAnswer = undefined;
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'A1234567' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
        });
        it('sets to any state if no alias', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.driverLicenseStateAnswer = {
                question: {
                    choices: [{ alias: 'NY' }],
                },
            };
            instance.licenseError = null;
            const event = new CustomEvent('valueUpdate', { detail: 'A1234567' });
            instance.onSaveAnswer(event);
            expect(instance.licenseError).toBeUndefined();
        });
    });
    describe('mapStateToProps', () => {
        it('returns drivers license question', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(fakeStore);
            expect(result.driverLicenseStateAnswer).toEqual(fakeStore.application.questionSets.list[0].answers[0]);
        });
    });
});
