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 DisponibilitaService from './disponibilita.service';
import Disponibilita from './disponibilita.vue';

type DisponibilitaComponentType = InstanceType<typeof Disponibilita>;

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

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

  describe('Disponibilita Management Component', () => {
    let disponibilitaServiceStub: SinonStubbedInstance<DisponibilitaService>;
    let mountOptions: MountingOptions<DisponibilitaComponentType>['global'];

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

      alertService = new AlertService({
        i18n: { t: vitest.fn() } as any,
        toast: {
          show: vitest.fn(),
        } as any,
      });

      mountOptions = {
        stubs: {
          jhiItemCount: true,
          bPagination: true,
          bModal: bModalStub as any,
          'font-awesome-icon': true,
          'b-badge': true,
          'jhi-sort-indicator': true,
          'b-button': true,
          'router-link': true,
        },
        directives: {
          'b-modal': {},
        },
        provide: {
          alertService,
          disponibilitaService: () => disponibilitaServiceStub,
        },
      };
    });

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

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

        // THEN
        expect(disponibilitaServiceStub.retrieve.calledOnce).toBeTruthy();
        expect(comp.disponibilitas[0]).toEqual(expect.objectContaining({ id: 123 }));
      });

      it('should calculate the sort attribute for an id', async () => {
        // WHEN
        const wrapper = shallowMount(Disponibilita, { global: mountOptions });
        const comp = wrapper.vm;
        await comp.$nextTick();

        // THEN
        expect(disponibilitaServiceStub.retrieve.lastCall.firstArg).toMatchObject({
          sort: ['id,asc'],
        });
      });
    });
    describe('Handles', () => {
      let comp: DisponibilitaComponentType;

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

      it('should load a page', async () => {
        // GIVEN
        disponibilitaServiceStub.retrieve.resolves({ headers: {}, data: [{ id: 123 }] });

        // WHEN
        comp.page = 2;
        await comp.$nextTick();

        // THEN
        expect(disponibilitaServiceStub.retrieve.called).toBeTruthy();
        expect(comp.disponibilitas[0]).toEqual(expect.objectContaining({ id: 123 }));
      });

      it('should re-initialize the page', async () => {
        // GIVEN
        comp.page = 2;
        await comp.$nextTick();
        disponibilitaServiceStub.retrieve.reset();
        disponibilitaServiceStub.retrieve.resolves({ headers: {}, data: [{ id: 123 }] });

        // WHEN
        comp.clear();
        await comp.$nextTick();

        // THEN
        expect(comp.page).toEqual(1);
        expect(disponibilitaServiceStub.retrieve.callCount).toEqual(1);
        expect(comp.disponibilitas[0]).toEqual(expect.objectContaining({ id: 123 }));
      });

      it('should calculate the sort attribute for a non-id attribute', async () => {
        // WHEN
        comp.propOrder = 'name';
        await comp.$nextTick();

        // THEN
        expect(disponibilitaServiceStub.retrieve.lastCall.firstArg).toMatchObject({
          sort: ['name,asc', 'id'],
        });
      });

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

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

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

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

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