From 788b39532722ce5a20689742e4ad3a166aedfdd8 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:35:33 -0800 Subject: [PATCH] Complete frontend coverage --- .../workflow-edit-dialog.component.spec.ts | 72 ++++++++++++++++++- .../workflow-edit-dialog.component.ts | 2 +- .../rest/workflow-action.service.spec.ts | 48 ------------- .../services/rest/workflow-action.service.ts | 43 ----------- .../rest/workflow-trigger.service.spec.ts | 57 --------------- .../services/rest/workflow-trigger.service.ts | 42 ----------- 6 files changed, 72 insertions(+), 192 deletions(-) delete mode 100644 src-ui/src/app/services/rest/workflow-action.service.spec.ts delete mode 100644 src-ui/src/app/services/rest/workflow-action.service.ts delete mode 100644 src-ui/src/app/services/rest/workflow-trigger.service.spec.ts delete mode 100644 src-ui/src/app/services/rest/workflow-trigger.service.ts diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts index 5c0d06d83..c152e3659 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts @@ -20,8 +20,44 @@ import { TagsComponent } from '../../input/tags/tags.component' import { TextComponent } from '../../input/text/text.component' import { SwitchComponent } from '../../input/switch/switch.component' import { EditDialogMode } from '../edit-dialog.component' -import { WorkflowEditDialogComponent } from './workflow-edit-dialog.component' +import { + DOCUMENT_SOURCE_OPTIONS, + WORKFLOW_TYPE_OPTIONS, + WorkflowEditDialogComponent, +} from './workflow-edit-dialog.component' import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service' +import { Workflow } from 'src/app/data/workflow' +import { + WorkflowTriggerType, + DocumentSource, +} from 'src/app/data/workflow-trigger' +import { CdkDragDrop } from '@angular/cdk/drag-drop' +import { WorkflowAction } from 'src/app/data/workflow-action' + +const workflow: Workflow = { + name: 'Workflow 1', + id: 1, + order: 1, + enabled: true, + triggers: [ + { + id: 1, + type: WorkflowTriggerType.Consumption, + sources: [DocumentSource.ConsumeFolder], + filter_filename: '*', + }, + ], + actions: [ + { + id: 1, + assign_title: 'foo', + }, + { + id: 4, + assign_owner: 2, + }, + ], +} describe('ConsumptionTemplateEditDialogComponent', () => { let component: WorkflowEditDialogComponent @@ -134,4 +170,38 @@ describe('ConsumptionTemplateEditDialogComponent', () => { fixture.detectChanges() expect(editTitleSpy).toHaveBeenCalled() }) + + it('should return source options, type options, type name', () => { + // coverage + expect(component.sourceOptions).toEqual(DOCUMENT_SOURCE_OPTIONS) + expect(component.typeOptions).toEqual(WORKFLOW_TYPE_OPTIONS) + expect( + component.getTypeOptionName(WorkflowTriggerType.DocumentAdded) + ).toEqual('Document Added') + expect(component.getTypeOptionName(null)).toEqual('') + }) + + it('should support add and remove triggers and actions', () => { + component.object = workflow + component.addTrigger() + expect(component.object.triggers.length).toEqual(2) + component.addAction() + expect(component.object.actions.length).toEqual(3) + component.removeTrigger(1) + expect(component.object.triggers.length).toEqual(1) + component.removeAction(1) + expect(component.object.actions.length).toEqual(2) + }) + + it('should update order and remove ids from actions on drag n drop', () => { + const action1 = workflow.actions[0] + const action2 = workflow.actions[1] + component.object = workflow + component.onActionDrop({ previousIndex: 0, currentIndex: 1 } as CdkDragDrop< + WorkflowAction[] + >) + expect(component.object.actions).toEqual([action2, action1]) + expect(action1.id).toBeNull() + expect(action2.id).toBeNull() + }) }) diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts index 1f07532ba..765d5a3e5 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -196,7 +196,7 @@ export class WorkflowEditDialogComponent } getTypeOptionName(type: WorkflowTriggerType): string { - return this.typeOptions.find((t) => t.id === type).name ?? '' + return this.typeOptions.find((t) => t.id === type)?.name ?? '' } addTrigger() { diff --git a/src-ui/src/app/services/rest/workflow-action.service.spec.ts b/src-ui/src/app/services/rest/workflow-action.service.spec.ts deleted file mode 100644 index 817ec1584..000000000 --- a/src-ui/src/app/services/rest/workflow-action.service.spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { HttpTestingController } from '@angular/common/http/testing' -import { TestBed } from '@angular/core/testing' -import { Subscription } from 'rxjs' -import { environment } from 'src/environments/environment' -import { commonAbstractPaperlessServiceTests } from './abstract-paperless-service.spec' -import { WorkflowActionService } from './workflow-action.service' -import { WorkflowAction } from 'src/app/data/workflow-action' - -let httpTestingController: HttpTestingController -let service: WorkflowActionService -const endpoint = 'workflow_actions' -const actions: WorkflowAction[] = [ - { - id: 1, - assign_correspondent: 2, - }, - { - id: 2, - assign_document_type: 1, - }, -] - -// run common tests -commonAbstractPaperlessServiceTests(endpoint, WorkflowActionService) - -describe(`Additional service tests for WorkflowActionService`, () => { - it('should reload', () => { - service.reload() - const req = httpTestingController.expectOne( - `${environment.apiBaseUrl}${endpoint}/?page=1&page_size=100000` - ) - req.flush({ - results: actions, - }) - expect(service.allActions).toEqual(actions) - }) - - beforeEach(() => { - // Dont need to setup again - - httpTestingController = TestBed.inject(HttpTestingController) - service = TestBed.inject(WorkflowActionService) - }) - - afterEach(() => { - httpTestingController.verify() - }) -}) diff --git a/src-ui/src/app/services/rest/workflow-action.service.ts b/src-ui/src/app/services/rest/workflow-action.service.ts deleted file mode 100644 index c462f60a8..000000000 --- a/src-ui/src/app/services/rest/workflow-action.service.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { HttpClient } from '@angular/common/http' -import { Injectable } from '@angular/core' -import { tap } from 'rxjs' -import { Workflow } from 'src/app/data/workflow' -import { AbstractPaperlessService } from './abstract-paperless-service' -import { WorkflowAction } from 'src/app/data/workflow-action' - -@Injectable({ - providedIn: 'root', -}) -export class WorkflowActionService extends AbstractPaperlessService { - loading: boolean - - constructor(http: HttpClient) { - super(http, 'workflow_actions') - } - - public reload() { - this.loading = true - this.listAll().subscribe((r) => { - this.actions = r.results - this.loading = false - }) - } - - private actions: WorkflowAction[] = [] - - public get allActions(): WorkflowAction[] { - return this.actions - } - - create(o: WorkflowAction) { - return super.create(o).pipe(tap(() => this.reload())) - } - - update(o: WorkflowAction) { - return super.update(o).pipe(tap(() => this.reload())) - } - - delete(o: WorkflowAction) { - return super.delete(o).pipe(tap(() => this.reload())) - } -} diff --git a/src-ui/src/app/services/rest/workflow-trigger.service.spec.ts b/src-ui/src/app/services/rest/workflow-trigger.service.spec.ts deleted file mode 100644 index 01d72e1cc..000000000 --- a/src-ui/src/app/services/rest/workflow-trigger.service.spec.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { HttpTestingController } from '@angular/common/http/testing' -import { TestBed } from '@angular/core/testing' -import { environment } from 'src/environments/environment' -import { commonAbstractPaperlessServiceTests } from './abstract-paperless-service.spec' -import { WorkflowTriggerService } from './workflow-trigger.service' -import { - DocumentSource, - WorkflowTrigger, - WorkflowTriggerType, -} from 'src/app/data/workflow-trigger' - -let httpTestingController: HttpTestingController -let service: WorkflowTriggerService -const endpoint = 'workflow_triggers' -const triggers: WorkflowTrigger[] = [ - { - id: 1, - type: WorkflowTriggerType.Consumption, - filter_filename: '*test*', - filter_path: null, - sources: [DocumentSource.ApiUpload], - }, - { - id: 2, - type: WorkflowTriggerType.DocumentAdded, - filter_filename: null, - filter_path: '/test/', - sources: [DocumentSource.ConsumeFolder, DocumentSource.ApiUpload], - }, -] - -// run common tests -commonAbstractPaperlessServiceTests(endpoint, WorkflowTriggerService) - -describe(`Additional service tests for WorkflowTriggerService`, () => { - it('should reload', () => { - service.reload() - const req = httpTestingController.expectOne( - `${environment.apiBaseUrl}${endpoint}/?page=1&page_size=100000` - ) - req.flush({ - results: triggers, - }) - expect(service.allWorkflows).toEqual(triggers) - }) - - beforeEach(() => { - // Dont need to setup again - - httpTestingController = TestBed.inject(HttpTestingController) - service = TestBed.inject(WorkflowTriggerService) - }) - - afterEach(() => { - httpTestingController.verify() - }) -}) diff --git a/src-ui/src/app/services/rest/workflow-trigger.service.ts b/src-ui/src/app/services/rest/workflow-trigger.service.ts deleted file mode 100644 index 963ed071e..000000000 --- a/src-ui/src/app/services/rest/workflow-trigger.service.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { HttpClient } from '@angular/common/http' -import { Injectable } from '@angular/core' -import { tap } from 'rxjs' -import { AbstractPaperlessService } from './abstract-paperless-service' -import { WorkflowTrigger } from 'src/app/data/workflow-trigger' - -@Injectable({ - providedIn: 'root', -}) -export class WorkflowTriggerService extends AbstractPaperlessService { - loading: boolean - - constructor(http: HttpClient) { - super(http, 'workflow_triggers') - } - - public reload() { - this.loading = true - this.listAll().subscribe((r) => { - this.triggers = r.results - this.loading = false - }) - } - - private triggers: WorkflowTrigger[] = [] - - public get allWorkflows(): WorkflowTrigger[] { - return this.triggers - } - - create(o: WorkflowTrigger) { - return super.create(o).pipe(tap(() => this.reload())) - } - - update(o: WorkflowTrigger) { - return super.update(o).pipe(tap(() => this.reload())) - } - - delete(o: WorkflowTrigger) { - return super.delete(o).pipe(tap(() => this.reload())) - } -}