/* eslint-disable max-len */
// Components
import questionsPageLayout from '@/iss-components/questions-page-layout/questions-page-layout.vue';

// Supporting Files
import { shallowMount } from '@vue/test-utils';
import { getMountOptions } from '@/helpers/unit-test-helper.js';
import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin';
import baseMixin from '@/mixins/base-mixin';

// Mock our module for promises.
jest.mock('@/helpers/layout-helper.js', () => ({
    settleAllPromises: jest.fn()
}));

// Mock fetchCmsContentForPage
jest.mock('@/helpers/cms-content-helper', () => ({
    fetchCmsContentForPage: jest.fn()
}));

function setupMocks() {
    const mountOptions = getMountOptions({
        mixins: [baseMixin, vehicleQuestionsMixin]
    });
    mountOptions.attachTo = document.body;
    const wrapper = shallowMount(questionsPageLayout, mountOptions);

    return { wrapper };
}

describe('questionsPageLayout.vue', () => {
    describe('method showThisQuestionChain...', () => {
        test('Should return true if index prop and passed index match', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({ index: 0 });

            const testGlassPiece = {
                questions: [
                    {
                        questionSequence: 1,
                        questionText:
                            'Is your vehicle equipped with the optional Lane-Keeping System which tugs on the steering wheel and/or beeps to alert you if you drift too close to the edge of the lane?',
                        answers: []
                    }
                ]
            };
            const testIndex = 0;

            // Act
            const result = wrapper.vm.showThisQuestionChain(testGlassPiece, testIndex);

            // Assert
            expect(result).toBe(true);

            wrapper.unmount();
        });

        test('Should return true if answerResult exists', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({ index: 0 });

            const testGlassPiece = {
                questions: [
                    {
                        questionSequence: 1,
                        questionText:
                            'Is your vehicle equipped with the optional Lane-Keeping System which tugs on the steering wheel and/or beeps to alert you if you drift too close to the edge of the lane?',
                        answers: []
                    }
                ],
                answerData: {
                    answerResult: 'test'
                }
            };
            const testIndex = 1;

            // Act
            const result = wrapper.vm.showThisQuestionChain(testGlassPiece, testIndex);

            // Assert
            expect(result).toBe(true);

            wrapper.unmount();
        });

        test("Should return false if indexes don't match and answerResult is missing", async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({ index: 0 });

            const testGlassPiece = {
                questions: [
                    {
                        questionSequence: 1,
                        questionText:
                            'Is your vehicle equipped with the optional Lane-Keeping System which tugs on the steering wheel and/or beeps to alert you if you drift too close to the edge of the lane?',
                        answers: []
                    }
                ]
            };
            const testIndex = 1;

            // Act
            const result = wrapper.vm.showThisQuestionChain(testGlassPiece, testIndex);

            // Assert
            expect(result).toBe(false);

            wrapper.unmount();
        });

        test('Should return false if no questions', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({ index: 0 });

            const testGlassPiece = {};
            const testIndex = 0;

            // Act
            const result = wrapper.vm.showThisQuestionChain(testGlassPiece, testIndex);

            // Assert
            expect(result).toBe(false);

            wrapper.unmount();
        });

        test('Should return false if suppressed', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({ index: 0 });

            const testGlassPiece = {
                isSuppressedPart: true,
                questions: [
                    {
                        questionSequence: 1,
                        questionText:
                            'Is your vehicle equipped with the optional Lane-Keeping System which tugs on the steering wheel and/or beeps to alert you if you drift too close to the edge of the lane?',
                        answers: []
                    }
                ],
                answerData: {
                    answerResult: 'test'
                }
            };
            const testIndex = 0;

            // Act
            const result = wrapper.vm.showThisQuestionChain(testGlassPiece, testIndex);

            // Assert
            expect(result).toBe(false);

            wrapper.unmount();
        });
    });

    describe('method handleForwardButtonAction...', () => {
        test('Should emit forwardButtonAction', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({});

            // Act
            wrapper.vm.handleForwardButtonAction();

            // Assert
            expect(wrapper.emitted()).toHaveProperty('forwardButtonAction');

            wrapper.unmount();
        });
    });

    describe('method handleBackButtonAction...', () => {
        test('Should emit backButtonAction', async () => {
            // Arrange
            const { wrapper } = setupMocks({});
            await wrapper.setProps({});

            // Act
            wrapper.vm.handleBackButtonAction();

            // Assert
            expect(wrapper.emitted()).toHaveProperty('back-click');

            wrapper.unmount();
        });
    });
});
