import axios from 'axios';
import sinon from 'sinon';

import { UtenteApp } from '@/shared/model/utente-app.model';

import UtenteAppService from './utente-app.service';

const error = {
  response: {
    status: null,
    data: {
      type: null,
    },
  },
};

const axiosStub = {
  get: sinon.stub(axios, 'get'),
  post: sinon.stub(axios, 'post'),
  put: sinon.stub(axios, 'put'),
  patch: sinon.stub(axios, 'patch'),
  delete: sinon.stub(axios, 'delete'),
};

describe('Service Tests', () => {
  describe('UtenteApp Service', () => {
    let service: UtenteAppService;
    let elemDefault;

    beforeEach(() => {
      service = new UtenteAppService();
      elemDefault = new UtenteApp(
        123,
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'USER',
        false,
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
        'AAAAAAA',
      );
    });

    describe('Service methods', () => {
      it('should find an element', async () => {
        const returnedFromService = { ...elemDefault };
        axiosStub.get.resolves({ data: returnedFromService });

        return service.find(123).then(res => {
          expect(res).toMatchObject(elemDefault);
        });
      });

      it('should not find an element', async () => {
        axiosStub.get.rejects(error);
        return service
          .find(123)
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });

      it('should create a UtenteApp', async () => {
        const returnedFromService = { id: 123, ...elemDefault };
        const expected = { ...returnedFromService };

        axiosStub.post.resolves({ data: returnedFromService });
        return service.create({}).then(res => {
          expect(res).toMatchObject(expected);
        });
      });

      it('should not create a UtenteApp', async () => {
        axiosStub.post.rejects(error);

        return service
          .create({})
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });

      it('should update a UtenteApp', async () => {
        const returnedFromService = {
          username: 'BBBBBB',
          email: 'BBBBBB',
          telefono: 'BBBBBB',
          ruolo: 'BBBBBB',
          attivo: true,
          nome: 'BBBBBB',
          cognome: 'BBBBBB',
          luogoNascita: 'BBBBBB',
          dataNascita: 'BBBBBB',
          residente: 'BBBBBB',
          societa: 'BBBBBB',
          sede: 'BBBBBB',
          codfiscale: 'BBBBBB',
          telefonoSoc: 'BBBBBB',
          emailSoc: 'BBBBBB',
          ...elemDefault,
        };

        const expected = { ...returnedFromService };
        axiosStub.put.resolves({ data: returnedFromService });

        return service.update(expected).then(res => {
          expect(res).toMatchObject(expected);
        });
      });

      it('should not update a UtenteApp', async () => {
        axiosStub.put.rejects(error);

        return service
          .update({})
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });

      it('should partial update a UtenteApp', async () => {
        const patchObject = {
          username: 'BBBBBB',
          telefono: 'BBBBBB',
          ruolo: 'BBBBBB',
          nome: 'BBBBBB',
          cognome: 'BBBBBB',
          luogoNascita: 'BBBBBB',
          societa: 'BBBBBB',
          sede: 'BBBBBB',
          codfiscale: 'BBBBBB',
          emailSoc: 'BBBBBB',
          ...new UtenteApp(),
        };
        const returnedFromService = Object.assign(patchObject, elemDefault);

        const expected = { ...returnedFromService };
        axiosStub.patch.resolves({ data: returnedFromService });

        return service.partialUpdate(patchObject).then(res => {
          expect(res).toMatchObject(expected);
        });
      });

      it('should not partial update a UtenteApp', async () => {
        axiosStub.patch.rejects(error);

        return service
          .partialUpdate({})
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });

      it('should return a list of UtenteApp', async () => {
        const returnedFromService = {
          username: 'BBBBBB',
          email: 'BBBBBB',
          telefono: 'BBBBBB',
          ruolo: 'BBBBBB',
          attivo: true,
          nome: 'BBBBBB',
          cognome: 'BBBBBB',
          luogoNascita: 'BBBBBB',
          dataNascita: 'BBBBBB',
          residente: 'BBBBBB',
          societa: 'BBBBBB',
          sede: 'BBBBBB',
          codfiscale: 'BBBBBB',
          telefonoSoc: 'BBBBBB',
          emailSoc: 'BBBBBB',
          ...elemDefault,
        };
        const expected = { ...returnedFromService };
        axiosStub.get.resolves([returnedFromService]);
        return service.retrieve().then(res => {
          expect(res).toContainEqual(expected);
        });
      });

      it('should not return a list of UtenteApp', async () => {
        axiosStub.get.rejects(error);

        return service
          .retrieve()
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });

      it('should delete a UtenteApp', async () => {
        axiosStub.delete.resolves({ ok: true });
        return service.delete(123).then(res => {
          expect(res.ok).toBeTruthy();
        });
      });

      it('should not delete a UtenteApp', async () => {
        axiosStub.delete.rejects(error);

        return service
          .delete(123)
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });
    });
  });
});
