Skip to content

Commit 3fdef20

Browse files
committed
added unit testing
1 parent dbccda4 commit 3fdef20

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

src/app/process-page/detail/process-detail.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ <h2 class="flex-grow-1">
55
{{ 'process.detail.title' | translate:{ id: process?.processId, name: process?.scriptName } }}
66
</h2>
77
</div>
8-
<div *ngIf="refreshCounter$ | async as seconds" class="col-2">
8+
<div *ngIf="refreshCounter$ | async as seconds" class="col-2 refresh-counter">
99
Refreshing in {{ seconds }}s <i class="fas fa-sync-alt fa-spin"></i>
1010
</div>
1111
</div>

src/app/process-page/detail/process-detail.component.spec.ts

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { NotificationsServiceStub } from '../../shared/testing/notifications-ser
3535
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
3636
import { NotificationsService } from '../../shared/notifications/notifications.service';
3737
import { getProcessListRoute } from '../process-page-routing.paths';
38+
import {ProcessStatus} from '../processes/process-status.model';
3839

3940
describe('ProcessDetailComponent', () => {
4041
let component: ProcessDetailComponent;
@@ -44,6 +45,7 @@ describe('ProcessDetailComponent', () => {
4445
let nameService: DSONameService;
4546
let bitstreamDataService: BitstreamDataService;
4647
let httpClient: HttpClient;
48+
let route: ActivatedRoute;
4749

4850
let process: Process;
4951
let fileName: string;
@@ -106,7 +108,8 @@ describe('ProcessDetailComponent', () => {
106108
});
107109
processService = jasmine.createSpyObj('processService', {
108110
getFiles: createSuccessfulRemoteDataObject$(createPaginatedList(files)),
109-
delete: createSuccessfulRemoteDataObject$(null)
111+
delete: createSuccessfulRemoteDataObject$(null),
112+
findById: createSuccessfulRemoteDataObject$(process),
110113
});
111114
bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', {
112115
findByHref: createSuccessfulRemoteDataObject$(logBitstream)
@@ -127,6 +130,13 @@ describe('ProcessDetailComponent', () => {
127130
router = jasmine.createSpyObj('router', {
128131
navigateByUrl:{}
129132
});
133+
134+
route = jasmine.createSpyObj('route', {
135+
data: observableOf({ process: createSuccessfulRemoteDataObject(process) }),
136+
snapshot: {
137+
params: { id: process.processId }
138+
}
139+
});
130140
}
131141

132142
beforeEach(waitForAsync(() => {
@@ -263,4 +273,92 @@ describe('ProcessDetailComponent', () => {
263273
});
264274
});
265275

276+
describe('refresh counter', () => {
277+
const queryRefreshCounter = () => fixture.debugElement.query(By.css('.refresh-counter'));
278+
279+
describe('if process is completed', () => {
280+
beforeEach(() => {
281+
process.processStatus = ProcessStatus.COMPLETED;
282+
route.data = observableOf({process: createSuccessfulRemoteDataObject(process)});
283+
});
284+
285+
it('should not show', () => {
286+
spyOn(component, 'startRefreshTimer');
287+
288+
const refreshCounter = queryRefreshCounter();
289+
expect(refreshCounter).toBeNull();
290+
291+
expect(component.startRefreshTimer).not.toHaveBeenCalled();
292+
});
293+
});
294+
295+
describe('if process is not finished', () => {
296+
beforeEach(() => {
297+
process.processStatus = ProcessStatus.RUNNING;
298+
route.data = observableOf({process: createSuccessfulRemoteDataObject(process)});
299+
fixture.detectChanges();
300+
component.stopRefreshTimer();
301+
});
302+
303+
it('should call startRefreshTimer', () => {
304+
spyOn(component, 'startRefreshTimer');
305+
306+
component.ngOnInit();
307+
fixture.detectChanges(); // subscribe to process observable with async pipe
308+
309+
expect(component.startRefreshTimer).toHaveBeenCalled();
310+
});
311+
312+
it('should call refresh method every 5 seconds, until process is completed', fakeAsync(() => {
313+
spyOn(component, 'refresh');
314+
spyOn(component, 'stopRefreshTimer');
315+
316+
process.processStatus = ProcessStatus.COMPLETED;
317+
// set findbyId to return a completed process
318+
(processService.findById as jasmine.Spy).and.returnValue(observableOf(createSuccessfulRemoteDataObject(process)));
319+
320+
component.ngOnInit();
321+
fixture.detectChanges(); // subscribe to process observable with async pipe
322+
323+
expect(component.refresh).not.toHaveBeenCalled();
324+
325+
expect(component.refreshCounter$.value).toBe(0);
326+
327+
tick(1001); // 1 second + 1 ms by the setTimeout
328+
expect(component.refreshCounter$.value).toBe(5); // 5 - 0
329+
330+
tick(2001); // 2 seconds + 1 ms by the setTimeout
331+
expect(component.refreshCounter$.value).toBe(3); // 5 - 2
332+
333+
tick(2001); // 2 seconds + 1 ms by the setTimeout
334+
expect(component.refreshCounter$.value).toBe(1); // 3 - 2
335+
336+
tick(1001); // 1 second + 1 ms by the setTimeout
337+
expect(component.refreshCounter$.value).toBe(0); // 1 - 1
338+
339+
tick(1000); // 1 second
340+
341+
expect(component.refresh).toHaveBeenCalledTimes(1);
342+
expect(component.stopRefreshTimer).toHaveBeenCalled();
343+
344+
expect(component.refreshCounter$.value).toBe(0);
345+
346+
tick(1001); // 1 second + 1 ms by the setTimeout
347+
// startRefreshTimer not called again
348+
expect(component.refreshCounter$.value).toBe(0);
349+
350+
discardPeriodicTasks(); // discard any periodic tasks that have not yet executed
351+
}));
352+
353+
it('should show if refreshCounter is different from 0', () => {
354+
component.refreshCounter$.next(1);
355+
fixture.detectChanges();
356+
357+
const refreshCounter = queryRefreshCounter();
358+
expect(refreshCounter).not.toBeNull();
359+
});
360+
361+
});
362+
363+
});
266364
});

0 commit comments

Comments
 (0)