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 QuestionDetails from './question-details.vue'; type QuestionDetailsComponentType = InstanceType; let route: Partial; const routerGoMock = vi.fn(); vi.mock('vue-router', () => ({ useRoute: () => route, useRouter: () => ({ go: routerGoMock }), })); const questionSample = { id: 123 }; describe('Component Tests', () => { let alertService: AlertService; afterEach(() => { vi.resetAllMocks(); }); describe('Question Management Detail Component', () => { let questionServiceStub: any; let mountOptions: MountingOptions['global']; beforeEach(() => { route = {}; questionServiceStub = { 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, questionService: () => questionServiceStub, }, }; }); describe('Navigate to details', () => { it('Should call load all on init', async () => { // GIVEN questionServiceStub.find.mockResolvedValue(questionSample); route = { params: { questionId: `${123}`, }, }; const wrapper = shallowMount(QuestionDetails, { global: mountOptions }); const comp = wrapper.vm; // WHEN await comp.$nextTick(); // THEN expect(comp.question).toMatchObject(questionSample); }); }); describe('Previous state', () => { it('Should go previous state', async () => { questionServiceStub.find.mockResolvedValue(questionSample); const wrapper = shallowMount(QuestionDetails, { global: mountOptions }); const comp = wrapper.vm; await comp.$nextTick(); comp.previousState(); await comp.$nextTick(); expect(routerGoMock).toHaveBeenCalledWith(-1); }); }); }); });