Skip to content

Commit fda4572

Browse files
author
Kuno Vercammen
committed
113904: Added tests for the getEPersonName function
1 parent 596b0db commit fda4572

2 files changed

Lines changed: 41 additions & 36 deletions

File tree

src/app/process-page/overview/table/process-overview-table.component.spec.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ describe('ProcessOverviewTableComponent', () => {
5656
let modalService: NgbModal;
5757
let authService; // : AuthService; Not typed as the mock does not fully implement AuthService
5858
let routeService: RouteService;
59-
let translateService: TranslateService;
6059

6160
let processes: Process[];
6261
let ePerson: EPerson;
6362

63+
let translateServiceSpy: jasmine.SpyObj<TranslateService>;
64+
6465
function init() {
6566
processes = [
6667
Object.assign(new Process(), {
@@ -69,23 +70,28 @@ describe('ProcessOverviewTableComponent', () => {
6970
startTime: '2020-03-19 00:30:00',
7071
endTime: '2020-03-19 23:30:00',
7172
processStatus: ProcessStatus.COMPLETED,
73+
userId: 'testid',
7274
}),
7375
Object.assign(new Process(), {
7476
processId: 2,
7577
scriptName: 'script-b',
7678
startTime: '2020-03-20 00:30:00',
7779
endTime: '2020-03-20 23:30:00',
7880
processStatus: ProcessStatus.FAILED,
81+
userId: 'testid',
7982
}),
8083
Object.assign(new Process(), {
8184
processId: 3,
8285
scriptName: 'script-c',
8386
startTime: '2020-03-21 00:30:00',
8487
endTime: '2020-03-21 23:30:00',
8588
processStatus: ProcessStatus.RUNNING,
89+
userId: 'testid',
8690
}),
8791
];
8892
ePerson = Object.assign(new EPerson(), {
93+
id: 'testid',
94+
uuid: 'testid',
8995
metadata: {
9096
'eperson.firstname': [
9197
{
@@ -144,6 +150,8 @@ describe('ProcessOverviewTableComponent', () => {
144150
beforeEach(waitForAsync(() => {
145151
init();
146152

153+
translateServiceSpy = jasmine.createSpyObj('TranslateService', ['get']);
154+
147155
void TestBed.configureTestingModule({
148156
declarations: [NgbCollapse],
149157
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), VarDirective, ProcessOverviewTableComponent],
@@ -228,50 +236,43 @@ describe('ProcessOverviewTableComponent', () => {
228236
});
229237

230238
});
231-
/*
232-
describe('getEPersonName', () => {
233-
beforeEach(() => {
234-
init();
235-
translateService = getMockTranslateService();
236-
});
237239

238-
it('should return the name when the ID is valid', () => {
239-
const id = 'valid_id';
240-
const expectedName = 'John Doe';
240+
describe('getEPersonName function', () => {
241+
it('should return unknown user when id is null', (done: DoneFn) => {
242+
const id = null;
243+
const expectedTranslation = 'process.overview.unknown.user';
241244

242-
spyOn(dsoNameService, 'getName').and.returnValue(expectedName);
245+
translateServiceSpy.get(expectedTranslation);
243246

244-
component.getEPersonName(id).subscribe(name => {
245-
expect(name).toEqual(expectedName);
247+
component.getEPersonName(id).subscribe((result: string) => {
248+
expect(result).toBe(expectedTranslation);
249+
done();
246250
});
247-
248-
expect(ePersonService.findById).toHaveBeenCalledWith(id);
251+
expect(translateServiceSpy.get).toHaveBeenCalledWith('process.overview.unknown.user');
249252
});
250253

251-
fit('should return "Unknown" when the ID is invalid', () => {
252-
const id = 'invalid_id';
253-
const translationKey = 'unknown_user';
254-
const expectedMessage = 'Unknown';
254+
it('should return unknown user when id is invalid', (done: DoneFn) => {
255+
const id = '';
256+
const expectedTranslation = 'process.overview.unknown.user';
255257

256-
spyOn(translateService, 'get').and.returnValue(of(expectedMessage));
258+
translateServiceSpy.get(expectedTranslation);
257259

258-
component.getEPersonName(id).subscribe(name => {
259-
expect(name).toEqual(expectedMessage);
260+
component.getEPersonName(id).subscribe((result: string) => {
261+
expect(result).toBe(expectedTranslation);
262+
done();
260263
});
261-
262-
expect(ePersonService.findById).toHaveBeenCalledWith(id);
263-
expect(translateService.get).toHaveBeenCalledWith(translationKey);
264+
expect(translateServiceSpy.get).toHaveBeenCalledWith('process.overview.unknown.user');
264265
});
265266

266-
it('should return an empty observable when the ID is null', () => {
267-
const id = null;
267+
it('should return EPerson name when id is correct', (done: DoneFn) => {
268+
const id = 'testid';
269+
const expectedName = 'John Doe';
268270

269-
component.getEPersonName(id).subscribe(name => {
270-
expect(name).toBeUndefined();
271+
component.getEPersonName(id).subscribe((result: string) => {
272+
expect(result).toEqual(expectedName);
273+
done();
271274
});
272-
273-
expect(ePersonService.findById).not.toHaveBeenCalled();
275+
expect(translateServiceSpy.get).not.toHaveBeenCalled();
274276
});
275277
});
276-
*/
277278
});

src/app/process-page/overview/table/process-overview-table.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ import { PaginationService } from 'src/app/core/pagination/pagination.service';
3838
import { AuthService } from '../../../core/auth/auth.service';
3939
import { DSONameService } from '../../../core/breadcrumbs/dso-name.service';
4040
import { FindListOptions } from '../../../core/data/find-list-options.model';
41+
import { Component, Input, OnInit, Inject, PLATFORM_ID, OnDestroy } from '@angular/core';
42+
import { ProcessStatus } from '../../processes/process-status.model';
43+
import { Observable, mergeMap, from as observableFrom, BehaviorSubject, Subscription, of } from 'rxjs';
44+
import { RemoteData } from '../../../core/data/remote-data';
4145
import { PaginatedList } from '../../../core/data/paginated-list.model';
4246
import { RemoteData } from '../../../core/data/remote-data';
4347
import { EPersonDataService } from '../../../core/eperson/eperson-data.service';
@@ -165,8 +169,8 @@ export class ProcessOverviewTableComponent implements OnInit, OnDestroy {
165169

166170
constructor(protected processOverviewService: ProcessOverviewService,
167171
protected processBulkDeleteService: ProcessBulkDeleteService,
168-
protected ePersonDataService: EPersonDataService,
169-
protected dsoNameService: DSONameService,
172+
public ePersonDataService: EPersonDataService,
173+
public dsoNameService: DSONameService,
170174
protected paginationService: PaginationService,
171175
protected routeService: RouteService,
172176
protected router: Router,
@@ -284,11 +288,11 @@ export class ProcessOverviewTableComponent implements OnInit, OnDestroy {
284288
getFirstCompletedRemoteData(),
285289
switchMap((rd: RemoteData<EPerson>) => {
286290
if (rd.hasSucceeded) {
287-
return observableFrom([this.dsoNameService.getName(rd.payload)]);
291+
return [this.dsoNameService.getName(rd.payload)];
288292
} else {
289293
return this.translateService.get('process.overview.unknown.user');
290294
}
291-
})
295+
}),
292296
);
293297
} else {
294298
return this.translateService.get('process.overview.unknown.user');

0 commit comments

Comments
 (0)