jest.mock('@boldpenguin/sdk-utility', () => (Object.assign(Object.assign({}, jest.requireActual('@boldpenguin/sdk-utility')), { focusErrors: jest.fn(() => { }), getGroupId: jest.fn(), getCommercialAutoFlowQuestion: jest.fn() })));
import { BpEnvironmentName, LoadingStates, VehicleFlowTypes, VehicleTypes, } from "@boldpenguin/sdk-types";
import { focusErrors, getCommercialAutoFlowQuestion } from "@boldpenguin/sdk-utility";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkVehiclePanelContainer } from "./bp-sdk-vehicle-panel-container";
const defaultStore = {
    config: {
        uri: 'https://test.local',
        env: BpEnvironmentName.Alpha,
    },
    auth: {
        token: 'test_token',
    },
    application: {
        form: {
            id: 'testAppFormId',
        },
    },
    viewState: {
        vehicleOptions: {
            years: {
                testQuestionGroupId: {
                    2022: 2022,
                },
            },
            makes: {
                testQuestionGroupId: {
                    testMakeId: 'Rivian',
                },
            },
            models: {
                testQuestionGroupId: {
                    testModelId: 'R1T',
                },
            },
            configurations: {
                testQuestionGroupId: {
                    testConfigurationId: 'Launch Edition',
                },
            },
            selection: {
                testQuestionGroupId: {
                    yearId: '2022',
                    makeId: 'testMakeId',
                    modelId: 'testModelId',
                    configurationId: 'testConfigurationId',
                },
            },
        },
    },
    status: {
        remote: {
            vehicles: {
                selection: {
                    testQuestionGroupId: LoadingStates.NotLoaded,
                },
            },
        },
    },
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-vehicle-panel-container></bp-sdk-vehicle-panel-container>',
        components: [BpSdkVehiclePanelContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.questionGroup = {
        groupId: 'testQuestionGroupId',
        parentQuestionAnswer: {
            answer_pool_id: 'testAnswerPoolId',
        },
    };
    jest.spyOn(instance, 'loadYears').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadModels').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadConfigurations').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadSingleQuestionSet').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadSingleApplicationForm').mockImplementation(jest.fn());
    jest.spyOn(instance, 'selectVehicle').mockImplementation(jest.fn());
    jest.spyOn(instance, 'resetSelectedVehicle').mockImplementation(jest.fn());
    jest.spyOn(instance, 'loadFullVehicleOptions').mockImplementation(jest.fn());
    jest.spyOn(instance, 'setVehicleSelection').mockImplementation(jest.fn());
    return instance;
};
describe('bp-sdk-vehicle-panel-container', () => {
    describe('renders', () => {
        it('renders', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-vehicle-panel');
            expect(element).toBeTruthy();
        });
        it('renders with no question group', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.questionGroup = undefined;
            instance.yearOptions = defaultStore.viewState.vehicleOptions.years.questionGroupId;
            instance.selectionStatus = defaultStore.status.remote.vehicles.selection.questionGroupId;
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-vehicle-panel');
            expect(element).toBeTruthy();
        });
        it("can't delete if can't edit", async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.yearOptions = defaultStore.viewState.vehicleOptions.years.questionGroupId;
            instance.selectionStatus = defaultStore.status.remote.vehicles.selection.questionGroupId;
            instance.canDelete = true;
            instance.canEdit = false;
            instance.questionGroup = {
                answers: [],
            };
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-vehicle-panel');
            expect(element.getAttribute('can-delete')).toBeFalsy();
        });
        it('renders external component if defined', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await setupPage();
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-vehicle-panel');
            expect(element).toBeFalsy();
            whenDefinedMock.mockRestore();
        });
    });
    describe('componentDidLoad', () => { });
    describe('disconnectedCallback', () => {
        it('makes the storeUnsubscribe call', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.storeUnsubscribe = jest.fn();
            instance.disconnectedCallback();
            expect(instance.storeUnsubscribe).toBeCalledTimes(1);
        });
    });
    describe('onOptionsUpdate', () => {
        it('handles a year event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'year', value: '2192' } });
            expect(instance.loadMakes).toBeCalledTimes(1);
            expect(instance._currentYear).toEqual(2192);
        });
        it('handles null detail', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            jest.spyOn(instance, 'loadConfigurations').mockImplementation(jest.fn());
            jest.spyOn(instance, 'loadModels').mockImplementation(jest.fn());
            jest.spyOn(instance, 'setVehicleSelection').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: undefined });
            expect(instance.loadMakes).toBeCalledTimes(0);
            expect(instance.loadModels).toBeCalledTimes(0);
            expect(instance.loadConfigurations).toBeCalledTimes(0);
            expect(instance.setVehicleSelection).toBeCalledTimes(0);
        });
        it('ignores falsy year event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'year', value: undefined } });
            expect(instance.loadMakes).toBeCalledTimes(0);
            expect(instance._currentYear).toBeUndefined();
        });
        it('ignores 0 year event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'year', value: '0' } });
            expect(instance.loadMakes).toBeCalledTimes(0);
            expect(instance._currentYear).toBeUndefined();
        });
        it('ignores nan year event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'year', value: 'abc' } });
            expect(instance.loadMakes).toBeCalledTimes(0);
            expect(instance._currentYear).toBeUndefined();
        });
        it('handles a make event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadModels').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'make', value: 'testMakeId', displayValue: 'displayValue' } });
            expect(instance.loadModels).toBeCalledTimes(1);
            expect(instance._currentMakeId).toEqual('testMakeId');
            expect(instance.makeDisplayValue).toEqual('displayValue');
        });
        it('ignores falsy make event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'make', value: undefined } });
            expect(instance.loadModels).toBeCalledTimes(0);
            expect(instance._currentMakeId).toBeUndefined();
        });
        it('handles a model event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadConfigurations').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'model', value: 'testModelId' } });
            expect(instance.loadConfigurations).toBeCalledTimes(1);
            expect(instance._currentModelId).toEqual('testModelId');
        });
        it('ignores falsy make event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'model', value: undefined } });
            expect(instance.loadConfigurations).toBeCalledTimes(0);
            expect(instance._currentModelId).toBeUndefined();
        });
        it('handles a configuration event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            instance.onOptionsUpdate({ detail: { type: 'configuration', value: 'testConfigurationId' } });
            expect(instance._currentConfigId).toEqual('testConfigurationId');
        });
        it('ignores falsy make event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.config = {};
            jest.spyOn(instance, 'loadMakes').mockImplementation(jest.fn());
            instance.onOptionsUpdate({ detail: { type: 'configuration', value: undefined } });
            expect(instance._currentConfigId).toBeUndefined();
        });
    });
    describe('onVehicleSave', () => {
        it('handles a ymm event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'selectVehicle').mockImplementation(jest.fn(() => Promise.resolve(true)));
            instance.config = {};
            instance.appFormId = 'testAppFormId';
            instance._currentConfigId = 'currentConfigId';
            instance.onVehicleSave({ detail: VehicleFlowTypes.ymm });
            expect(instance.selectVehicle).toBeCalledWith({
                answerPoolId: instance.questionGroup.poolId,
                appId: instance.appFormId,
                questionGroupId: instance.questionGroup.groupId,
                configurationId: instance._currentConfigId,
                flowType: VehicleFlowTypes.ymm,
                config: instance.config,
                callback: instance.onVehicleSaveCallback,
            });
        });
        it('handles a vin event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'selectVehicle').mockImplementation(jest.fn(() => Promise.resolve(true)));
            instance.config = {};
            instance.appFormId = 'testAppFormId';
            instance._currentConfigId = 'currentConfigId';
            instance.onVehicleSave({ detail: VehicleFlowTypes.vin });
            expect(instance.selectVehicle).toBeCalledWith({
                appId: instance.appFormId,
                questionGroupId: instance.questionGroup.groupId,
                configurationId: instance._currentConfigId,
                flowType: VehicleFlowTypes.vin,
                config: instance.config,
                callback: instance.onVehicleSaveCallback,
            });
        });
        it('handles a free-form trailer flow', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'selectVehicle').mockImplementation(jest.fn(() => Promise.resolve(true)));
            instance.config = {};
            instance.appFormId = 'testAppFormId';
            instance._currentYear = 1234;
            instance.makeDisplayValue = 'makeDisplay';
            instance._currentModelId = 'model';
            instance.onVehicleSave({ detail: VehicleFlowTypes.trailers });
            expect(instance.selectVehicle).toBeCalledWith({
                appId: instance.appFormId,
                questionGroupId: instance.questionGroup.groupId,
                configurationId: undefined,
                flowType: VehicleFlowTypes.trailers,
                type: VehicleFlowTypes.trailers,
                config: instance.config,
                callback: instance.onVehicleSaveCallback,
                vehicle: {
                    yearId: instance._currentYear,
                    makeId: instance.makeDisplayValue,
                    modelId: instance._currentModelId,
                },
            });
        });
        it('handles no type', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'selectVehicle').mockImplementation(jest.fn(() => Promise.resolve(true)));
            instance.config = {};
            instance.appFormId = 'testAppFormId';
            instance._currentConfigId = 'currentConfigId';
            instance.isEditing = true;
            instance.onVehicleSave({ detail: undefined });
            expect(instance.selectVehicle).toBeCalledTimes(0);
        });
    });
    describe('editVehicle', () => {
        it('makes expected changes', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const event = {
                stopPropagation: jest.fn(),
            };
            expect(instance.isEditing).toBeFalsy();
            instance.editVehicle(event);
            expect(instance.isEditing).toBeTruthy();
            expect(event.stopPropagation).toBeCalledTimes(1);
        });
        it('calls loadYears action creator when no vehicle options', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const event = {
                stopPropagation: jest.fn(),
            };
            instance.yearOptions = undefined;
            instance.editVehicle(event);
            expect(instance.loadYears).toHaveBeenCalledTimes(1);
        });
        it('calls loadYears action creator when years missing from vehicle options', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const event = {
                stopPropagation: jest.fn(),
            };
            instance.yearOptions = undefined;
            instance.editVehicle(event);
            expect(instance.loadYears).toHaveBeenCalledTimes(1);
        });
        it('doesnt call loadYears action creator when years are already loaded', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const event = {
                stopPropagation: jest.fn(),
            };
            instance.yearOptions = {
                '2018': 2018,
            };
            instance.editVehicle(event);
            expect(instance.loadYears).not.toHaveBeenCalled();
        });
    });
    describe('deleteVehicle', () => {
        it('makes expected changes', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.deleteItem, 'emit').mockImplementation((() => { }));
            instance.code = '34';
            instance.deleteVehicle();
            expect(instance.deleteItem.emit).toBeCalledWith(instance.code);
        });
    });
    describe('onOpen', () => {
        it('makes expected changes', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isEditing = true;
            instance.canEdit = false;
            instance.onOpen();
            expect(instance.isEditing).toBeFalsy();
            expect(instance.canEdit).toBeTruthy();
        });
    });
    describe('onClose', () => {
        it('makes expected changes', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isEditing = true;
            instance.canEdit = true;
            instance.onClose();
            expect(instance.isEditing).toBeFalsy();
            expect(instance.canEdit).toBeFalsy();
        });
    });
    describe('populateVehicleOptions', () => {
        let questionGroup;
        beforeEach(() => {
            questionGroup = {
                answers: [
                    {
                        id: '9d100cd8-94e4-48ad-9f0d-0690e9e2f8db',
                        value: '{"id":830703,"configuration_id":830703,"year":1995,"make":"DODGE","make_id":14,"model":"3500 HD","model_id":280857,"category":"Light Duty Trucks","name":"Ram Custom Series 4x4 Conventional Chs \u0026 Cab","description":"Ram Custom Series 4x4 Conventional Chs \u0026 Cab","msrp":"20414","gcw":null,"gvw":"10500","tractor":"No","anti_lock_brakes":"No","engine_cc":null,"shown":false,"unadjusted_retail":2596}',
                        is_completed: true,
                        question: {
                            reference_type: 'ca_json',
                        },
                    },
                ],
            };
        });
        it('handles no value on answer', async () => {
            questionGroup = {
                answers: [
                    {
                        id: '9d100cd8-94e4-48ad-9f0d-0690e9e2f8db',
                        value: undefined,
                        is_completed: true,
                        question: {
                            reference_type: 'ca_json',
                        },
                    },
                ],
            };
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(questionGroup);
            expect(instance.loadFullVehicleOptions).toHaveBeenCalledTimes(0);
        });
        it('handles empty value for json', async () => {
            questionGroup = {
                answers: [
                    {
                        id: '9d100cd8-94e4-48ad-9f0d-0690e9e2f8db',
                        value: '',
                        is_completed: true,
                        question: {
                            reference_type: 'ca_json',
                        },
                    },
                ],
            };
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(questionGroup);
            expect(instance.loadFullVehicleOptions).toHaveBeenCalledTimes(0);
        });
        it('handles missing information', async () => {
            questionGroup = {
                answers: [
                    {
                        id: '9d100cd8-94e4-48ad-9f0d-0690e9e2f8db',
                        value: '{}',
                        is_completed: true,
                        question: {
                            reference_type: 'ca_json',
                        },
                    },
                ],
            };
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(questionGroup);
            expect(instance.loadFullVehicleOptions).toHaveBeenCalledTimes(0);
        });
        it('calls loadFullVehicleOptions', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(questionGroup);
            expect(instance.loadFullVehicleOptions).toHaveBeenCalledTimes(1);
        });
        it('calls setVehicleSelection', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(questionGroup);
            expect(instance.setVehicleSelection).toHaveBeenCalledTimes(1);
        });
        it('should not dispatch actions when questionGroup is not yet loaded', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.populateVehicleOptions(undefined);
            expect(instance.loadFullVehicleOptions).not.toHaveBeenCalled();
            expect(instance.setVehicleSelection).not.toHaveBeenCalled();
        });
        describe('internal state', () => {
            it('sets _currentYear', async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance._currentYear = 0;
                instance.populateVehicleOptions(questionGroup);
                expect(instance._currentYear).toEqual(1995);
            });
            it('sets _currentMakeId', async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance._currentMakeId = 'beforeMake';
                instance.populateVehicleOptions(questionGroup);
                expect(instance._currentMakeId).toEqual('14');
            });
            it('sets _currentModelId', async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance._currentModelId = 'beforeModel';
                instance.populateVehicleOptions(questionGroup);
                expect(instance._currentModelId).toEqual('280857');
            });
            it('sets _currentConfigId', async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance._currentConfigId = 'beforeConfig';
                instance.populateVehicleOptions(questionGroup);
                expect(instance._currentConfigId).toEqual('830703');
            });
        });
    });
    describe('onVehicleInformationChange', () => {
        it('sets true if vehicle is fully defined', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isValid = false;
            instance.onVehicleInformationChange({
                make: 'some make',
                model: 'some model',
                year: '0000',
            });
            expect(instance.isValid).toBeTruthy();
        });
        it('sets false if missing year', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isValid = false;
            instance.onVehicleInformationChange({
                make: 'some make',
                model: 'some model',
                year: '',
            });
            expect(instance.isValid).toBeFalsy();
        });
        it('sets false if missing make', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isValid = false;
            instance.onVehicleInformationChange({
                make: '',
                model: 'some model',
                year: '0000',
            });
            expect(instance.isValid).toBeFalsy();
        });
        it('sets false if missing model', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.isValid = false;
            instance.onVehicleInformationChange({
                make: 'some make',
                model: '',
                year: '0000',
            });
            expect(instance.isValid).toBeFalsy();
        });
    });
    describe('onVehicleResourceChange', () => {
        it('calls resetSelectedVehicle when LoadingStates.Loaded', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'resetSelectedVehicle').mockImplementation(jest.fn());
            instance.isEditing = true;
            instance.onVehicleResourceChange(LoadingStates.Loaded);
            expect(instance.resetSelectedVehicle).toHaveBeenCalled();
        });
        it('does nothing for other LoadingStates', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'resetSelectedVehicle').mockImplementation(jest.fn());
            instance.isEditing = true;
            instance.onVehicleResourceChange(LoadingStates.Loading);
            expect(instance.resetSelectedVehicle).not.toHaveBeenCalled();
        });
        it('does nothing if not editing', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'resetSelectedVehicle').mockImplementation(jest.fn());
            instance.isEditing = false;
            instance.onVehicleResourceChange(LoadingStates.Loaded);
            expect(instance.resetSelectedVehicle).not.toHaveBeenCalled();
        });
    });
    describe('mapStateToProps', () => {
        it('returns an object with the expected keys', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.questionGroupId = 'testQuestionGroupId';
            const result = instance.mapStateToProps(defaultStore);
            expect(result).toHaveProperty('config');
            expect(result).toHaveProperty('appFormId');
            expect(result).toHaveProperty('selectionStatus');
            expect(result).toHaveProperty('yearOptions');
            expect(result).toHaveProperty('vehicleInformation');
        });
    });
    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(unsubscribe).toHaveBeenCalledTimes(1);
            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);
        });
    });
    describe('handleTabChanged', () => {
        it('does nothing if external', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.useExternal = true;
            instance.tabIndex = 0;
            instance.handleTabChanged({ detail: 1 });
            expect(instance.tabIndex).toEqual(0);
        });
        it('updates tab index to match detail', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.useExternal = false;
            instance.tabIndex = 0;
            instance.handleTabChanged({ detail: 1 });
            expect(instance.tabIndex).toEqual(1);
        });
    });
    describe('onQuestionGroupChange', () => {
        it('does nothing while saving', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.handleSubscriptions = jest.fn();
            instance.isSaving = true;
            instance.onQuestionGroupChange({});
            expect(instance.handleSubscriptions).toBeCalledTimes(0);
        });
        it('resets subscriptions on change', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.handleSubscriptions = jest.fn();
            instance.isSaving = false;
            instance.onQuestionGroupChange({});
            expect(instance.handleSubscriptions).toBeCalledTimes(1);
        });
    });
    describe('onMakeDirty', () => {
        it('if invalid and request is made, expand and focus errors if not in editing mode', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.onEdit = jest.fn();
            focusErrors.mockReset();
            focusErrors.mockImplementationOnce(() => undefined);
            global.setTimeout = jest.fn((f) => f());
            instance.isValid = false;
            instance.isEditing = false;
            instance.canEdit = true;
            instance.onMakeDirty();
            expect(instance.onEdit).toBeCalledTimes(1);
            expect(focusErrors).toBeCalledTimes(1);
        });
        it('if valid do nothing', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.onEdit = jest.fn();
            focusErrors.mockReset();
            focusErrors.mockImplementationOnce(() => undefined);
            global.setTimeout = jest.fn((f) => f());
            instance.isValid = true;
            instance.isEditing = false;
            instance.canEdit = true;
            instance.onMakeDirty();
            expect(instance.onEdit).not.toHaveBeenCalled();
            expect(focusErrors).not.toHaveBeenCalled();
        });
        it('if already editing do nothing', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.onEdit = jest.fn();
            focusErrors.mockReset();
            focusErrors.mockImplementationOnce(() => undefined);
            global.setTimeout = jest.fn((f) => f());
            instance.isValid = false;
            instance.isEditing = true;
            instance.canEdit = true;
            instance.onMakeDirty();
            expect(instance.onEdit).not.toHaveBeenCalled();
            expect(focusErrors).not.toHaveBeenCalled();
        });
        it('if not able editing do nothing', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.onEdit = jest.fn();
            focusErrors.mockReset();
            focusErrors.mockImplementationOnce(() => undefined);
            global.setTimeout = jest.fn((f) => f());
            instance.isValid = false;
            instance.isEditing = false;
            instance.canEdit = false;
            instance.onMakeDirty();
            expect(instance.onEdit).not.toHaveBeenCalled();
            expect(focusErrors).not.toHaveBeenCalled();
        });
    });
    describe('vinUpdate', () => {
        it('handle a vinUpdate event', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const answer = { id: '4566' };
            instance.questionGroup = {
                answers: [answer],
            };
            jest.spyOn(instance.saveAnswer, 'emit').mockImplementation();
            instance.onVinUpdate({ detail: { vin: '1234', answerId: '4566' } });
            expect(instance.saveAnswer.emit).toHaveBeenCalledWith({ answer, new_value: '1234' });
        });
    });
    describe('isTrailerTab', () => {
        it('returns true if matches order of trailer tab choice', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            getCommercialAutoFlowQuestion.mockImplementationOnce(() => ({
                question: { choices: [{ value: 'trailer', order: 0 }] },
            }));
            expect(instance.isTrailerTab(0)).toBeTruthy();
        });
        it('returns false if makes order of flow trailer', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            getCommercialAutoFlowQuestion.mockImplementationOnce(() => ({
                question: { choices: [{ value: 'trailer', order: 1 }] },
            }));
            expect(instance.isTrailerTab(0)).toBeFalsy();
        });
        it('handles no flow questions', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            getCommercialAutoFlowQuestion.mockImplementationOnce(() => undefined);
            expect(instance.isTrailerTab(0)).toBeFalsy();
            expect(instance.isTrailerTab(2)).toBeTruthy();
        });
    });
    describe('updateType', () => {
        it('does nothing if external', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const type = 'fake type';
            instance.useExternal = true;
            instance.populateVehicleOptions = jest.fn();
            instance.type = type;
            instance.updateType(2, true);
            expect(instance.type).toEqual(type);
            expect(instance.setVehicleSelection).not.toHaveBeenCalled();
            expect(instance.populateVehicleOptions).not.toHaveBeenCalled();
            expect(instance.loadYears).not.toHaveBeenCalled();
        });
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const type = VehicleTypes.trailers;
            instance.useExternal = false;
            instance.populateVehicleOptions = jest.fn();
            instance.type = '';
            instance.updateType(2, true);
            expect(instance.type).toEqual(type);
            expect(instance.setVehicleSelection).toHaveBeenCalled();
            expect(instance.populateVehicleOptions).toHaveBeenCalled();
            expect(instance.loadYears).toHaveBeenCalled();
        });
    });
    describe('saving trailers', () => {
        let instance;
        const dummyEvent = new CustomEvent('', {
            detail: {
                value: 'something',
            },
        });
        beforeEach(() => {
            instance = new BpSdkVehiclePanelContainer();
            instance.isVehicleTrailer = true;
        });
        describe('when the year, make, and model are defined', () => {
            beforeEach(() => {
                instance._currentYear = 2020;
                instance._currentMakeId = 'make';
                instance._currentModelId = 'model';
                // The actual event content doesn't matter; this function updates canSave
                instance.onOptionsUpdate(dummyEvent);
            });
            it('should be able to save', () => {
                expect(instance.canSave).toEqual(true);
            });
        });
        describe('when a configuration id is defined', () => {
            beforeEach(() => {
                instance._currentConfigId = '1234';
                // The actual event content doesn't matter; this function updates canSave
                instance.onOptionsUpdate(dummyEvent);
            });
            it('should be able to save', () => {
                expect(instance.canSave).toEqual(true);
            });
        });
        describe('when the year, make, or model are not defined', () => {
            beforeEach(() => {
                // The actual event content doesn't matter; this function updates canSave
                instance.onOptionsUpdate(dummyEvent);
            });
            it('should not be able to save', () => {
                expect(instance.canSave).toEqual(false);
            });
        });
    });
    describe('onOptionsUpdate', () => {
        let instance;
        const dummyEvent = new CustomEvent('', {
            detail: {
                value: 'something',
            },
        });
        describe('saving a vehicle', () => {
            beforeEach(() => {
                instance = new BpSdkVehiclePanelContainer();
                instance.isVehicleTrailer = false;
            });
            describe('when a configuration id is defined', () => {
                beforeEach(() => {
                    instance._currentConfigId = '1234';
                    // The actual event content doesn't matter; this function updates canSave
                    instance.onOptionsUpdate(dummyEvent);
                });
                it('should be able to save', () => {
                    expect(instance.canSave).toEqual(true);
                });
            });
            describe('when a configuration id is not defined', () => {
                beforeEach(() => {
                    instance._currentYear = 2020;
                    instance._currentMakeId = 'make';
                    instance._currentModelId = 'model';
                    // The actual event content doesn't matter; this function updates canSave
                    instance.onOptionsUpdate(dummyEvent);
                });
                it('should not yet be able to save', () => {
                    expect(instance.canSave).toEqual(false);
                });
            });
        });
        describe('saving a trailer', () => {
            beforeEach(() => {
                instance = new BpSdkVehiclePanelContainer();
                instance.isVehicleTrailer = true;
            });
            describe('when a user selected make and model from pre-populated options and the configuration id is defined', () => {
                beforeEach(() => {
                    instance._currentConfigId = '1234';
                    instance._currentYear = 2020;
                    instance._currentMakeId = '12345';
                    instance._currentModelId = '12345';
                    // The actual event content doesn't matter; this function updates canSave
                    instance.onOptionsUpdate(dummyEvent);
                });
                it('should be able to save', () => {
                    expect(instance.canSave).toEqual(true);
                });
            });
            describe('when a user selected make and model from pre-populated options but the configuration id is not yet defined', () => {
                beforeEach(() => {
                    instance._currentConfigId = undefined;
                    instance._currentYear = 2020;
                    instance._currentMakeId = '12345';
                    instance._currentModelId = '12345';
                    // The actual event content doesn't matter; this function updates canSave
                    instance.onOptionsUpdate(dummyEvent);
                });
                it('should not yet be able to save', () => {
                    expect(instance.canSave).toEqual(false);
                });
            });
            describe('when a user free-filled make and model and therefore a configuration id is always undefined', () => {
                beforeEach(() => {
                    instance._currentConfigId = undefined;
                    instance._currentYear = 2020;
                    instance._currentMakeId = 'User Typed Make';
                    instance._currentModelId = 'User Typed Model';
                    // The actual event content doesn't matter; this function updates canSave
                    instance.onOptionsUpdate(dummyEvent);
                });
                it('should be able to save', () => {
                    expect(instance.canSave).toEqual(true);
                });
            });
        });
    });
    describe('vehicle label', () => {
        let instance;
        const dummyEvent = new CustomEvent('', {
            detail: {
                value: 'something',
            },
        });
        beforeEach(() => {
            instance = new BpSdkVehiclePanelContainer();
        });
        describe('when the question group parent has a value', () => {
            const parentValue = 'value';
            beforeEach(() => {
                instance.questionGroup = {
                    parentQuestionAnswer: {
                        value: parentValue,
                    },
                };
                // The actual event content doesn't matter; this function updates canSave
                instance.onOptionsUpdate(dummyEvent);
            });
            it('should map it to the vehicle label', () => {
                expect(instance.vehicleLabel).toEqual(parentValue);
            });
        });
        describe('when the question group parent has no value', () => {
            const parentValue = 'value';
            beforeEach(() => {
                instance.questionGroup = {
                    parentQuestionAnswer: {},
                };
                // The actual event content doesn't matter; this function updates canSave
                instance.onOptionsUpdate(dummyEvent);
            });
            it('should map a blank vehicle label', () => {
                expect(instance.vehicleLabel).toEqual('');
            });
        });
    });
});
