diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/externe-fachstelle-container/externe-fachstelle-container.component.spec.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/externe-fachstelle-container/externe-fachstelle-container.component.spec.ts index 51f7b9f39c2a513bba610db9e81113a53411dfd4..7defa2f726d2600948b567387a209b4b43f1aa90 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/externe-fachstelle-container/externe-fachstelle-container.component.spec.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/externe-fachstelle-container/externe-fachstelle-container.component.spec.ts @@ -9,6 +9,7 @@ import { notExistsAsHtmlElement, useFromMock, } from '@alfa-client/test-utils'; +import { SnackBarService } from '@alfa-client/ui'; import { VorgangWithEingangLinkRel, VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import { SearchExterneFachstelleContainerComponent } from '@alfa-client/zustaendige-stelle'; import { ComponentFixture, TestBed } from '@angular/core/testing'; @@ -40,7 +41,11 @@ describe('ExterneFachstelleContainerComponent', () => { const collaborationListResource: CollaborationListResource = createCollaborationListResource(); beforeEach(async () => { - formService = new CollaborationRequestFormService(new FormBuilder(), useFromMock(mock(CollaborationService))); + formService = new CollaborationRequestFormService( + new FormBuilder(), + useFromMock(mock(CollaborationService)), + useFromMock(mock(SnackBarService)), + ); service = mock(CollaborationService); TestBed.overrideComponent(CollaborationRequestFormComponent, { set: { diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/organisations-einheit-container/organisations-einheit-container.component.spec.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/organisations-einheit-container/organisations-einheit-container.component.spec.ts index 6184b6fdb3a36d7404160df9ce3005e9bc42eea6..414d95263a5b2c0158a455909863171803fade67 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/organisations-einheit-container/organisations-einheit-container.component.spec.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-in-vorgang/organisations-einheit-container/organisations-einheit-container.component.spec.ts @@ -9,6 +9,7 @@ import { notExistsAsHtmlElement, useFromMock, } from '@alfa-client/test-utils'; +import { SnackBarService } from '@alfa-client/ui'; import { VorgangWithEingangLinkRel, VorgangWithEingangResource } from '@alfa-client/vorgang-shared'; import { SearchOrganisationsEinheitContainerComponent } from '@alfa-client/zustaendige-stelle'; import { ComponentFixture, TestBed } from '@angular/core/testing'; @@ -40,7 +41,11 @@ describe('OrganisationsEinheitContainerComponent', () => { const collaborationListResource: CollaborationListResource = createCollaborationListResource(); beforeEach(async () => { - formService = new CollaborationRequestFormService(new FormBuilder(), useFromMock(mock(CollaborationService))); + formService = new CollaborationRequestFormService( + new FormBuilder(), + useFromMock(mock(CollaborationService)), + useFromMock(mock(SnackBarService)), + ); service = mock(CollaborationService); TestBed.overrideComponent(CollaborationRequestFormComponent, { set: { diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.spec.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.spec.ts index c6afc4f1d8ce8bbe019c5a6ad1c1a70bf4f91255..23949586177e6dd74e43221e75f8583f9ccf0e40 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.spec.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.spec.ts @@ -2,6 +2,7 @@ import { CollaborationListResource } from '@alfa-client/collaboration-shared'; import { CommandLinkRel, CommandResource } from '@alfa-client/command-shared'; import { StateResource, createStateResource } from '@alfa-client/tech-shared'; import { dispatchEventFromFixture, getMockComponent, mock, useFromMock } from '@alfa-client/test-utils'; +import { SnackBarService } from '@alfa-client/ui'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; import { ButtonWithSpinnerComponent, TextEditorComponent, TextareaEditorComponent } from '@ods/component'; @@ -28,7 +29,11 @@ describe('CollaborationRequestFormComponent', () => { const stateCommandResource: StateResource<CommandResource> = createStateResource(createCommandResource()); beforeEach(async () => { - formService = new CollaborationRequestFormService(new FormBuilder(), useFromMock(mock(CollaborationService))); + formService = new CollaborationRequestFormService( + new FormBuilder(), + useFromMock(mock(CollaborationService)), + useFromMock(mock(SnackBarService)), + ); TestBed.overrideComponent(CollaborationRequestFormComponent, { set: { providers: [ @@ -132,14 +137,6 @@ describe('CollaborationRequestFormComponent', () => { expect(formService.submit).toHaveBeenCalled(); }); - it('should emit show result', () => { - component.showResult.emit = jest.fn(); - - component.doSubmit().subscribe(); - - expect(component.showResult.emit).toHaveBeenCalledWith(formService.form.value); - }); - it('should return value', () => { formService.submit = jest.fn().mockReturnValue(of(stateCommandResource)); diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.ts index 36fabdc2f2ec25a404005de60ec512640d1cd04e..482844e993c719211db9f9ef8e1833953fbed29d 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration-request-form.component.ts @@ -1,5 +1,5 @@ -import { Collaboration, CollaborationListResource } from '@alfa-client/collaboration-shared'; -import { CommandResource, tapOnCommandSuccessfullyDone } from '@alfa-client/command-shared'; +import { CollaborationListResource } from '@alfa-client/collaboration-shared'; +import { CommandResource } from '@alfa-client/command-shared'; import { StateResource, createEmptyStateResource } from '@alfa-client/tech-shared'; import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Observable, of } from 'rxjs'; @@ -11,7 +11,6 @@ import { CollaborationRequestFormService } from './collaboration.request.formser }) export class CollaborationRequestFormComponent { @Output() public abbrechen: EventEmitter<void> = new EventEmitter<void>(); - @Output() public showResult: EventEmitter<Collaboration> = new EventEmitter<Collaboration>(); @Input() public set collaborationListResource(collaborationListResource: CollaborationListResource) { this.formService.setListResource(collaborationListResource); @@ -28,8 +27,6 @@ export class CollaborationRequestFormComponent { } doSubmit(): Observable<StateResource<CommandResource>> { - return this.formService - .submit() - .pipe(tapOnCommandSuccessfullyDone(() => this.showResult.emit(this.formService.getFormValue()))); + return this.formService.submit(); } } diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.spec.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.spec.ts index 4820b2987eddad0fea076dbf9d94c391f19735f3..0688eb4db4e249d6fd12760e752c831db9a0de1e 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.spec.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.spec.ts @@ -1,11 +1,13 @@ import { CollaborationListResource } from '@alfa-client/collaboration-shared'; +import { SnackBarService } from '@alfa-client/ui'; +import { TestBed } from '@angular/core/testing'; import { CommandLinkRel, CommandResource } from '@alfa-client/command-shared'; -import { StateResource, createStateResource } from '@alfa-client/tech-shared'; +import { StateResource, createEmptyStateResource, createStateResource } from '@alfa-client/tech-shared'; import { Mock, mock, useFromMock } from '@alfa-client/test-utils'; import { UntypedFormBuilder } from '@angular/forms'; import { CollaborationService } from 'libs/collaboration-shared/src/lib/collaboration.service'; import { createCollaborationListResource } from 'libs/collaboration-shared/test/collaboration'; -import { createCommandResource } from 'libs/command-shared/test/command'; +import { createCommandErrorResource, createCommandResource } from 'libs/command-shared/test/command'; import { singleColdCompleted } from 'libs/tech-shared/test/marbles'; import { Observable, of } from 'rxjs'; import { CollaborationRequestFormService } from './collaboration.request.formservice'; @@ -14,15 +16,25 @@ describe('CollaborationRequestFormService', () => { let formService: CollaborationRequestFormService; let service: Mock<CollaborationService>; - - const formBuilder: UntypedFormBuilder = new UntypedFormBuilder(); + let snackBarService: Mock<SnackBarService>; const collaborationListResource: CollaborationListResource = createCollaborationListResource(); beforeEach(() => { service = mock(CollaborationService); + snackBarService = mock(SnackBarService); + + TestBed.configureTestingModule({ + providers: [ + CollaborationRequestFormService, + UntypedFormBuilder, + { provide: CollaborationService, useValue: service }, + { provide: SnackBarService, useValue: snackBarService }, + ], + }); - formService = new CollaborationRequestFormService(formBuilder, useFromMock(service)); + formService = TestBed.inject(CollaborationRequestFormService); + TestBed.inject(CollaborationRequestFormService); }); it('should create', () => { @@ -65,6 +77,14 @@ describe('CollaborationRequestFormService', () => { expect(service.refreshList).toHaveBeenCalled(); }); + it('should call handleCommandError', () => { + const handleCommanErrorSpy = jest.spyOn(formService as any, 'handleCommandError'); + + formService.submit().subscribe(); + + expect(handleCommanErrorSpy).toHaveBeenCalledWith(stateCommandResource.resource); + }); + it('should return stateCommandResource', () => { const response$: Observable<StateResource<CommandResource>> = formService.submit(); @@ -72,6 +92,41 @@ describe('CollaborationRequestFormService', () => { }); }); + describe('handleCommandError', () => { + let showErrorSpy: jest.SpyInstance; + + beforeEach(() => { + showErrorSpy = jest.spyOn(formService as any, 'showError'); + }); + + it('should not do anything if commandResource is nil', () => { + service.create.mockReturnValue(of(createEmptyStateResource())); + + formService.submit().subscribe(); + + expect(showErrorSpy).not.toHaveBeenCalled(); + }); + + it('should call showError if commandResource has error', () => { + service.create.mockReturnValue(of(createStateResource(createCommandErrorResource()))); + + formService.submit().subscribe(); + + expect(showErrorSpy).toHaveBeenCalled(); + }); + }); + + describe('showError', () => { + it('should call snackBarService.showError with commandResource errorMessage', () => { + const commandResourceWithError: CommandResource = createCommandErrorResource(); + service.create.mockReturnValue(of(createStateResource(commandResourceWithError))); + + formService.submit().subscribe(); + + expect(snackBarService.showError).toHaveBeenCalledWith(CollaborationRequestFormService.GENERIC_ERROR_MESSAGE); + }); + }); + describe('set list resource', () => { it('should set given list resource', () => { formService.listResource = undefined; diff --git a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.ts b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.ts index 0b3f33a899156e8af47cd1a7db35abe9a255a38e..71dd8739c3b80fafbcef6dc4e534daaee16f267a 100644 --- a/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.ts +++ b/alfa-client/libs/collaboration/src/lib/collaboration-in-vorgang-container/collaboration-request-form/collaboration.request.formservice.ts @@ -1,11 +1,13 @@ import { CollaborationListResource } from '@alfa-client/collaboration-shared'; -import { CommandResource, tapOnCommandSuccessfullyDone } from '@alfa-client/command-shared'; +import { CommandResource, hasCommandError, tapOnCommandSuccessfullyDone } from '@alfa-client/command-shared'; import { AbstractFormService, StateResource } from '@alfa-client/tech-shared'; +import { SnackBarService } from '@alfa-client/ui'; import { Injectable } from '@angular/core'; import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; import { ResourceUri } from '@ngxp/rest'; import { CollaborationService } from 'libs/collaboration-shared/src/lib/collaboration.service'; -import { Observable } from 'rxjs'; +import { isNil } from 'lodash-es'; +import { Observable, tap } from 'rxjs'; @Injectable() export class CollaborationRequestFormService extends AbstractFormService<CommandResource> { @@ -13,6 +15,8 @@ export class CollaborationRequestFormService extends AbstractFormService<Command public static readonly FIELD_TITEL: string = 'titel'; public static readonly FIELD_BESCHREIBUNG: string = 'beschreibung'; + public static readonly GENERIC_ERROR_MESSAGE: string = 'Zuarbeit konnte nicht angefragt werden'; + private static readonly PATH_PREFIX: string = 'command.body'; listResource: CollaborationListResource; @@ -20,6 +24,7 @@ export class CollaborationRequestFormService extends AbstractFormService<Command constructor( formBuilder: FormBuilder, private service: CollaborationService, + private snackBarService: SnackBarService, ) { super(formBuilder); } @@ -33,9 +38,24 @@ export class CollaborationRequestFormService extends AbstractFormService<Command } protected doSubmit(): Observable<StateResource<CommandResource>> { - return this.service - .create(this.listResource, this.getFormValue()) - .pipe(tapOnCommandSuccessfullyDone(() => this.service.refreshList())); + return this.service.create(this.listResource, this.getFormValue()).pipe( + tapOnCommandSuccessfullyDone(() => this.service.refreshList()), + tap((commandStateResource: StateResource<CommandResource>) => { + this.handleCommandError(commandStateResource.resource); + }), + ); + } + + protected handleCommandError(commandResource: CommandResource): void { + if (isNil(commandResource)) return; + + if (hasCommandError(commandResource)) { + this.showError(); + } + } + + protected showError(): void { + this.snackBarService.showError(CollaborationRequestFormService.GENERIC_ERROR_MESSAGE); } protected getPathPrefix(): string {