<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const enumImports = this.generateEntityClientEnumImports(fields.filter(field => !field.id));
_%>
/* tslint:disable max-line-length */
import axios from 'axios';
import sinon from 'sinon'
<%_ if (anyFieldIsDateDerived) { _%>
import dayjs from 'dayjs';
<%_ } _%>

<%_ if (anyFieldIsLocalDate || anyFieldIsInstant || anyFieldIsZonedDateTime) { _%>
import { <%_ if (anyFieldIsLocalDate) { _%>DATE_FORMAT,<%_ } if (anyFieldIsInstant || anyFieldIsZonedDateTime) {_%> DATE_TIME_FORMAT<%_ } _%> } from '@/shared/composables/date-format';
<%_ } _%>
import <%= entityAngularName %>Service from './<%= entityFileName %>.service';
import { <%= entityAngularName %> } from '@/models/<%= entityModelFileName %>.model';
<%_ enumImports.forEach( (importedPath, importedType) => { _%>
import { <%- importedType %> } from '<%- importedPath %>';
<%_ }); _%>

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('<%= entityAngularName %> Service', () => {
    let service;
    let elemDefault;
<%_ if (anyFieldIsDateDerived) { _%>
    let currentDate: Date;
<%_ } _%>

    beforeEach(() => {
      service = <%= entityAngularName %>Service;
<%_ if (anyFieldIsDateDerived) { _%>
      currentDate = new Date();
<%_ } _%>
      elemDefault = new <%= entityAngularName %>(
        <%- this.generateTestEntityId(primaryKey.type) %>,
<%_ fields.filter(field => !field.id).forEach((field) => {
    const fieldType = field.fieldType; _%>
  <%_ if (field.fieldIsEnum) { _%>
        '<%= field.enumValues[0].name  %>',
  <%_ } else if (field.fieldTypeBoolean) { _%>
        false,
  <%_ } else if (field.fieldTypeDuration) { _%>
        'PT1S',
  <%_ } else if (field.fieldTypeNumeric) { _%>
        0,
  <%_ } else if (field.fieldTypeCharSequence) { _%>
        'AAAAAAA',
  <%_ } else if (field.fieldTypeTemporal) { _%>
        currentDate,
  <%_ } else if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
        'image/png',
        'AAAAAAA',
  <%_ } else { /* (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'any' || (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'image' || fieldType === 'LocalDate' */ _%>
        'AAAAAAA',
  <%_ } _%>
<%_ }) _%>
      );
    });

    describe('Service methods', () => {
      it('should find an element', async () => {
        const returnedFromService = Object.assign({
<%_ fields.filter(field => !field.id).forEach((field) => { _%>
  <%_ if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_FORMAT),
  <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_TIME_FORMAT),
  <%_ } _%>
<%_ }) _%>
        }, elemDefault);
        axiosStub.get.resolves({ data: returnedFromService });

        return service
          .find(<%- tsKeyId %>).then((res) => {
            expect(res).toMatchObject(elemDefault);
        });
      });

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

