blob: 14f1b90f8f3aa99307a5b222b6c1c19368d5fead [file] [log] [blame]
/********************************************************************************
* 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 {RouterTestingModule} from "@angular/router/testing";
import {provideMockActions} from "@ngrx/effects/testing";
import {Action} from "@ngrx/store";
import {EMPTY, Observable, of, Subscription} from "rxjs";
import {SPA_BACKEND_ROUTE, TCompleteTaskVariable} from "../../../core";
import {IAPIProcessTask} from "../../../core/api/process";
import {
claimAndCompleteTask,
claimTaskAction,
completeTaskAction,
deleteTaskAction,
setProcessLoadingAction,
setTasksAction,
unclaimAllTasksAction
} from "../actions";
import {ProcessTaskEffect} from "./process-task.effect";
describe("ProcessTaskEffect", () => {
let actions$: Observable<Action>;
let httpTestingController: HttpTestingController;
let effect: ProcessTaskEffect;
let subscription: Subscription;
let router: Router;
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule
],
providers: [
provideMockActions(() => actions$),
{
provide: SPA_BACKEND_ROUTE,
useValue: "/"
}
]
});
effect = TestBed.inject(ProcessTaskEffect);
httpTestingController = TestBed.inject(HttpTestingController);
router = TestBed.inject(Router);
});
afterEach(() => {
if (subscription != null) {
subscription.unsubscribe();
}
});
it("should claim a task", async () => {
const statementId = 19;
const taskId = "191919";
const task: IAPIProcessTask = {...{} as IAPIProcessTask, taskId};
actions$ = of(claimTaskAction({statementId, taskId}));
const navigateSpy = spyOn(effect, "navigateTo").and.returnValue(EMPTY);
const expectedResult = [
setProcessLoadingAction({statementId, loading: true}),
deleteTaskAction({statementId}),
setTasksAction({statementId, tasks: [task]}),
setProcessLoadingAction({statementId, loading: false})
];
const results: Action[] = [];
subscription = effect.claim$.subscribe((action) => results.push(action));
expectClaimTask(statementId, taskId, task);
expectGetStatementTasks(statementId, [task]);
expect(results).toEqual(expectedResult);
expect(navigateSpy).toHaveBeenCalledWith(statementId, taskId);
httpTestingController.verify();
});
it("should claim and complete a task", async () => {
const statementId = 19;
const taskId = "191919";
const task: IAPIProcessTask = {...{} as IAPIProcessTask, taskId};
const variables = {};
actions$ = of(claimAndCompleteTask({statementId, taskId, variables, claimNext: true}));
const completeTaskSpy = spyOn(effect, "completeTask").and.returnValue(EMPTY);
const expectedResult = [
setProcessLoadingAction({statementId, loading: true}),
setProcessLoadingAction({statementId, loading: false})
];
const results: Action[] = [];
subscription = effect.claimAndComplete$.subscribe((action) => results.push(action));
expectClaimTask(statementId, taskId, task);
expect(results).toEqual(expectedResult);
expect(completeTaskSpy).toHaveBeenCalledWith(statementId, taskId, variables, true);
httpTestingController.verify();
});
it("should complete a task", async () => {
const statementId = 19;
const taskId = "191919";
const nextTaskId = "161616";
const variables = {};
const nextTask: IAPIProcessTask = {...{} as IAPIProcessTask, taskId: nextTaskId};
actions$ = of(completeTaskAction({statementId, taskId, variables, claimNext: true}));
const navigateSpy = spyOn(effect, "navigateTo").and.returnValue(EMPTY);
const expectedResult = [
setProcessLoadingAction({statementId, loading: true}),
deleteTaskAction({statementId}),
setTasksAction({statementId, tasks: [nextTask]}),
setProcessLoadingAction({statementId, loading: false})
];
const results: Action[] = [];
subscription = effect.completeTask$.subscribe((action) => results.push(action));
expectCompleteTask(statementId, taskId, variables);
expectGetStatementTasks(statementId, [nextTask]);
expectClaimTask(statementId, nextTaskId, nextTask);
expectGetStatementTasks(statementId, [nextTask]);
expect(results).toEqual(expectedResult);
expect(navigateSpy).toHaveBeenCalledWith(statementId, nextTaskId);
httpTestingController.verify();
});
it("should unclaim all tasks for a statement", async () => {
const statementId = 19;
const assignee = "assignee";
const tasks: IAPIProcessTask[] = [
{...{} as IAPIProcessTask, taskId: "19", assignee},
{...{} as IAPIProcessTask, taskId: "20"}
];
actions$ = of(unclaimAllTasksAction({statementId, assignee}));
const expectedResult = [
deleteTaskAction({statementId}),
setTasksAction({statementId, tasks}),
];
const results: Action[] = [];
subscription = effect.unclaimAll$.subscribe((action) => results.push(action));
expectGetStatementTasks(statementId, tasks);
expectUnclaimTask(statementId, tasks[0].taskId);
expectGetStatementTasks(statementId, tasks);
expect(results).toEqual(expectedResult);
httpTestingController.verify();
});
it("should navigate to details or edit page", async () => {
const statementId = 19;
const taskId = "19191919";
const navigateToSpy = spyOn(router, "navigate").and.returnValue(Promise.resolve(true));
await effect.navigateTo(statementId).toPromise();
expect(navigateToSpy).toHaveBeenCalledWith(["details"], {queryParams: {id: statementId}});
await effect.navigateTo(statementId, taskId).toPromise();
expect(navigateToSpy).toHaveBeenCalledWith(["edit"], {queryParams: {id: statementId, taskId}});
});
function expectCompleteTask(statementId: number, taskId: string, body: TCompleteTaskVariable) {
const url = `/process/statements/${statementId}/task/${taskId}/complete`;
const request = httpTestingController.expectOne(url);
expect(request.request.method).toBe("POST");
expect(request.request.body).toEqual(body);
request.flush({});
}
function expectGetStatementTasks(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);
}
function expectUnclaimTask(statementId: number, taskId: string) {
const url = `/process/statements/${statementId}/task/${taskId}/unclaim`;
const request = httpTestingController.expectOne(url);
expect(request.request.method).toBe("POST");
request.flush({statementId, taskId});
}
});