/* eslint-env jest */
import { mount, flushPromises } from '@vue/test-utils';
import { createTestingPinia } from '@pinia/testing';
import navigationScenarios from '@/router/router-constants/navigation-scenarios';
import routerParams from '@/router/router-constants/router-params';
import { useMainStore } from '@/store';
import vehicleCategories from '@/constants/vehicle-categories';
import VehicleDamageComponent from '@/layouts/vehicle-damage/vehicle-damage.vue';

const mockRoute = {
    params: {}
};
const mockRouter = {
    navigate: jest.fn()
};
const mountOptions = {
    global: {
        mixins: [
            {
                computed: {
                    navigationScenarios() {
                        return navigationScenarios;
                    },
                    routerParams() {
                        return routerParams;
                    },
                    vehicleCategories() {
                        return vehicleCategories;
                    }
                },
                methods: {
                    getCmsContent: jest.fn(),
                    getFooterInfoBoxHeight: jest.fn(() => 80),
                    getPageNameByQueryString: jest.fn(() => '')
                }
            }
        ],
        mocks: {
            $route: mockRoute,
            $router: mockRouter
        },
        stubs: {
            siteHeader: true,
            siteSubHeader: true,
            siteDoorOptions: true,
            damageLocationQuestion: true,
            windshieldOptions: true,
            replaceOptionsQuestion: true,
            alert: true
        }
    }
};

beforeEach(() => {
    jest.clearAllMocks();
});
describe('vehicle-damage.vue', () => {
    test('When damage selected is a repair, navigate to coverage-statement page', async () => {
        mountOptions.global.plugins = [createTestingPinia({
            initialState: {
                main: {
                    order: {
                        damage: {
                            isRepair: true
                        }
                    }
                }
            }
        })];
        const wrapper = mount(VehicleDamageComponent, mountOptions);
        const siteFooterWrapper = wrapper.getComponent({ ref: 'siteFooter' });

        useMainStore().getSupportingItems = jest.fn().mockImplementation(() => Promise.resolve({
            data: { data: [
                {
                    description: null,
                    partNumber: 'SUPPLIES-REPAIR',
                    partType: 'REPAIR FEE'
                },
                {
                    description: null,
                    partNumber: 'WSREPAIR',
                    partType: 'REPAIR FEE'
                }
            ] }
        }));

        siteFooterWrapper.vm.$emit('forwardClicked');

        await flushPromises();
        expect(mockRouter.navigate).toHaveBeenCalledTimes(1);
        expect(mockRouter.navigate)
            .toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_WITH_REPAIR, mockRoute);
    });
    test('Error in getPartsOrQuestions call => bailout true and navigate forward with CLICKED_FORWARD_WITH_BAILOUT scenario', async () => {
        mountOptions.global.plugins = [createTestingPinia({
            initialState: {
                main: {
                    order: {
                        damage: {
                            isRepair: false
                        },
                        vehicle: {
                            vin: 'MOCK VIN'
                        }
                    }
                }
            }
        })];
        mountOptions.data = () => ({
            hasBailedOut: true
        });

        const wrapper = mount(VehicleDamageComponent, mountOptions);
        const siteFooterWrapper = wrapper.getComponent({ ref: 'siteFooter' });
        const partsQuestionsErrorResponse = {
            error: 'Error getting parts'
        };
        useMainStore().getPartsOrQuestions = jest.fn().mockImplementation(() => (
            partsQuestionsErrorResponse
        ));
        siteFooterWrapper.vm.$emit('forwardClicked');

        await flushPromises();
        expect(mockRouter.navigate).toHaveBeenCalledTimes(1);
        expect(mockRouter.navigate)
            .toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_WITH_BAILOUT, mockRoute);
    });
});
