finished tests

This commit is contained in:
lotte
2020-04-08 15:41:55 +02:00
parent d003400c16
commit e43f04e564
18 changed files with 383 additions and 23 deletions

View File

@@ -0,0 +1,124 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
import { WorkflowItemActionPageComponent } from './workflow-item-action-page.component';
import { MockTranslateLoader } from '../shared/mocks/mock-translate-loader';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../shared/testing/utils';
import { ActivatedRouteStub } from '../shared/testing/active-router-stub';
import { NotificationsServiceStub } from '../shared/testing/notifications-service-stub';
import { NotificationsService } from '../shared/notifications/notifications.service';
import { RouteService } from '../core/services/route.service';
import { RouterStub } from '../shared/testing/router-stub';
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
import { WorkflowItemDataService } from '../core/submission/workflowitem-data.service';
import { ActivatedRoute, Router } from '@angular/router';
import { WorkflowItem } from '../core/submission/models/workflowitem.model';
import { Observable, of as observableOf } from 'rxjs';
import { VarDirective } from '../shared/utils/var.directive';
import { By } from '@angular/platform-browser';
const type = 'testType';
describe('WorkflowItemActionPageComponent', () => {
let component: WorkflowItemActionPageComponent;
let fixture: ComponentFixture<WorkflowItemActionPageComponent>;
let wfiService;
let wfi;
let itemRD$;
let id;
function init() {
wfiService = jasmine.createSpyObj('workflowItemService', {
sendBack: observableOf(true)
});
itemRD$ = createSuccessfulRemoteDataObject$(itemRD$);
wfi = new WorkflowItem();
wfi.item = itemRD$;
id = 'de11b5e5-064a-4e98-a7ac-a1a6a65ddf80';
}
beforeEach(async(() => {
init();
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: MockTranslateLoader
}
})],
declarations: [TestComponent, VarDirective],
providers: [
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub({}, { wfi: createSuccessfulRemoteDataObject(wfi) }) },
{ provide: Router, useClass: RouterStub },
{ provide: RouteService, useValue: {} },
{ provide: NotificationsService, useClass: NotificationsServiceStub },
{ provide: WorkflowItemDataService, useValue: wfiService },
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should set the initial type correctly', () => {
expect(component.type).toEqual(type);
});
describe('clicking the button with class btn-danger', () => {
beforeEach(() => {
spyOn(component, 'performAction');
});
it('should call performAction on clicking the btn-danger', () => {
const button = fixture.debugElement.query(By.css('.btn-danger')).nativeElement;
button.click();
fixture.detectChanges();
expect(component.performAction).toHaveBeenCalled();
});
});
describe('clicking the button with class btn-default', () => {
beforeEach(() => {
spyOn(component, 'previousPage');
});
it('should call performAction on clicking the btn-default', () => {
const button = fixture.debugElement.query(By.css('.btn-default')).nativeElement;
button.click();
fixture.detectChanges();
expect(component.previousPage).toHaveBeenCalled();
});
});
});
@Component({
selector: 'ds-workflow-item-test-action-page',
templateUrl: 'workflow-item-action-page.component.html'
}
)
class TestComponent extends WorkflowItemActionPageComponent {
constructor(protected route: ActivatedRoute,
protected workflowItemService: WorkflowItemDataService,
protected router: Router,
protected routeService: RouteService,
protected notificationsService: NotificationsService,
protected translationService: TranslateService) {
super(route, workflowItemService, router, routeService, notificationsService, translationService);
}
getType(): string {
return type;
}
sendRequest(id: string): Observable<boolean> {
return observableOf(true);
}
}

View File

@@ -12,6 +12,9 @@ import { RemoteData } from '../core/data/remote-data';
import { getAllSucceededRemoteData, getRemoteDataPayload } from '../core/shared/operators';
import { isEmpty } from '../shared/empty.util';
/**
* Abstract component representing a page to perform an action on a workflow item
*/
export abstract class WorkflowItemActionPageComponent implements OnInit {
public type;
public wfi$: Observable<WorkflowItem>;
@@ -25,12 +28,18 @@ export abstract class WorkflowItemActionPageComponent implements OnInit {
protected translationService: TranslateService) {
}
/**
* Sets up the type, workflow item and its item object
*/
ngOnInit() {
this.type = this.getType();
this.wfi$ = this.route.data.pipe(map((data: Data) => data.wfi as RemoteData<WorkflowItem>), getRemoteDataPayload());
this.item$ = this.wfi$.pipe(switchMap((wfi: WorkflowItem) => (wfi.item as Observable<RemoteData<Item>>).pipe(getAllSucceededRemoteData(), getRemoteDataPayload())));
}
/**
* Performs the action and shows a notification based on the outcome of the action
*/
performAction() {
this.wfi$.pipe(
take(1),
@@ -49,6 +58,10 @@ export abstract class WorkflowItemActionPageComponent implements OnInit {
})
}
/**
* Navigates to the previous url
* If there's not previous url, it continues to the mydspace page instead
*/
previousPage() {
this.routeService.getPreviousUrl().pipe(take(1))
.subscribe((url) => {
@@ -56,10 +69,18 @@ export abstract class WorkflowItemActionPageComponent implements OnInit {
url = '/mydspace';
}
this.router.navigateByUrl(url);
}
);
}
/**
* Performs the action of this workflow item action page
* @param id The id of the WorkflowItem
*/
abstract sendRequest(id: string): Observable<boolean>;
/**
* Returns the type of page
*/
abstract getType(): string;
}

