blob: beb7779921525247a3adea1f46b1bcb1989dd534 [file]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
import {TestBed} from "@angular/core/testing";
import {Router} from "@angular/router";
import {provideMockActions} from "@ngrx/effects/testing";
import {Action} from "@ngrx/store";
import {Observable, of, Subscription, timer} from "rxjs";
import {SPA_BACKEND_ROUTE} from "../../../core";
import {EAPIProcessTaskDefinitionKey, IAPIProcessTask} from "../../../core/api/process";
import {completeTaskAction, deleteTaskAction} from "../actions";
import {CompleteTaskEffect} from "./complete-task.effect";
describe("CompleteTaskEffect", () => {
let actions$: Observable<Action>;
let httpTestingController: HttpTestingController;
let effect: CompleteTaskEffect;
let subscription: Subscription;
const routerSpy = {navigate: jasmine.createSpy("navigate")};
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
CompleteTaskEffect,
provideMockActions(() => actions$),
{
provide: SPA_BACKEND_ROUTE,
useValue: "/"
},
{provide: Router, useValue: routerSpy}
]
});
effect = TestBed.inject(CompleteTaskEffect);
httpTestingController = TestBed.inject(HttpTestingController);
});
afterEach(() => {
if (subscription != null) {
subscription.unsubscribe();
}
});
it("should claim the task and dispatch the updateTaskAction", async () => {
const statementId = 19;
const taskId = "taskId";
const expectedResult = [deleteTaskAction({statementId, taskId})];
const results: Action[] = [];
actions$ = of(completeTaskAction({statementId, taskId, variables: {}}));
subscription = effect.completeTask$.subscribe((action) => results.push(action));
expectCompleteTask(statementId, taskId);
await timer(500).toPromise();
expect(results).toEqual(expectedResult);
httpTestingController.verify();
});
it("should complete the task and afterwards claim it", async () => {
const statementId = 19;
const taskId = "taskId";
const expectedResult = [deleteTaskAction({statementId, taskId})];
const results: Action[] = [];
actions$ = of(completeTaskAction({statementId, taskId, variables: {}}));
subscription = effect.completeTask$.subscribe((action) => results.push(action));
expectCompleteTask(statementId, taskId);
await timer(500).toPromise();
expect(results).toEqual(expectedResult);
httpTestingController.verify();
});
it("should claim the next task", async () => {
const statementId = 19;
const taskId = "taskId";
const fetchResult: IAPIProcessTask[] = [
{
statementId: 19,
taskId: "taskId",
taskDefinitionKey: EAPIProcessTaskDefinitionKey.ADD_BASIC_INFO_DATA,
processDefinitionKey: "string",
assignee: "string",
requiredVariables: {
var1: "string"
}
}
];
const expectedResult = [deleteTaskAction({statementId, taskId})];
const results: Action[] = [];
actions$ = of(completeTaskAction({statementId, taskId, variables: {}, claimNext: true}));
subscription = effect.completeTask$.subscribe((action) => results.push(action));
expectCompleteTask(statementId, taskId);
expectGetStatement(statementId, fetchResult);
expectClaimTask(statementId, taskId, fetchResult[0]);
await timer(500).toPromise();
expect(results).toEqual(expectedResult);
httpTestingController.verify();
});
it("should follow up with the actions supplied in endWith", async () => {
const statementId = 19;
const taskId = "taskId";
const expectedResult = [
deleteTaskAction({statementId, taskId}),
deleteTaskAction({statementId, taskId})
];
const results: Action[] = [];
actions$ = of(completeTaskAction({statementId, taskId, variables: {}, endWith: [deleteTaskAction({statementId, taskId})]}));
subscription = effect.completeTask$.subscribe((action) => results.push(action));
expectCompleteTask(statementId, taskId);
await timer(500).toPromise();
expect(results).toEqual(expectedResult);
httpTestingController.verify();
});
function expectCompleteTask(statementId: number, taskId: string) {
const url = `/process/statements/${statementId}/task/${taskId}/complete`;
const request = httpTestingController.expectOne(url);
expect(request.request.method).toBe("POST");
request.flush({});
}
function expectGetStatement(statementId: number, returnValue: IAPIProcessTask[]) {
const url = `/process/statements/${statementId}/task`;
const request = httpTestingController.expectOne(url);
expect(request.request.method).toBe("GET");
request.flush(returnValue);
}
function expectClaimTask(statementId: number, taskId: string, returnValue: IAPIProcessTask) {
const url = `/process/statements/${statementId}/task/${taskId}/claim`;
const request = httpTestingController.expectOne(url);
expect(request.request.method).toBe("POST");
request.flush(returnValue);
}
});