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 UtenteAppService from '@/entities/utente-app/utente-app.service';
import AlertService from '@/shared/alert/alert.service';

import ConfermaUpdate from './conferma-update.vue';
import ConfermaService from './conferma.service';

type ConfermaUpdateComponentType = InstanceType<typeof ConfermaUpdate>;

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

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

const confermaSample = { id: 123 };

describe('Component Tests', () => {
  let mountOptions: MountingOptions<ConfermaUpdateComponentType>['global'];
  let alertService: AlertService;

  describe('Conferma Management Update Component', () => {
    let comp: ConfermaUpdateComponentType;
    let confermaServiceStub: SinonStubbedInstance<ConfermaService>;

    beforeEach(() => {
      route = {};
      confermaServiceStub = sinon.createStubInstance<ConfermaService>(ConfermaService);
      confermaServiceStub.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,
          confermaService: () => confermaServiceStub,
          utenteAppService: () =>
            sinon.createStubInstance<UtenteAppService>(UtenteAppService, {
              retrieve: sinon.stub().resolves({}),
            } as any),
        },
      };
    });

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

    describe('save', () => {
      it('Should call update service on save for existing entity', async () => {
        // GIVEN
        const wrapper = shallowMount(ConfermaUpdate, { global: mountOptions });
        comp = wrapper.vm;
        comp.conferma = confermaSample;
        confermaServiceStub.update.resolves(confermaSample);

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

        // THEN
        expect(confermaServiceStub.update.calledWith(confermaSample)).toBeTruthy();
        expect(comp.isSaving).toEqual(false);
      });

      it('Should call create service on save for new entity', async () => {
        // GIVEN
        const entity = {};
        confermaServiceStub.create.resolves(entity);
        const wrapper = shallowMount(ConfermaUpdate, { global: mountOptions });
        comp = wrapper.vm;
        comp.conferma = entity;

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

        // THEN
        expect(confermaServiceStub.create.calledWith(entity)).toBeTruthy();
        expect(comp.isSaving).toEqual(false);
      });
    });

    describe('Before route enter', () => {
      it('Should retrieve data', async () => {
        // GIVEN
        confermaServiceStub.find.resolves(confermaSample);
        confermaServiceStub.retrieve.resolves([confermaSample]);

        // WHEN
        route = {
          params: {
            confermaId: `${confermaSample.id}`,
          },
        };
        const wrapper = shallowMount(ConfermaUpdate, { global: mountOptions });
        comp = wrapper.vm;
        await comp.$nextTick();

        // THEN
        expect(comp.conferma).toMatchObject(confermaSample);
      });
    });

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

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

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