View File

@@ -1,16 +1,59 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { WorkflowItemDeleteComponent } from './workflow-item-delete.component';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { MockTranslateLoader } from '../../shared/mocks/mock-translate-loader';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterStub } from '../../shared/testing/router-stub';
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
import { RouteService } from '../../core/services/route.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { WorkflowItemDataService } from '../../core/submission/workflowitem-data.service';
import { NotificationsServiceStub } from '../../shared/testing/notifications-service-stub';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/testing/utils';
import { WorkflowItem } from '../../core/submission/models/workflowitem.model';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { VarDirective } from '../../shared/utils/var.directive';
import { of as observableOf } from 'rxjs';
describe('WorkflowItemDeleteComponent', () => {
let component: WorkflowItemDeleteComponent;
let fixture: ComponentFixture<WorkflowItemDeleteComponent>;
let wfiService;
let wfi;
let itemRD$;
let id;
function init() {
wfiService = jasmine.createSpyObj('workflowItemService', {
delete: observableOf(true)
});
itemRD$ = createSuccessfulRemoteDataObject$(itemRD$);
wfi = new WorkflowItem();
wfi.item = itemRD$;
id = 'de11b5e5-064a-4e98-a7ac-a1a6a65ddf80';
}
beforeEach(async(() => {
init();
TestBed.configureTestingModule({
declarations: [ WorkflowItemDeleteComponent ]
imports: [TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: MockTranslateLoader
}
})],
declarations: [WorkflowItemDeleteComponent, VarDirective],
providers: [
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub({}, { wfi: createSuccessfulRemoteDataObject(wfi) }) },
{ provide: Router, useClass: RouterStub },
{ provide: RouteService, useValue: {} },
{ provide: NotificationsService, useClass: NotificationsServiceStub },
{ provide: WorkflowItemDataService, useValue: wfiService },
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
.compileComponents();
}));
beforeEach(() => {
@@ -22,4 +65,9 @@ describe('WorkflowItemDeleteComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call delete on the workflow-item service when sendRequest is called', () => {
component.sendRequest(id);
expect(wfiService.delete).toHaveBeenCalledWith(id);
});
});

View File

@@ -11,6 +11,9 @@ import { TranslateService } from '@ngx-translate/core';
selector: 'ds-workflow-item-delete',
templateUrl: '../workflow-item-action-page.component.html'
})
/**
* Component representing a page to delete a workflow item
*/
export class WorkflowItemDeleteComponent extends WorkflowItemActionPageComponent {
constructor(protected route: ActivatedRoute,
protected workflowItemService: WorkflowItemDataService,
@@ -21,10 +24,17 @@ export class WorkflowItemDeleteComponent extends WorkflowItemActionPageComponent
super(route, workflowItemService, router, routeService, notificationsService, translationService);
}
/**
* Returns the type of page
*/
getType(): string {
return 'delete';
}
/**
* Performs the action of this workflow item action page
* @param id The id of the WorkflowItem
*/
sendRequest(id: string): Observable<boolean> {
return this.workflowItemService.delete(id);
}

View File

@@ -0,0 +1,29 @@
import { first } from 'rxjs/operators';
import { of as observableOf } from 'rxjs';
import { WorkflowItemPageResolver } from './workflow-item-page.resolver';
import { WorkflowItemDataService } from '../core/submission/workflowitem-data.service';
describe('WorkflowItemPageResolver', () => {
describe('resolve', () => {
let resolver: WorkflowItemPageResolver;
let wfiService: WorkflowItemDataService;
const uuid = '1234-65487-12354-1235';
beforeEach(() => {
wfiService = {
findById: (id: string) => observableOf({ payload: { id }, hasSucceeded: true }) as any
} as any;
resolver = new WorkflowItemPageResolver(wfiService);
});
it('should resolve a workflow item with the correct id', () => {
resolver.resolve({ params: { id: uuid } } as any, undefined)
.pipe(first())
.subscribe(
(resolved) => {
expect(resolved.payload.id).toEqual(uuid);
}
);
});
});
});

View File

@@ -2,8 +2,6 @@ import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { RemoteData } from '../core/data/remote-data';
import { ItemDataService } from '../core/data/item-data.service';
import { Item } from '../core/shared/item.model';
import { hasValue } from '../shared/empty.util';
import { find } from 'rxjs/operators';
import { followLink } from '../shared/utils/follow-link-config.model';
@@ -11,7 +9,7 @@ import { WorkflowItemDataService } from '../core/submission/workflowitem-data.se
import { WorkflowItem } from '../core/submission/models/workflowitem.model';
/**
* This class represents a resolver that requests a specific item before the route is activated
* This class represents a resolver that requests a specific workflow item before the route is activated
*/
@Injectable()
export class WorkflowItemPageResolver implements Resolve<RemoteData<WorkflowItem>> {
@@ -19,10 +17,10 @@ export class WorkflowItemPageResolver implements Resolve<RemoteData<WorkflowItem
}
/**
* Method for resolving an item based on the parameters in the current route
* Method for resolving a workflow item based on the parameters in the current route
* @param {ActivatedRouteSnapshot} route The current ActivatedRouteSnapshot
* @param {RouterStateSnapshot} state The current RouterStateSnapshot
* @returns Observable<<RemoteData<Item>> Emits the found item based on the parameters in the current route,
* @returns Observable<<RemoteData<Item>> Emits the found workflow item based on the parameters in the current route,
* or an error if something went wrong
*/
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<RemoteData<WorkflowItem>> {

View File

@@ -1,16 +1,59 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { WorkflowItemSendBackComponent } from './workflow-item-send-back.component';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { MockTranslateLoader } from '../../shared/mocks/mock-translate-loader';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterStub } from '../../shared/testing/router-stub';
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
import { RouteService } from '../../core/services/route.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { WorkflowItemDataService } from '../../core/submission/workflowitem-data.service';
import { NotificationsServiceStub } from '../../shared/testing/notifications-service-stub';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/testing/utils';
import { WorkflowItem } from '../../core/submission/models/workflowitem.model';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { VarDirective } from '../../shared/utils/var.directive';
import { of as observableOf } from 'rxjs';
import { WorkflowItemSendBackComponent } from './workflow-item-send-back.component';
describe('WorkflowItemSendBackComponent', () => {
let component: WorkflowItemSendBackComponent;
let fixture: ComponentFixture<WorkflowItemSendBackComponent>;
let wfiService;
let wfi;
let itemRD$;
let id;
function init() {
wfiService = jasmine.createSpyObj('workflowItemService', {
sendBack: observableOf(true)
});
itemRD$ = createSuccessfulRemoteDataObject$(itemRD$);
wfi = new WorkflowItem();
wfi.item = itemRD$;
id = 'de11b5e5-064a-4e98-a7ac-a1a6a65ddf80';
}
beforeEach(async(() => {
init();
TestBed.configureTestingModule({
declarations: [ WorkflowItemSendBackComponent ]
imports: [TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: MockTranslateLoader
}
})],
declarations: [WorkflowItemSendBackComponent, VarDirective],
providers: [
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub({}, { wfi: createSuccessfulRemoteDataObject(wfi) }) },
{ provide: Router, useClass: RouterStub },
{ provide: RouteService, useValue: {} },
{ provide: NotificationsService, useClass: NotificationsServiceStub },
{ provide: WorkflowItemDataService, useValue: wfiService },
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
.compileComponents();
}));
beforeEach(() => {
@@ -22,4 +65,9 @@ describe('WorkflowItemSendBackComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call sendBack on the workflow-item service when sendRequest is called', () => {
component.sendRequest(id);
expect(wfiService.sendBack).toHaveBeenCalledWith(id);
});
});

View File

@@ -11,6 +11,9 @@ import { TranslateService } from '@ngx-translate/core';
selector: 'ds-workflow-item-send-back',
templateUrl: '../workflow-item-action-page.component.html'
})
/**
* Component representing a page to send back a workflow item to the submitter
*/
export class WorkflowItemSendBackComponent extends WorkflowItemActionPageComponent {
constructor(protected route: ActivatedRoute,
protected workflowItemService: WorkflowItemDataService,
@@ -21,10 +24,17 @@ export class WorkflowItemSendBackComponent extends WorkflowItemActionPageCompone
super(route, workflowItemService, router, routeService, notificationsService, translationService);
}
/**
* Returns the type of page
*/
getType(): string {
return 'send-back';
}
/**
* Performs the action of this workflow item action page
* @param id The id of the WorkflowItem
*/
sendRequest(id: string): Observable<boolean> {
return this.workflowItemService.sendBack(id);
}