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

import UtenteAppUpdate from './utente-app-update.vue';
import UtenteAppService from './utente-app.service';

type UtenteAppUpdateComponentType = InstanceType<typeof UtenteAppUpdate>;

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

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

const utenteAppSample = { id: 123 };

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

  describe('UtenteApp Management Update Component', () => {
    let comp: UtenteAppUpdateComponentType;
    let utenteAppServiceStub: SinonStubbedInstance<UtenteAppService>;

    beforeEach(() => {
      route = {};
      utenteAppServiceStub = sinon.createStubInstance<UtenteAppService>(UtenteAppService);
      utenteAppServiceStub.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,
          utenteAppService: () => utenteAppServiceStub,

          userService: () =>
            sinon.createStubInstance<UserService>(UserService, {
              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(UtenteAppUpdate, { global: mountOptions });
        comp = wrapper.vm;
        comp.utenteApp = utenteAppSample;
        utenteAppServiceStub.update.resolves(utenteAppSample);

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

        // THEN
        expect(utenteAppServiceStub.update.calledWith(utenteAppSample)).toBeTruthy();
        expect(comp.isSaving).toEqual(false);
      });

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

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

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

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

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

        // THEN
        expect(comp.utenteApp).toMatchObject(utenteAppSample);
      });
    });

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

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

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