<%#
 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(user.primaryKey.type);
const testEntityPrimaryKey0 = this.generateTestEntityPrimaryKey(user.primaryKey, 0);
const testEntityPrimaryKey1 = this.generateTestEntityPrimaryKey(user.primaryKey, 1);
_%>
import { TestBed } from '@angular/core/testing';
import { HttpErrorResponse } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { User, IUser } from './user.model';

import { UserService } from './user.service';

describe('User Service', () => {
  let service: UserService;
  let httpMock: HttpTestingController;
  let expectedResult: IUser | IUser[] | boolean | number | null;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
    });
    expectedResult = null;
    service = TestBed.inject(UserService);
    httpMock = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    httpMock.verify();
  });

  describe('Service methods', () => {
    it('should return Users', () => {
      service.query().subscribe(received => {
        expectedResult = received.body;
      });

      const req = httpMock.expectOne({ method: 'GET' });
      req.flush([new User(<%- tsKeyId %>, 'user')]);
      expect(expectedResult).toEqual([{ id: <%- tsKeyId %>, login: 'user' }]);
    });

    it('should propagate not found response', () => {
      service.query().subscribe({
        error: (error: HttpErrorResponse) => expectedResult = error.status
      });

      const req = httpMock.expectOne({ method: 'GET' });
      req.flush('Internal Server Error', {
        status: 500,
        statusText: 'Internal Server Error',
      });
      expect(expectedResult).toEqual(500);
    });

    describe('addUserToCollectionIfMissing', () => {
      it('should add a User to an empty array', () => {
        const user: IUser = <%- testEntityPrimaryKey0 %>;
        expectedResult = service.addUserToCollectionIfMissing([], user);
        expect(expectedResult).toHaveLength(1);
        expect(expectedResult).toContain(user);
      });

      it('should not add a User to an array that contains it', () => {
        const user: IUser = <%- testEntityPrimaryKey0 %>;
        const userCollection: IUser[] = [
          {
            ...user,
          },
          <%- testEntityPrimaryKey1 %>,
        ];
        expectedResult = service.addUserToCollectionIfMissing(userCollection, user);
        expect(expectedResult).toHaveLength(2);
      });

      it("should add a User to an array that doesn't contain it", () => {
        const user: IUser = <%- testEntityPrimaryKey0 %>;
        const userCollection: IUser[] = [<%- testEntityPrimaryKey1 %>];
        expectedResult = service.addUserToCollectionIfMissing(userCollection, user);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(user);
      });

      it('should add only unique User to an array', () => {
        const userArray: IUser[] = [<%- testEntityPrimaryKey0 %>, <%- testEntityPrimaryKey1 %>, <%- this.generateTestEntityPrimaryKey(user.primaryKey) %>];
        const userCollection: IUser[] = [<%- testEntityPrimaryKey1 %>];
        expectedResult = service.addUserToCollectionIfMissing(userCollection, ...userArray);
        expect(expectedResult).toHaveLength(3);
      });

      it("should accept varargs", () => {
        const user: IUser = <%- testEntityPrimaryKey0 %>;
        const user2: IUser = <%- testEntityPrimaryKey1 %>;
        expectedResult = service.addUserToCollectionIfMissing([], user, user2);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(user);
        expect(expectedResult).toContain(user2);
      });

      it("should accept null and undefined values", () => {
        const user: IUser = <%- testEntityPrimaryKey0 %>;
        expectedResult = service.addUserToCollectionIfMissing([], null, user, undefined);
        expect(expectedResult).toHaveLength(1);
        expect(expectedResult).toContain(user);
      });

      it('should return initial array if no users is added', () => {
        const userCollection: IUser[] = [<%- testEntityPrimaryKey1 %>];
        expectedResult = service.addUserToCollectionIfMissing(userCollection, null, undefined);
        expect(expectedResult).toEqual(userCollection);
      });
    });
  });
});
