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 ModelloLiberatoriaDetails from './modello-liberatoria-details.vue';
import ModelloLiberatoriaService from './modello-liberatoria.service';

type ModelloLiberatoriaDetailsComponentType = InstanceType<typeof ModelloLiberatoriaDetails>;

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

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

const modelloLiberatoriaSample = { id: 123 };

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

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

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

    beforeEach(() => {
      route = {};
      modelloLiberatoriaServiceStub = sinon.createStubInstance<ModelloLiberatoriaService>(ModelloLiberatoriaService);

      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,
          modelloLiberatoriaService: () => modelloLiberatoriaServiceStub,
        },
      };
    });

    describe('Navigate to details', () => {
      it('Should call load all on init', async () => {
        // GIVEN
        modelloLiberatoriaServiceStub.find.resolves(modelloLiberatoriaSample);
        route = {
          params: {
            modelloLiberatoriaId: `${123}`,
          },
        };
        const wrapper = shallowMount(ModelloLiberatoriaDetails, { global: mountOptions });
        const comp = wrapper.vm;
        // WHEN
        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(ModelloLiberatoriaDetails, { global: mountOptions });
        const comp = wrapper.vm;
        await comp.$nextTick();

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

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