import { vitest } from 'vitest';
import { type RouteLocation } from 'vue-router';

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

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

import DisponibilitaDetails from './disponibilita-details.vue';
import DisponibilitaService from './disponibilita.service';

type DisponibilitaDetailsComponentType = InstanceType<typeof DisponibilitaDetails>;

let route: Partial<RouteLocation>;
const routerGoMock = vitest.fn();

vitest.mock('vue-router', () => ({
  useRoute: () => route,
  useRouter: () => ({ go: routerGoMock }),
}));

const disponibilitaSample = { id: 123 };

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

  afterEach(() => {
    vitest.resetAllMocks();
  });

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

    beforeEach(() => {
      route = {};
      disponibilitaServiceStub = sinon.createStubInstance<DisponibilitaService>(DisponibilitaService);

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

      mountOptions = {
        stubs: {
          'font-awesome-icon': true,
          'router-link': true,
        },
        provide: {
          alertService,
          disponibilitaService: () => disponibilitaServiceStub,
        },
      };
    });

    describe('Navigate to details', () => {
      it('Should call load all on init', async () => {
        // GIVEN
        disponibilitaServiceStub.find.resolves(disponibilitaSample);
        route = {
          params: {
            disponibilitaId: `${123}`,
          },
        };
        const wrapper = shallowMount(DisponibilitaDetails, { global: mountOptions });
        const comp = wrapper.vm;
        // WHEN
        await comp.$nextTick();

        // THEN
        expect(comp.disponibilita).toMatchObject(disponibilitaSample);
      });
    });

    describe('Previous state', () => {
      it('Should go previous state', async () => {
        disponibilitaServiceStub.find.resolves(disponibilitaSample);
        const wrapper = shallowMount(DisponibilitaDetails, { global: mountOptions });
        const comp = wrapper.vm;
        await comp.$nextTick();

        comp.previousState();
        await comp.$nextTick();

        expect(routerGoMock).toHaveBeenCalledWith(-1);
      });
    });
  });
});
