import { newSpecPage } from "@stencil/core/testing";
import { BpSdkVehicleInterface } from "./bp-sdk-vehicle-interface";
const mockQuestionGroup = {
    answers: [
        {
            id: 'year',
            question: {
                code: 'mqs_vehicle_year',
            },
        },
        {
            id: 'make',
            question: {
                code: 'mqs_vehicle_make',
            },
        },
        {
            id: 'model',
            question: {
                code: 'mqs_vehicle_model',
            },
        },
        {
            id: 'configuration',
            question: {
                code: 'mqs_vehicle_submodel',
            },
        },
    ],
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-vehicle-interface></bp-sdk-vehicle-interface>',
        components: [BpSdkVehicleInterface],
    });
};
const setupInstance = (page) => {
    return page.rootInstance;
};
describe('bp-sdk-vehicle-interface', () => {
    it('renders', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = {
            id: 'some id',
        };
        await page.waitForChanges();
        const element = page.root.querySelector('.bp-sdk-vehicle-interface__container');
        expect(element).toBeTruthy();
    });
    it('renders if no question group', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = undefined;
        await page.waitForChanges();
        const element = page.root.querySelector('.bp-sdk-vehicle-interface__container');
        expect(element).toBeTruthy();
    });
    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-interface__container');
        expect(element).toBeFalsy();
        whenDefinedMock.mockRestore();
    });
    describe('answer id map', () => {
        describe('when the component loads', () => {
            let instance;
            beforeEach(async () => {
                const page = await setupPage();
                instance = setupInstance(page);
                instance.answerIdMap = {
                    year: 'year',
                    make: 'make',
                    model: 'model',
                    configuration: 'configuration',
                };
                instance.componentWillLoad();
            });
            it('should use default values', () => {
                expect(instance.answerIdMap.year).toEqual('');
                expect(instance.answerIdMap.make).toEqual('');
                expect(instance.answerIdMap.model).toEqual('');
                expect(instance.answerIdMap.configuration).toEqual('');
            });
        });
        describe('when the component is reset', () => {
            let instance;
            beforeEach(() => {
                instance = new BpSdkVehicleInterface();
                instance.answerIdMap = {
                    year: 'year',
                    make: 'make',
                    model: 'model',
                    configuration: 'configuration',
                };
                instance.resetState();
            });
            it('should use default values', () => {
                expect(instance.answerIdMap.year).toEqual('');
                expect(instance.answerIdMap.make).toEqual('');
                expect(instance.answerIdMap.model).toEqual('');
                expect(instance.answerIdMap.configuration).toEqual('');
            });
        });
        describe('when the question group changes', () => {
            let instance;
            beforeEach(() => {
                instance = new BpSdkVehicleInterface();
                instance.questionGroup = mockQuestionGroup;
                instance.onQuestionGroupChange();
            });
            it('should update the answer ID map', () => {
                expect(instance.answerIdMap.year).toEqual('year');
                expect(instance.answerIdMap.make).toEqual('make');
                expect(instance.answerIdMap.model).toEqual('model');
                expect(instance.answerIdMap.configuration).toEqual('configuration');
            });
        });
    });
    describe('answer id map', () => {
        describe('when the component loads', () => {
            let instance;
            beforeEach(() => {
                instance = new BpSdkVehicleInterface();
                instance.answerIdMap = {
                    year: 'year',
                    make: 'make',
                    model: 'model',
                    configuration: 'configuration',
                };
                instance.componentWillLoad();
            });
            it('should use default values', () => {
                expect(instance.answerIdMap.year).toEqual('');
                expect(instance.answerIdMap.make).toEqual('');
                expect(instance.answerIdMap.model).toEqual('');
                expect(instance.answerIdMap.configuration).toEqual('');
            });
        });
        describe('when the component is reset', () => {
            let instance;
            beforeEach(() => {
                instance = new BpSdkVehicleInterface();
                instance.answerIdMap = {
                    year: 'year',
                    make: 'make',
                    model: 'model',
                    configuration: 'configuration',
                };
                instance.resetState();
            });
            it('should use default values', () => {
                expect(instance.answerIdMap.year).toEqual('');
                expect(instance.answerIdMap.make).toEqual('');
                expect(instance.answerIdMap.model).toEqual('');
                expect(instance.answerIdMap.configuration).toEqual('');
            });
        });
        describe('when the question group changes', () => {
            let instance;
            beforeEach(() => {
                instance = new BpSdkVehicleInterface();
                instance.questionGroup = mockQuestionGroup;
                instance.onQuestionGroupChange();
            });
            it('should update the answer ID map', () => {
                expect(instance.answerIdMap.year).toEqual('year');
                expect(instance.answerIdMap.make).toEqual('make');
                expect(instance.answerIdMap.model).toEqual('model');
                expect(instance.answerIdMap.configuration).toEqual('configuration');
            });
        });
    });
});
