import { AddressValidationVerdict, ValidationStatus } from "@boldpenguin/sdk-types";
import { newSpecPage } from "@stencil/core/testing";
import { mockAnswer } from "../../mocks/answer";
import { BpSdkAccordion } from "./bp-sdk-accordion";
const fakeStore = {
    config: {
        options: {
            hideNestedQuestionGroupLabels: false,
        },
    },
    viewState: {
        addressValidation: {
            shouldOverrideValidationTrigger: false,
            validations: {},
        },
    },
};
const mockGroup = () => ({
    answers: [mockAnswer],
    groupId: '123',
    group_type: 'accordion',
    label: '',
    nestedGroupsWithAnswers: new Map(),
    order: 0,
    parentQuestionAnswer: Object.assign(Object.assign({}, mockAnswer), { value: 'Parent', question: Object.assign(Object.assign({}, mockAnswer.question), { text: 'Parent Text' }) }),
    poolId: '123',
});
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-accordion></bp-sdk-accordion>',
        components: [BpSdkAccordion],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-accordion', () => {
    it('renders', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = mockGroup();
        await page.waitForChanges();
        const element = page.root.querySelector('div.bp-sdk-accordion__container');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-accordion')).toBeFalsy();
    });
    it('renders answers', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = mockGroup();
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-dynamic-input-redux-container');
        expect(element).toBeTruthy();
    });
    it('renders external component if defined', async () => {
        const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = mockGroup();
        await page.waitForChanges();
        expect(page.root.querySelector('bp-sdk-external-accordion')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    it('renders nested question group if defined', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = Object.assign(Object.assign({}, mockGroup()), { nestedGroupsWithAnswers: { get: () => true, answers: [mockAnswer], group_type: 'not-recognized' } });
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-question-group-container');
        expect(element).toBeTruthy();
    });
    describe('renders label conditionally', () => {
        it('mapStateToProps', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.questionGroup = mockGroup();
            const result = instance.mapStateToProps(fakeStore);
            expect(result.configOptions.hideNestedQuestionGroupLabels).toBeFalsy();
            expect(result.addressValidationStatus).toBeFalsy();
        });
        it('mapStateToProps - should get status when address validation is present', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.questionGroup = mockGroup();
            const result = instance.mapStateToProps(Object.assign(Object.assign({}, fakeStore), { viewState: Object.assign(Object.assign({}, fakeStore.viewState), { addressValidation: {
                        shouldOverrideValidationTrigger: false,
                        validations: {
                            123: {
                                verdict: AddressValidationVerdict.CONFIRM,
                                responseId: '123456',
                                address: {},
                                validationStatus: ValidationStatus.REQUIRED,
                                stagedAnswers: [],
                                attempt: 1,
                                originalAddress: '100 Main St. Columbus, OH 43210',
                            },
                        },
                    } }) }));
            expect(result.addressValidationStatus).toEqual(ValidationStatus.REQUIRED);
        });
        it('renders label if hideNestedQuestionGroupLabels is false', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.configOptions = { hideNestedQuestionGroupLabels: false };
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-question-group-label');
            expect(element).toBeTruthy();
        });
        it('does not render label if hideNestedQuestionGroupLabels is true', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.configOptions = { hideNestedQuestionGroupLabels: true };
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-question-group-label');
            expect(element).toBeNull();
        });
    });
});
