import { beforeEach, describe, expect, it, vi } from 'vitest'; import { type MountingOptions, shallowMount } from '@vue/test-utils'; import AlertService from '@/shared/alert/alert.service'; import Choice from './choice.vue'; type ChoiceComponentType = InstanceType; const bModalStub = { render: () => {}, methods: { hide: () => {}, show: () => {}, }, }; describe('Component Tests', () => { let alertService: AlertService; describe('Choice Management Component', () => { let choiceServiceStub: any; let mountOptions: MountingOptions['global']; beforeEach(() => { choiceServiceStub = { retrieve: vi.fn(), delete: vi.fn(), }; choiceServiceStub.retrieve.mockResolvedValue({ headers: {} }); alertService = new AlertService({ i18n: { t: vi.fn() } as any, toast: { create: vi.fn(), } as any, }); mountOptions = { stubs: { bModal: bModalStub as any, 'font-awesome-icon': true, 'b-badge': true, 'b-button': true, 'router-link': true, }, directives: { 'b-modal': {}, }, provide: { alertService, choiceService: () => choiceServiceStub, }, }; }); describe('Mount', () => { it('Should call load all on init', async () => { // GIVEN choiceServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [{ id: 123 }] }); // WHEN const wrapper = shallowMount(Choice, { global: mountOptions }); const comp = wrapper.vm; await comp.$nextTick(); // THEN expect(choiceServiceStub.retrieve).toHaveBeenCalledOnce(); expect(comp.choices[0]).toEqual(expect.objectContaining({ id: 123 })); }); }); describe('Handles', () => { let comp: ChoiceComponentType; beforeEach(async () => { const wrapper = shallowMount(Choice, { global: mountOptions }); comp = wrapper.vm; await comp.$nextTick(); choiceServiceStub.retrieve.mockReset(); choiceServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [] }); }); it('Should call delete service on confirmDelete', async () => { // GIVEN choiceServiceStub.delete.mockResolvedValue({}); // WHEN comp.prepareRemove({ id: 123 }); comp.removeChoice(); await comp.$nextTick(); // clear components // THEN expect(choiceServiceStub.delete).toHaveBeenCalled(); // THEN await comp.$nextTick(); // handle component clear watch expect(choiceServiceStub.retrieve).toHaveBeenCalledTimes(1); }); }); }); });