import { vitest } from 'vitest'; import { type RouteLocation } from 'vue-router'; import { type MountingOptions, shallowMount } from '@vue/test-utils'; import dayjs from 'dayjs'; import sinon, { type SinonStubbedInstance } from 'sinon'; import StrutturaService from '@/entities/struttura/struttura.service'; import AlertService from '@/shared/alert/alert.service'; import { DATE_TIME_LONG_FORMAT } from '@/shared/composables/date-format'; import ModelloLiberatoriaUpdate from './modello-liberatoria-update.vue'; import ModelloLiberatoriaService from './modello-liberatoria.service'; type ModelloLiberatoriaUpdateComponentType = InstanceType; let route: Partial; const routerGoMock = vitest.fn(); vitest.mock('vue-router', () => ({ useRoute: () => route, useRouter: () => ({ go: routerGoMock }), })); const modelloLiberatoriaSample = { id: 123 }; describe('Component Tests', () => { let mountOptions: MountingOptions['global']; let alertService: AlertService; describe('ModelloLiberatoria Management Update Component', () => { let comp: ModelloLiberatoriaUpdateComponentType; let modelloLiberatoriaServiceStub: SinonStubbedInstance; beforeEach(() => { route = {}; modelloLiberatoriaServiceStub = sinon.createStubInstance(ModelloLiberatoriaService); modelloLiberatoriaServiceStub.retrieve.onFirstCall().resolves(Promise.resolve([])); alertService = new AlertService({ i18n: { t: vitest.fn() } as any, toast: { show: vitest.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, modelloLiberatoriaService: () => modelloLiberatoriaServiceStub, strutturaService: () => sinon.createStubInstance(StrutturaService, { retrieve: sinon.stub().resolves({}), } as any), }, }; }); afterEach(() => { vitest.resetAllMocks(); }); describe('load', () => { beforeEach(() => { const wrapper = shallowMount(ModelloLiberatoriaUpdate, { global: mountOptions }); comp = wrapper.vm; }); it('Should convert date from string', () => { // GIVEN const date = new Date('2019-10-15T11:42:02Z'); // WHEN const convertedDate = comp.convertDateTimeFromServer(date); // THEN expect(convertedDate).toEqual(dayjs(date).format(DATE_TIME_LONG_FORMAT)); }); it('Should not convert date if date is not present', () => { expect(comp.convertDateTimeFromServer(null)).toBeNull(); }); }); describe('save', () => { it('Should call update service on save for existing entity', async () => { // GIVEN const wrapper = shallowMount(ModelloLiberatoriaUpdate, { global: mountOptions }); comp = wrapper.vm; comp.modelloLiberatoria = modelloLiberatoriaSample; modelloLiberatoriaServiceStub.update.resolves(modelloLiberatoriaSample); // WHEN comp.save(); await comp.$nextTick(); // THEN expect(modelloLiberatoriaServiceStub.update.calledWith(modelloLiberatoriaSample)).toBeTruthy(); expect(comp.isSaving).toEqual(false); }); it('Should call create service on save for new entity', async () => { // GIVEN const entity = {}; modelloLiberatoriaServiceStub.create.resolves(entity); const wrapper = shallowMount(ModelloLiberatoriaUpdate, { global: mountOptions }); comp = wrapper.vm; comp.modelloLiberatoria = entity; // WHEN comp.save(); await comp.$nextTick(); // THEN expect(modelloLiberatoriaServiceStub.create.calledWith(entity)).toBeTruthy(); expect(comp.isSaving).toEqual(false); }); }); describe('Before route enter', () => { it('Should retrieve data', async () => { // GIVEN modelloLiberatoriaServiceStub.find.resolves(modelloLiberatoriaSample); modelloLiberatoriaServiceStub.retrieve.resolves([modelloLiberatoriaSample]); // WHEN route = { params: { modelloLiberatoriaId: `${modelloLiberatoriaSample.id}`, }, }; const wrapper = shallowMount(ModelloLiberatoriaUpdate, { global: mountOptions }); comp = wrapper.vm; await comp.$nextTick(); // THEN expect(comp.modelloLiberatoria).toMatchObject(modelloLiberatoriaSample); }); }); describe('Previous state', () => { it('Should go previous state', async () => { modelloLiberatoriaServiceStub.find.resolves(modelloLiberatoriaSample); const wrapper = shallowMount(ModelloLiberatoriaUpdate, { global: mountOptions }); comp = wrapper.vm; await comp.$nextTick(); comp.previousState(); await comp.$nextTick(); expect(routerGoMock).toHaveBeenCalledWith(-1); }); }); }); });