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 NotificaDetails from './notifica-details.vue';
import NotificaService from './notifica.service';

type NotificaDetailsComponentType = InstanceType<typeof NotificaDetails>;

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

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

const notificaSample = { id: 123 };

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

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

  describe('Notifica Management Detail Component', () => {
    let notificaServiceStub: SinonStubbedInstance<NotificaService>;
    let mountOptions: MountingOptions<NotificaDetailsComponentType>['global'];

    beforeEach(() => {
      route = {};
      notificaServiceStub = sinon.createStubInstance<NotificaService>(NotificaService);

      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,
          notificaService: () => notificaServiceStub,
        },
      };
    });

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

        // THEN
        expect(comp.notifica).toMatchObject(notificaSample);
      });
    });

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

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

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