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 ExamAnswerUpdate from './exam-answer-update.vue'; type ExamAnswerUpdateComponentType = InstanceType; let route: Partial; const routerGoMock = vi.fn(); vi.mock('vue-router', () => ({ useRoute: () => route, useRouter: () => ({ go: routerGoMock }), })); const examAnswerSample = { id: 123 }; describe('Component Tests', () => { let mountOptions: MountingOptions['global']; let alertService: AlertService; describe('ExamAnswer Management Update Component', () => { let comp: ExamAnswerUpdateComponentType; let examAnswerServiceStub: any; beforeEach(() => { route = {}; examAnswerServiceStub = { retrieve: vi.fn(), find: vi.fn(), update: vi.fn(), create: vi.fn(), }; examAnswerServiceStub.retrieve.mockResolvedValueOnce([]); alertService = new AlertService({ i18n: { t: vi.fn() } as any, toast: { create: vi.fn(), } as any, }); mountOptions = { stubs: { 'font-awesome-icon': true, 'b-input-group': true, 'b-input-group-prepend': true, 'b-form-datepicker': true, 'b-form-input': true, }, provide: { alertService, examAnswerService: () => examAnswerServiceStub, questionService: () => ({ retrieve: vi.fn().mockResolvedValue({}), }), examAttemptService: () => ({ retrieve: vi.fn().mockResolvedValue({}), }), }, }; }); afterEach(() => { vi.resetAllMocks(); }); describe('save', () => { it('Should call update service on save for existing entity', async () => { // GIVEN const wrapper = shallowMount(ExamAnswerUpdate, { global: mountOptions }); comp = wrapper.vm; comp.examAnswer = examAnswerSample; examAnswerServiceStub.update.mockResolvedValue(examAnswerSample); // WHEN comp.save(); await comp.$nextTick(); // THEN expect(examAnswerServiceStub.update).toHaveBeenCalledWith(examAnswerSample); expect(comp.isSaving).toEqual(false); }); it('Should call create service on save for new entity', async () => { // GIVEN const entity = {}; examAnswerServiceStub.create.mockResolvedValue(entity); const wrapper = shallowMount(ExamAnswerUpdate, { global: mountOptions }); comp = wrapper.vm; comp.examAnswer = entity; // WHEN comp.save(); await comp.$nextTick(); // THEN expect(examAnswerServiceStub.create).toHaveBeenCalledWith(entity); expect(comp.isSaving).toEqual(false); }); }); describe('Before route enter', () => { it('Should retrieve data', async () => { // GIVEN examAnswerServiceStub.find.mockResolvedValue(examAnswerSample); examAnswerServiceStub.retrieve.mockResolvedValue([examAnswerSample]); // WHEN route = { params: { examAnswerId: `${examAnswerSample.id}`, }, }; const wrapper = shallowMount(ExamAnswerUpdate, { global: mountOptions }); comp = wrapper.vm; await comp.$nextTick(); // THEN expect(comp.examAnswer).toMatchObject(examAnswerSample); }); }); describe('Previous state', () => { it('Should go previous state', async () => { examAnswerServiceStub.find.mockResolvedValue(examAnswerSample); const wrapper = shallowMount(ExamAnswerUpdate, { global: mountOptions }); comp = wrapper.vm; await comp.$nextTick(); comp.previousState(); await comp.$nextTick(); expect(routerGoMock).toHaveBeenCalledWith(-1); }); }); }); });