import { newSpecPage } from "@stencil/core/testing";
import { BpSdkDriverGroup } from "./bp-sdk-driver-group";
const defaultAnswerGroupLabel = 'Driver';
describe('bp-sdk-driver-group', () => {
    let page;
    let instance;
    describe('renders', () => {
        it('vanilla', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            instance = page.rootInstance;
            const group = createMappedGroupAnswers([{ question: { code: '' } }]);
            instance.group = group;
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-icon-container');
            expect(element.getAttribute('name')).toBe('person');
        });
        it('errors', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            instance = page.rootInstance;
            const group = createMappedGroupAnswers([{ question: { id: '1', code: '' } }]);
            instance.group = group;
            instance.errors = {
                '1': ['errors'],
            };
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-icon-container');
            expect(element.getAttribute('name')).toBe('error-outline');
        });
        it('delete button', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            instance = page.rootInstance;
            const group = createMappedGroupAnswers([{ question: { id: '1', code: '' } }]);
            instance.group = group;
            instance.canDelete = true;
            await page.waitForChanges();
            const element = page.root.querySelector('.bp-sdk-driver-group__delete-button');
            expect(element.nodeName).toBe('BUTTON');
        });
        it('fallback', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            instance = page.rootInstance;
            instance.group = {};
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeTruthy();
        });
        it('no group', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeTruthy();
        });
        it('renders external component if defined', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            page = await createSpecPage();
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeFalsy();
            whenDefinedMock.mockRestore();
        });
    });
    describe('onDeleteButtonClick', () => {
        it('no group', async () => {
            page = await createSpecPage();
            await page.waitForChanges();
            instance = page.rootInstance;
            jest.spyOn(instance.deleteItem, 'emit').mockImplementation((data) => new CustomEvent('custom'));
            instance.onDeleteButtonClick();
            expect(instance.deleteItem.emit).toBeCalledTimes(1);
        });
    });
    describe('toggling', () => {
        describe('when an open expansion panel is toggled', () => {
            beforeEach(async () => {
                page = await createSpecPage();
                await page.waitForChanges();
                instance = page.rootInstance;
                instance.isExpanded = true;
                instance.onToggleExpansion();
            });
            it('should close it', () => {
                expect(instance.isExpanded).toEqual(false);
            });
        });
        describe('when a closed expansion panel is toggled', () => {
            beforeEach(async () => {
                page = await createSpecPage();
                await page.waitForChanges();
                instance = page.rootInstance;
                instance.isExpanded = false;
                instance.onToggleExpansion();
            });
            it('should open it', () => {
                expect(instance.isExpanded).toEqual(true);
            });
        });
    });
    describe('updateDriverName', () => {
        describe(`should prefer the parent answer value`, () => {
            beforeEach(async () => {
                page = await createSpecPage();
                await page.waitForChanges();
                instance = page.rootInstance;
                const group = createMappedGroupAnswers([
                    {
                        value: 'first',
                        question: {
                            code: 'mqs_driver_first_name',
                        },
                    },
                    {
                        value: 'last',
                        question: {
                            code: 'mqs_driver_last_name',
                        },
                    },
                ], 'Parent name');
                instance.updateDriverName(group);
            });
            it('should display the parent question value', () => {
                expect(instance.driverName).toEqual('Parent name');
            });
        });
        describe('when a driver name has not been provided', () => {
            beforeEach(async () => {
                page = await createSpecPage();
                await page.waitForChanges();
                instance = page.rootInstance;
                const group = createMappedGroupAnswers([
                    {
                        question: {
                            code: 'mqs_driver_first_name',
                        },
                        value: undefined,
                    },
                    {
                        question: {
                            code: 'mqs_driver_last_name',
                        },
                        value: undefined,
                    },
                ]);
                instance.updateDriverName(group);
            });
            it('should display the group label', () => {
                expect(instance.driverName).toEqual(defaultAnswerGroupLabel);
            });
        });
        describe('when the group label and driver name are blank', () => {
            beforeEach(async () => {
                page = await createSpecPage();
                await page.waitForChanges();
                instance = page.rootInstance;
                const group = createMappedGroupAnswers([
                    {
                        question: {
                            code: 'mqs_driver_first_name',
                        },
                        value: undefined,
                    },
                    {
                        question: {
                            code: 'mqs_driver_last_name',
                        },
                        value: undefined,
                    },
                ]);
                group.label = '';
                instance.updateDriverName(group);
            });
            it('should display a default label', () => {
                expect(instance.driverName).toEqual('Driver');
            });
        });
    });
});
async function createSpecPage() {
    return await newSpecPage({
        html: '<bp-sdk-driver-group></bp-sdk-driver-group>',
        components: [BpSdkDriverGroup],
    });
}
function createMappedGroupAnswers(answers, parentValue = null) {
    return {
        answers,
        group_type: '',
        label: defaultAnswerGroupLabel,
        order: 5,
        parentQuestionAnswer: {
            value: parentValue,
        },
    };
}