<%_ if (!readOnly) { _%>
      it('should create a <%= entityAngularName %>', async () => {
        const returnedFromService = Object.assign({
          <%= primaryKey.name %>: <%- this.generateTestEntityId(primaryKey.type) %>,
  <%_ fields.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_FORMAT),
    <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_TIME_FORMAT),
    <%_ } _%>
  <%_ }) _%>
        }, elemDefault);
        const expected = Object.assign({
  <%_ fields.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: currentDate,
    <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: currentDate,
    <%_ } _%>
  <%_ }) _%>
          }, returnedFromService);

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

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

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

      it('should update a <%= entityAngularName %>', async () => {
        const returnedFromService = Object.assign({
  <%_ fields.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeBoolean) { _%>
          <%= field.fieldName%>: true,
    <%_ } else if (field.fieldTypeDuration) { _%>
          <%= field.fieldName%>: 'PT2S',
    <%_ } else if (field.fieldTypeNumeric) { _%>
          <%= field.fieldName%>: 1,
    <%_ } else if (field.fieldTypeCharSequence) { _%>
          <%= field.fieldName%>: 'BBBBBB',
    <%_ } else if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_FORMAT),
    <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_TIME_FORMAT),
    <%_ } else { _%>
          <%= field.fieldName%>: 'BBBBBB',
    <%_ } _%>
  <%_ }) _%>
          }, elemDefault);

          const expected = Object.assign({
  <%_ fields.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: currentDate,
    <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: currentDate,
    <%_ } _%>
  <%_ }) _%>
          }, returnedFromService);
          axiosStub.put.resolves({ data: returnedFromService });

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

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

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

  <% const fieldsToIncludeInPartialPatchTest = fields.filter(field => !field.id && !field.transient && faker.datatype.boolean()); %>
      it('should partial update a <%= entityAngularName %>', async () => {
          const patchObject = Object.assign({
  <%_ fieldsToIncludeInPartialPatchTest.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeBoolean) { _%>
                  <%= field.fieldName%>: true,
    <%_ } else if (field.fieldTypeDuration) { _%>
                  <%= field.fieldName%>: 'PT2S',
    <%_ } else if (field.fieldTypeNumeric) { _%>
                  <%= field.fieldName%>: 1,
    <%_ } else if (field.fieldTypeCharSequence) { _%>
                  <%= field.fieldName%>: 'BBBBBB',
    <%_ } else if (field.fieldTypeLocalDate) { _%>
                  <%= field.fieldName%>: dayjs(currentDate).format(DATE_FORMAT),
    <%_ } else if (field.fieldTypeTimed) { _%>
                  <%= field.fieldName%>: dayjs(currentDate).format(DATE_TIME_FORMAT),
    <%_ } else { _%>
                  <%= field.fieldName%>: 'BBBBBB',
    <%_ } _%>
  <%_ }) _%>
          }, new <%= entityAngularName %>())
          const returnedFromService = Object.assign(patchObject, elemDefault);

          const expected = Object.assign({
  <%_ fields.filter(field => !field.id).forEach((field) => { _%>
    <%_ if (field.fieldTypeLocalDate) { _%>
                  <%= field.fieldName%>: currentDate,
    <%_ } else if (field.fieldTypeTimed) { _%>
                  <%= field.fieldName%>: currentDate,
    <%_ } _%>
  <%_ }) _%>
          }, returnedFromService);
          axiosStub.patch.resolves({ data: returnedFromService });

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

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

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

<%_ } _%>
      it('should return a list of <%= entityAngularName %>', async () => {
        const returnedFromService = Object.assign({
<%_ fields.filter(field => !field.id).forEach((field) => { _%>
  <%_ if (field.fieldTypeBoolean) { _%>
          <%= field.fieldName%>: true,
  <%_ } else if (field.fieldTypeDuration) { _%>
          <%= field.fieldName%>: 'PT2S',
  <%_ } else if (field.fieldTypeNumeric) { _%>
          <%= field.fieldName%>: 1,
  <%_ } else if (field.fieldTypeCharSequence) { _%>
          <%= field.fieldName%>: 'BBBBBB',
  <%_ } else if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_FORMAT),
  <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: dayjs(currentDate).format(DATE_TIME_FORMAT),
  <%_ } else { /* (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'any' || (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'image' || fieldType === 'LocalDate' */ _%>
          <%= field.fieldName%>: 'BBBBBB',
  <%_ } _%>
<%_ }) _%>
        }, elemDefault);
        const expected = Object.assign({
<%_ fields.filter(field => !field.id).forEach((field) => { _%>
  <%_ if (field.fieldTypeLocalDate) { _%>
          <%= field.fieldName%>: currentDate,
  <%_ } else if (field.fieldTypeTimed) { _%>
          <%= field.fieldName%>: currentDate,
  <%_ } _%>
<%_ }) _%>
        }, returnedFromService);
        axiosStub.get.resolves([returnedFromService]);
        return service.retrieve(<% if (!paginationNo) { %>{ sort: {}, page: 0, size: 10 }<% } %>).then((res) => {
          expect(res).toContainEqual(expected);
        });
      });

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

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

<%_ if (!readOnly) { _%>
      it('should delete a <%= entityAngularName %>', async () => {
          axiosStub.delete.resolves({ok: true});
          return service.delete(<%- tsKeyId %>).then((res) => {
              expect(res.ok).toBeTruthy();
          });
      });

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

        return service.delete(<%- tsKeyId %>)
          .then()
          .catch(err => {
            expect(err).toMatchObject(error);
          });
      });
<%_ } _%>
    });
  });
});
