import { HttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';

import { AuthService } from './auth.service';

describe('AuthService', () => {
  let service: AuthService;
  const mockHttp = jasmine.createSpyObj('HttpClient', ['post']);
  mockHttp.post.and.returnValue(of({
    token: 'token',
    user: {id: 1, email: 'mail', firstName: 'first', lastName: 'last'}
  }))

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [],
      providers: [{ provide: HttpClient, useValue: mockHttp }]
    });
    service = TestBed.inject(AuthService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should log in', async () => {
    expect(service['authInfoSubject'].value).toBeNull();
    const success = await service.login('mail', 'passwort123');
    expect(success).toBeTrue();
    expect(service['authInfoSubject'].value).not.toBeNull();
    expect(service['authInfoSubject'].value.id).toBe(1);
    expect(service['authInfoSubject'].value.email).toEqual('mail');
    expect(service['authInfoSubject'].value.firstName).toEqual('first');
    expect(service['authInfoSubject'].value.lastName).toEqual('last');
  })
});
