import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { type RouteLocation } from 'vue-router'; import { type MountingOptions, shallowMount } from '@vue/test-utils'; import AlertService from '@/shared/alert/alert.service'; import ChoiceDetails from './choice-details.vue'; type ChoiceDetailsComponentType = InstanceType; let route: Partial; const routerGoMock = vi.fn(); vi.mock('vue-router', () => ({ useRoute: () => route, useRouter: () => ({ go: routerGoMock }), })); const choiceSample = { id: 123 }; describe('Component Tests', () => { let alertService: AlertService; afterEach(() => { vi.resetAllMocks(); }); describe('Choice Management Detail Component', () => { let choiceServiceStub: any; let mountOptions: MountingOptions['global']; beforeEach(() => { route = {}; choiceServiceStub = { find: vi.fn(), }; alertService = new AlertService({ i18n: { t: vi.fn() } as any, toast: { create: vi.fn(), } as any, }); mountOptions = { stubs: { 'font-awesome-icon': true, 'router-link': true, }, provide: { alertService, choiceService: () => choiceServiceStub, }, }; }); describe('Navigate to details', () => { it('Should call load all on init', async () => { // GIVEN choiceServiceStub.find.mockResolvedValue(choiceSample); route = { params: { choiceId: `${123}`, }, }; const wrapper = shallowMount(ChoiceDetails, { global: mountOptions }); const comp = wrapper.vm; // WHEN await comp.$nextTick(); // THEN expect(comp.choice).toMatchObject(choiceSample); }); }); describe('Previous state', () => { it('Should go previous state', async () => { choiceServiceStub.find.mockResolvedValue(choiceSample); const wrapper = shallowMount(ChoiceDetails, { global: mountOptions }); const comp = wrapper.vm; await comp.$nextTick(); comp.previousState(); await comp.$nextTick(); expect(routerGoMock).toHaveBeenCalledWith(-1); }); }); }); });