/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { inject, TestBed, waitForAsync } from '@angular/core/testing';
import { User } from './user';
import { UserService } from './user-service.service';

describe('UserService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [UserService],
    });
  });

  it('should be created', inject([UserService], (service: UserService) => {
    expect(service).toBeTruthy();
  }));

  it(
    'should return all users',
    waitForAsync(
      inject([UserService], (service: UserService) => {
        service.queryAll().subscribe((result) => {
          expect(result.length).toBe(11);
        });
      })
    )
  );

  it(
    'should return user Bob',
    waitForAsync(
      inject([UserService], (service: UserService) => {
        service.querySingle('1').subscribe((user) => {
          expect(user.name).toBe('Bob');
        });
      })
    )
  );

  it('should append user', inject([UserService], (service: UserService) => {
    const userToCreate: User = new User('11', 'Max', 'User', 'Sales', 'Max@company.com');
    const insertedUser = service.upsert(userToCreate);
    expect(insertedUser.id).toBe(userToCreate.id);
    expect(insertedUser.name).toBe(userToCreate.name);
  }));

  it('should update user', inject([UserService], (service: UserService) => {
    const userToUpdate: User = new User('1', 'Bobby', 'User', 'Security', 'Bob@company.com');
    const updatedUser = service.upsert(userToUpdate);
    expect(updatedUser.name).toBe('Bobby');
    expect(updatedUser.role).toBe('User');
  }));

  it(
    'should calculate number of pages',
    waitForAsync(
      inject([UserService], (service: UserService) => {
        service.queryAll().subscribe(() => {
          expect(service.pageCount).toBe(1);
          expect(service.currentPage).toBe(0);
          expect(service.itemCount).toBe(11);
        });
      })
    )
  );

  it(
    'should calculate number of pages with pagesize 5',
    waitForAsync(
      inject([UserService], (service: UserService) => {
        service.pageSize = 5;
        service.queryAll().subscribe(() => {
          expect(service.pageCount).toBe(3);
          expect(service.currentPage).toBe(0);
          expect(service.itemCount).toBe(5);
        });
      })
    )
  );
});
