jest.mock('@boldpenguin/sdk-utility', () => (Object.assign({
    // eslint-disable-next-line @typescript-eslint/naming-convention
    __esModule: true
}, jest.requireActual('@boldpenguin/sdk-utility'))));
import * as utils from "@boldpenguin/sdk-utility";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkVehicleVinInputContainer } from "./bp-sdk-vehicle-vin-input-container";
describe('bp-sdk-vehicle-vin-input-container', () => {
    it('renders', async () => {
        const page = await newSpecPage({
            html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
            components: [BpSdkVehicleVinInputContainer],
        });
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-vehicle-vin-input');
        expect(element).toBeTruthy();
    });
    it('renders external component if defined', async () => {
        const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
        const page = await newSpecPage({
            html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
            components: [BpSdkVehicleVinInputContainer],
        });
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-vehicle-vin-input');
        expect(element).toBeFalsy();
        whenDefinedMock.mockRestore();
    });
    describe('componentDidLoad', () => {
        it('makes expected calls', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            jest.spyOn(instance, 'loadFocusEvents').mockImplementation(() => { });
            instance.componentDidLoad();
            expect(instance.loadFocusEvents).toBeCalledTimes(1);
        });
    });
    describe('disconnectedCallback', () => {
        it('makes expected calls', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            jest.spyOn(instance, 'unloadFocusEvents').mockImplementation(() => { });
            instance.disconnectedCallback();
            expect(instance.unloadFocusEvents).toBeCalledTimes(1);
        });
    });
    describe('loadFocusEvents', () => {
        it('sets expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.useExternal = false;
            jest.spyOn(instance.el, 'addEventListener').mockReturnValue();
            await page.waitForChanges();
            instance.loadFocusEvents();
            expect(instance.el.addEventListener).toBeCalledTimes(1);
            expect(instance.el.addEventListener).toBeCalledWith('focusout', instance.onFocusOut);
        });
        it('handles truthy useExternal', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.useExternal = true;
            jest.spyOn(instance.el, 'addEventListener').mockReturnValue();
            await page.waitForChanges();
            instance.loadFocusEvents();
            expect(instance.el.addEventListener).toBeCalledTimes(0);
        });
    });
    describe('unloadFocusEvents', () => {
        it('sets expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            jest.spyOn(instance.el, 'removeEventListener').mockImplementation(() => { });
            await page.waitForChanges();
            instance.unloadFocusEvents();
            expect(instance.el.removeEventListener).toBeCalledTimes(1);
            expect(instance.el.removeEventListener).toBeCalledWith('focusout', instance.onFocusOut);
        });
    });
    describe('onMakeDirty', () => {
        it('should ignore unchanged touched state', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.touched = false;
            instance.pristine = true;
            instance.onMakeDirty();
            expect(instance.touched).toBeTruthy();
            expect(instance.pristine).toBeFalsy();
        });
    });
    describe('onValueUpdate', () => {
        it('sets expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.pristine = true;
            instance.onValueUpdate({ detail: [''] });
            expect(instance.pristine).toBeFalsy();
            expect(instance.value).toEqual('');
        });
        it('handles null details', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.pristine = true;
            instance.onValueUpdate({ detail: undefined });
            expect(instance.pristine).toBeFalsy();
            expect(instance.value).toBeUndefined();
        });
        it('handles truthy useExternal', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.useExternal = true;
            instance.pristine = true;
            instance.onValueUpdate({ detail: [''] });
            expect(instance.pristine).toBeTruthy();
            expect(instance.value).toBeUndefined();
        });
        it('emits event if no errors', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.answerId = '123';
            jest.spyOn(instance.vinUpdate, 'emit').mockReturnValue(null);
            instance.onValueUpdate({ detail: [''] });
            expect(instance.vinUpdate.emit).toHaveBeenCalledWith({ vin: '', answerId: '123' });
        });
        it('does not emit event if there are errors', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.errors = ['required'];
            jest.spyOn(instance, 'updateErrors').mockReturnValue(null);
            jest.spyOn(instance.vinUpdate, 'emit').mockReturnValue(null);
            instance.onValueUpdate({ detail: [''] });
            expect(instance.vinUpdate.emit).not.toHaveBeenCalled();
        });
    });
    describe('onFocusOut', () => {
        it('sets expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            instance.touched = false;
            instance.onFocusOut();
            expect(instance.touched).toBeTruthy();
        });
    });
    describe('updateErrors', () => {
        it('returns required if error determined', async () => {
            jest.spyOn(utils, 'hasRequiredError').mockReturnValue(true);
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            await page.waitForChanges();
            instance.updateErrors(false, false, '');
            expect(instance.errors).toEqual(['required']);
        });
        it('otherwise empty state', async () => {
            jest.spyOn(utils, 'hasRequiredError').mockReturnValue(false);
            const page = await newSpecPage({
                html: '<bp-sdk-vehicle-vin-input-container></bp-sdk-vehicle-vin-input-container>',
                components: [BpSdkVehicleVinInputContainer],
            });
            const instance = page.rootInstance;
            await page.waitForChanges();
            instance.updateErrors(false, false, '');
            expect(instance.errors).toEqual([]);
        });
    });
});
