54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { ApiService } from './api.service';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {LocalStorageService} from 'angular-2-local-storage';
|
|
import {Observable} from 'rxjs';
|
|
|
|
let apiService: ApiService;
|
|
let httpClientSpy: jasmine.SpyObj<HttpClient>;
|
|
let localStorageServiceSpy: jasmine.SpyObj<LocalStorageService>;
|
|
|
|
describe('ApiService', () => {
|
|
beforeEach(() => {
|
|
const httpSpy = jasmine.createSpyObj('HttpClient', ['get']);
|
|
const localStorageSpy = jasmine.createSpyObj('LocalStorageService', ['get']);
|
|
|
|
TestBed.configureTestingModule({
|
|
providers: [
|
|
ApiService,
|
|
{provide: HttpClient, useValue: httpSpy},
|
|
{provide: LocalStorageService, useValue: localStorageSpy}
|
|
]
|
|
});
|
|
|
|
apiService = TestBed.inject(ApiService);
|
|
httpClientSpy = TestBed.inject(HttpClient) as jasmine.SpyObj<HttpClient>;
|
|
localStorageServiceSpy = TestBed.inject(LocalStorageService) as jasmine.SpyObj<LocalStorageService>;
|
|
});
|
|
|
|
it('should be created', () => {
|
|
expect(apiService).toBeTruthy();
|
|
});
|
|
|
|
it('should do get requests without auth if not available', () => {
|
|
const getReturn = new Observable();
|
|
httpClientSpy.get.and.returnValue(getReturn);
|
|
localStorageServiceSpy.get.and.returnValue(undefined);
|
|
|
|
const result = apiService.get('/testurl');
|
|
expect(result).toBe(getReturn);
|
|
expect(httpClientSpy.get).toHaveBeenCalledWith('/testurl', {});
|
|
expect(localStorageServiceSpy.get).toHaveBeenCalledWith('basicAuth');
|
|
});
|
|
it('should do get requests with basic auth if available', () => {
|
|
const getReturn = new Observable();
|
|
httpClientSpy.get.and.returnValue(getReturn);
|
|
localStorageServiceSpy.get.and.returnValue('basicAuth');
|
|
|
|
const result = apiService.get('/testurl');
|
|
expect(result).toBe(getReturn);
|
|
expect(httpClientSpy.get).toHaveBeenCalledWith('/testurl', jasmine.any(Object)); // could not test http headers better
|
|
expect(localStorageServiceSpy.get).toHaveBeenCalledWith('basicAuth');
|
|
});
|
|
});
|