Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Aug 11, 2020
1 parent fb78aad commit 210ed9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 61 deletions.
53 changes: 10 additions & 43 deletions lib/core/services/ecm-user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,47 @@
import { TestBed } from '@angular/core/testing';
import { fakeEcmUser } from '../mock/ecm-user.service.mock';
import { EcmUserService } from '../services/ecm-user.service';
import { setupTestBed } from '../testing/setup-test-bed';
import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { AuthenticationService } from './authentication.service';
import { ContentService } from './content.service';

declare let jasmine: any;

describe('EcmUserService', () => {

let service: EcmUserService;
let authService: AuthenticationService;
let contentService: ContentService;

setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule
]
});

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
CoreTestingModule
]
});

service = TestBed.inject(EcmUserService);
authService = TestBed.inject(AuthenticationService);
contentService = TestBed.inject(ContentService);
});

beforeEach(() => {
jasmine.Ajax.install();
});

afterEach(() => {
jasmine.Ajax.uninstall();
});

describe('when user is logged in', () => {

beforeEach(() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
});

it('should be able to retrieve current user info', (done) => {
spyOn(service.peopleApi, 'getPerson').and.returnValue(Promise.resolve({ entry: fakeEcmUser }));
service.getCurrentUserInfo().subscribe(
(user) => {
expect(user).toBeDefined();
expect(user.firstName).toEqual('fake-ecm-first-name');
expect(user.lastName).toEqual('fake-ecm-last-name');
expect(user.email).toEqual('fakeEcm@ecmUser.com');
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'application/json',
responseText: JSON.stringify({entry: fakeEcmUser})
});
});

it('should be able to log errors on call', (done) => {
service.getCurrentUserInfo().subscribe(() => {
}, () => {
done();
});

jasmine.Ajax.requests.mostRecent().respondWith({
status: 403
});
}
);
});

it('should retrieve avatar url for current user', () => {
Expand All @@ -92,13 +67,5 @@ describe('EcmUserService', () => {

expect(urlRs).toEqual('fake/url/image/for/ecm/user');
});

it('should not call content service without avatar id', () => {
spyOn(contentService, 'getContentUrl').and.callThrough();
const urlRs = service.getUserProfileImage(undefined);

expect(urlRs).toBeNull();
expect(contentService.getContentUrl).not.toHaveBeenCalled();
});
});
});
26 changes: 8 additions & 18 deletions lib/core/services/ecm-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
*/

import { Injectable } from '@angular/core';
import { Observable, from, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Observable, from } from 'rxjs';
import { map } from 'rxjs/operators';
import { ContentService } from './content.service';
import { AlfrescoApiService } from './alfresco-api.service';
import { LogService } from './log.service';
import { EcmUserModel } from '../models/ecm-user.model';
import { PeopleApi } from '@alfresco/js-api';

Expand All @@ -29,13 +28,14 @@ import { PeopleApi } from '@alfresco/js-api';
})
export class EcmUserService {

private _peopleApi: PeopleApi;

constructor(private apiService: AlfrescoApiService,
private contentService: ContentService,
private logService: LogService) {
private contentService: ContentService) {
}

private get peopleApi(): PeopleApi {
return new PeopleApi(this.apiService.getInstance());
get peopleApi(): PeopleApi {
return this._peopleApi || (this._peopleApi = new PeopleApi(this.apiService.getInstance()));
}

/**
Expand All @@ -46,8 +46,7 @@ export class EcmUserService {
getUserInfo(userName: string): Observable<EcmUserModel> {
return from(this.peopleApi.getPerson(userName))
.pipe(
map((personEntry) => new EcmUserModel(personEntry.entry)),
catchError((err) => this.handleError(err))
map((personEntry) => new EcmUserModel(personEntry.entry))
);
}

Expand All @@ -67,13 +66,4 @@ export class EcmUserService {
getUserProfileImage(avatarId: string): string {
return this.contentService.getContentUrl(avatarId);
}

/**
* Throw the error
* @param error
*/
private handleError(error: any) {
this.logService.error(error);
return throwError(error || 'Server error');
}
}

0 comments on commit 210ed9c

Please sign in to comment.