Commits

Kuno Vercammen authored fda45723cc2
113904: Added tests for the getEPersonName function
No tags

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

Modified
49 49 let fixture: ComponentFixture<ProcessOverviewTableComponent>;
50 50
51 51 let processOverviewService: ProcessOverviewService;
52 52 let processService: ProcessDataService;
53 53 let ePersonService: EPersonDataService;
54 54 let paginationService; // : PaginationService; Not typed as the stub does not fully implement PaginationService
55 55 let processBulkDeleteService: ProcessBulkDeleteService;
56 56 let modalService: NgbModal;
57 57 let authService; // : AuthService; Not typed as the mock does not fully implement AuthService
58 58 let routeService: RouteService;
59 - let translateService: TranslateService;
60 59
61 60 let processes: Process[];
62 61 let ePerson: EPerson;
63 62
63 + let translateServiceSpy: jasmine.SpyObj<TranslateService>;
64 +
64 65 function init() {
65 66 processes = [
66 67 Object.assign(new Process(), {
67 68 processId: 1,
68 69 scriptName: 'script-a',
69 70 startTime: '2020-03-19 00:30:00',
70 71 endTime: '2020-03-19 23:30:00',
71 72 processStatus: ProcessStatus.COMPLETED,
73 + userId: 'testid',
72 74 }),
73 75 Object.assign(new Process(), {
74 76 processId: 2,
75 77 scriptName: 'script-b',
76 78 startTime: '2020-03-20 00:30:00',
77 79 endTime: '2020-03-20 23:30:00',
78 80 processStatus: ProcessStatus.FAILED,
81 + userId: 'testid',
79 82 }),
80 83 Object.assign(new Process(), {
81 84 processId: 3,
82 85 scriptName: 'script-c',
83 86 startTime: '2020-03-21 00:30:00',
84 87 endTime: '2020-03-21 23:30:00',
85 88 processStatus: ProcessStatus.RUNNING,
89 + userId: 'testid',
86 90 }),
87 91 ];
88 92 ePerson = Object.assign(new EPerson(), {
93 + id: 'testid',
94 + uuid: 'testid',
89 95 metadata: {
90 96 'eperson.firstname': [
91 97 {
92 98 value: 'John',
93 99 language: null,
94 100 },
95 101 ],
96 102 'eperson.lastname': [
97 103 {
98 104 value: 'Doe',
137 143 open: {},
138 144 });
139 145
140 146 authService = new AuthServiceMock();
141 147 routeService = routeServiceStub;
142 148 }
143 149
144 150 beforeEach(waitForAsync(() => {
145 151 init();
146 152
153 + translateServiceSpy = jasmine.createSpyObj('TranslateService', ['get']);
154 +
147 155 void TestBed.configureTestingModule({
148 156 declarations: [NgbCollapse],
149 157 imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), VarDirective, ProcessOverviewTableComponent],
150 158 providers: [
151 159 { provide: ProcessOverviewService, useValue: processOverviewService },
152 160 { provide: ProcessDataService, useValue: processService },
153 161 { provide: EPersonDataService, useValue: ePersonService },
154 162 { provide: PaginationService, useValue: paginationService },
155 163 { provide: ProcessBulkDeleteService, useValue: processBulkDeleteService },
156 164 { provide: NgbModal, useValue: modalService },
221 229 expect(processBulkDeleteService.toggleDelete).toHaveBeenCalledWith(processes[index].processId);
222 230 });
223 231 });
224 232
225 233 it('should indicate a row that has been selected for deletion', () => {
226 234 const deleteRow = fixture.debugElement.query(By.css('.table-danger'));
227 235 expect(deleteRow.nativeElement.innerHTML).toContain('/processes/' + processes[1].processId);
228 236 });
229 237
230 238 });
231 -/*
232 - describe('getEPersonName', () => {
233 - beforeEach(() => {
234 - init();
235 - translateService = getMockTranslateService();
236 - });
237 239
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';
241 244
242 - spyOn(dsoNameService, 'getName').and.returnValue(expectedName);
245 + translateServiceSpy.get(expectedTranslation);
243 246
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();
246 250 });
247 -
248 - expect(ePersonService.findById).toHaveBeenCalledWith(id);
251 + expect(translateServiceSpy.get).toHaveBeenCalledWith('process.overview.unknown.user');
249 252 });
250 253
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';
255 257
256 - spyOn(translateService, 'get').and.returnValue(of(expectedMessage));
258 + translateServiceSpy.get(expectedTranslation);
257 259
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();
260 263 });
261 -
262 - expect(ePersonService.findById).toHaveBeenCalledWith(id);
263 - expect(translateService.get).toHaveBeenCalledWith(translationKey);
264 + expect(translateServiceSpy.get).toHaveBeenCalledWith('process.overview.unknown.user');
264 265 });
265 266
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';
268 270
269 - component.getEPersonName(id).subscribe(name => {
270 - expect(name).toBeUndefined();
271 + component.getEPersonName(id).subscribe((result: string) => {
272 + expect(result).toEqual(expectedName);
273 + done();
271 274 });
272 -
273 - expect(ePersonService.findById).not.toHaveBeenCalled();
275 + expect(translateServiceSpy.get).not.toHaveBeenCalled();
274 276 });
275 277 });
276 -*/
277 278 });

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut