import { vitest } from 'vitest';

import { type MountingOptions, shallowMount } from '@vue/test-utils';
import sinon, { type SinonStubbedInstance } from 'sinon';

import AlertService from '@/shared/alert/alert.service';

import ModelloLiberatoriaService from './modello-liberatoria.service';
import ModelloLiberatoria from './modello-liberatoria.vue';

type ModelloLiberatoriaComponentType = InstanceType<typeof ModelloLiberatoria>;

const bModalStub = {
  render: () => {},
  methods: {
    hide: () => {},
    show: () => {},
  },
};

describe('Component Tests', () => {
  let alertService: AlertService;

  describe('ModelloLiberatoria Management Component', () => {
    let modelloLiberatoriaServiceStub: SinonStubbedInstance<ModelloLiberatoriaService>;
    let mountOptions: MountingOptions<ModelloLiberatoriaComponentType>['global'];

    beforeEach(() => {
      modelloLiberatoriaServiceStub = sinon.createStubInstance<ModelloLiberatoriaService>(ModelloLiberatoriaService);
      modelloLiberatoriaServiceStub.retrieve.resolves({ headers: {} });

      alertService = new AlertService({
        i18n: { t: vitest.fn() } as any,
        toast: {
          show: vitest.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,
          modelloLiberatoriaService: () => modelloLiberatoriaServiceStub,
        },
      };
    });

    describe('Mount', () => {
      it('Should call load all on init', async () => {
        // GIVEN
        modelloLiberatoriaServiceStub.retrieve.resolves({ headers: {}, data: [{ id: 123 }] });

        // WHEN
        const wrapper = shallowMount(ModelloLiberatoria, { global: mountOptions });
        const comp = wrapper.vm;
        await comp.$nextTick();

        // THEN
        expect(modelloLiberatoriaServiceStub.retrieve.calledOnce).toBeTruthy();
        expect(comp.modelloLiberatorias[0]).toEqual(expect.objectContaining({ id: 123 }));
      });
    });
    describe('Handles', () => {
      let comp: ModelloLiberatoriaComponentType;

      beforeEach(async () => {
        const wrapper = shallowMount(ModelloLiberatoria, { global: mountOptions });
        comp = wrapper.vm;
        await comp.$nextTick();
        modelloLiberatoriaServiceStub.retrieve.reset();
        modelloLiberatoriaServiceStub.retrieve.resolves({ headers: {}, data: [] });
      });

      it('Should call delete service on confirmDelete', async () => {
        // GIVEN
        modelloLiberatoriaServiceStub.delete.resolves({});

        // WHEN
        comp.prepareRemove({ id: 123 });

        comp.removeModelloLiberatoria();
        await comp.$nextTick(); // clear components

        // THEN
        expect(modelloLiberatoriaServiceStub.delete.called).toBeTruthy();

        // THEN
        await comp.$nextTick(); // handle component clear watch
        expect(modelloLiberatoriaServiceStub.retrieve.callCount).toEqual(1);
      });
    });
  });
});
