import { newSpecPage } from "@stencil/core/testing";
import { BpSdkAccumulationQuestionGroup } from "./bp-sdk-accumulation-question-group";
import { MockQuestion, MockAnswer } from "../../mocks/answer";
describe('bp-sdk-accumulation-question-group', () => {
    describe('renders', () => {
        it('vanilla', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeTruthy();
        });
        it('indent', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.answers = [
                {
                    question: {
                        reference_type: 'indent',
                    },
                },
            ];
            await page.waitForChanges();
            const element = page.root.querySelector('bp-sdk-dynamic-input-redux-container');
            expect(element.className).toEqual('bp-sdk-indent');
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__value')).toBeFalsy();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__warning')).toBeFalsy();
        });
        it('accumulator', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.answers = [
                {
                    value: '99',
                    question: {
                        reference_type: 'accumulator',
                        min_value: 100,
                        max_value: 100,
                    },
                },
            ];
            await page.waitForChanges();
            expect(instance.validateAccumulatorValue()).toBeTruthy();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__value')).toBeTruthy();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__warning')).toBeTruthy();
        });
        it('renders errors on accumulator if there are', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.answers = [
                {
                    value: '99',
                    question: {
                        reference_type: 'accumulator',
                        min_value: 100,
                        max_value: 100,
                    },
                },
            ];
            await page.waitForChanges();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__error')).toBeNull();
            instance.onAccumulatorErrorsChange({ detail: ['min'] });
            await page.waitForChanges();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__warning')).toBeNull();
            expect(page.root.querySelector('.bp-sdk-accumulation-question-set__error')).toBeTruthy();
        });
        it(`doesn't render a label if answer is accumulator`, async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            const mockAnswer = new MockAnswer();
            const question = new MockQuestion();
            instance.answers = [
                Object.assign(Object.assign({}, mockAnswer), { question }),
                Object.assign(Object.assign({}, mockAnswer), { question: Object.assign(Object.assign({}, question), { reference_type: 'accumulator' }) }),
                Object.assign(Object.assign({}, mockAnswer), { question: Object.assign(Object.assign({}, question), { field_type: 'checkbox' }) }),
            ];
            expect(instance.shouldHideLabel(instance.answers[0])).toBeFalsy();
            expect(instance.shouldHideLabel(instance.answers[1])).toBeTruthy();
            expect(instance.shouldHideLabel(instance.answers[2])).toBeTruthy();
        });
        it('renders external component if defined', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeFalsy();
            whenDefinedMock.mockRestore();
        });
    });
    describe('shouldHideLabel', () => {
        it('returns expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            expect(instance.shouldHideLabel(null)).toBeFalsy();
            expect(instance.shouldHideLabel({})).toBeFalsy();
            expect(instance.shouldHideLabel({ question: {} })).toBeFalsy();
            expect(instance.shouldHideLabel({ question: { field_type: 'not checkbox' } })).toBeFalsy();
            expect(instance.shouldHideLabel({ question: { field_type: 'checkbox' } })).toBeTruthy();
        });
    });
    describe('getAriaLabel', () => {
        it('returns expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            expect(instance.getAriaLabel(null)).toBeUndefined();
            expect(instance.getAriaLabel({})).toBeUndefined();
            expect(instance.getAriaLabel({ question: { text: 'some text' } })).toBeUndefined();
            expect(instance.getAriaLabel({ question: { field_type: 'not checkbox', text: 'some text' } })).toBeUndefined();
            expect(instance.getAriaLabel({ question: { field_type: 'checkbox', text: 'some text' } })).toEqual('some text');
        });
    });
    describe('shouldHideLabel', () => {
        it('returns expected values', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            expect(instance.shouldHideLabel(null)).toBeFalsy();
            expect(instance.shouldHideLabel({})).toBeFalsy();
            expect(instance.shouldHideLabel({ question: {} })).toBeFalsy();
            expect(instance.shouldHideLabel({ question: { field_type: 'not checkbox' } })).toBeFalsy();
            expect(instance.shouldHideLabel({ question: { field_type: 'checkbox' } })).toBeTruthy();
        });
    });
    describe('onAnswersChange', () => {
        it('null', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.accumulator = undefined;
            instance.onAnswersChange(null);
            expect(instance.accumulator).toBeUndefined();
        });
        it('empty array', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.accumulator = undefined;
            instance.onAnswersChange([]);
            expect(instance.accumulator).toBeUndefined();
        });
        it('no number field_type', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.accumulator = undefined;
            instance.onAnswersChange([null, {}, { question: {} }, { question: { field_type: 'not number' } }]);
            expect(instance.accumulator).toBeUndefined();
        });
        it('answer with field_type number', async () => {
            const page = await newSpecPage({
                html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
                components: [BpSdkAccumulationQuestionGroup],
            });
            const instance = page.rootInstance;
            instance.accumulator = undefined;
            const answers = [
                {
                    value: '99',
                    question: {
                        reference_type: 'accumulator',
                        min_value: 100,
                        max_value: 100,
                    },
                },
            ];
            instance.onAnswersChange(answers);
            expect(instance.accumulator).toEqual(answers[0]);
        });
    });
});
describe('onAccumulatorChange', () => {
    it('null', async () => {
        const page = await newSpecPage({
            html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
            components: [BpSdkAccumulationQuestionGroup],
        });
        const instance = page.rootInstance;
        instance.buffer = 100;
        instance.onAccumulatorChange(null);
        expect(instance.buffer).toBeUndefined();
    });
    it('empty object', async () => {
        const page = await newSpecPage({
            html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
            components: [BpSdkAccumulationQuestionGroup],
        });
        const instance = page.rootInstance;
        instance.buffer = 100;
        instance.onAccumulatorChange({});
        expect(instance.buffer).toBeUndefined();
    });
    it('object with value property', async () => {
        const page = await newSpecPage({
            html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
            components: [BpSdkAccumulationQuestionGroup],
        });
        const instance = page.rootInstance;
        instance.buffer = 100;
        instance.onAccumulatorChange({ value: '100' });
        expect(instance.buffer).toBe(100);
    });
    it('drops decimals', async () => {
        const page = await newSpecPage({
            html: '<bp-sdk-accumulation-question-group></bp-sdk-accumulation-question-group>',
            components: [BpSdkAccumulationQuestionGroup],
        });
        const instance = page.rootInstance;
        instance.buffer = 100;
        instance.onAccumulatorChange({ value: '99.9' });
        expect(instance.buffer).toBe(99);
    });
});
