Skip to content
Snippets Groups Projects
Commit 51f90426 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6185 implement update bescheid, fix state, rename function

parent e62a846c
No related branches found
No related tags found
No related merge requests found
......@@ -58,6 +58,7 @@ describe('BescheidWizardContainerComponent', () => {
bescheidService = mock(BescheidService);
ozgcloudDialogService = mock(OzgcloudDialogService);
formService = new BescheidFormService(new UntypedFormBuilder(), useFromMock(bescheidService));
formService.patchValues = jest.fn();
wizardDialogRef = new DialogRefMock();
});
......@@ -118,6 +119,12 @@ describe('BescheidWizardContainerComponent', () => {
});
describe('ngOnInit', () => {
it('should init bescheid service', () => {
component.ngOnInit();
expect(bescheidService.init).toHaveBeenCalled();
});
it('should set vorgang with eingang on form service', () => {
formService.setVorgangWithEingangResource = jest.fn();
......@@ -174,9 +181,15 @@ describe('BescheidWizardContainerComponent', () => {
);
});
it('should patch form values', () => {
formService.patchValues = jest.fn();
it('should set bescheid state resource', () => {
component.subscribeToBescheidResource();
component.bescheidDraftStateResource$.subscribe();
expect(component.bescheidStateResource).toEqual(bescheidDraftStateResource);
});
it('should patch form values', () => {
component.subscribeToBescheidResource();
component.bescheidDraftStateResource$.subscribe();
......@@ -204,8 +217,6 @@ describe('BescheidWizardContainerComponent', () => {
});
it('should not patch form values', () => {
formService.patchValues = jest.fn();
component.subscribeToBescheidResource();
expect(formService.patchValues).not.toHaveBeenCalled();
......@@ -318,7 +329,7 @@ describe('BescheidWizardContainerComponent', () => {
});
});
describe('dialog canceled', () => {
describe('dialog cancelled', () => {
beforeEach(() => {
cancelDialogRef = new DialogRefMock();
const value = createDialogCancelResult();
......@@ -358,9 +369,9 @@ describe('BescheidWizardContainerComponent', () => {
component.cancelWizard = jest.fn();
});
it('should close wizard if bescheid resource loaded', () => {
it('should cancel wizard if bescheid resource loaded', () => {
const bescheidResource: BescheidResource = createBescheidResource();
component.bescheidDraftStateResource$ = of(createStateResource(bescheidResource));
component.bescheidStateResource = createStateResource(bescheidResource);
wizardDialogRef.keydownEvents = of(createKeydownKeyboardEvent(ESCAPE_KEY));
component.handleEscapeKey();
......@@ -368,7 +379,7 @@ describe('BescheidWizardContainerComponent', () => {
expect(component.cancelWizard).toHaveBeenCalledWith(bescheidResource);
});
it('should close wizard if bescheid resource not exists', () => {
it('should cancel wizard if bescheid resource not exists', () => {
component.bescheidDraftStateResource$ = of(createEmptyStateResource<BescheidResource>());
wizardDialogRef.keydownEvents = of(createKeydownKeyboardEvent(ESCAPE_KEY));
......@@ -377,7 +388,7 @@ describe('BescheidWizardContainerComponent', () => {
expect(component.cancelWizard).toHaveBeenCalledWith(null);
});
it('should not close if no escape key pressed', () => {
it('should not cancel if no escape key pressed', () => {
const bescheidResource: BescheidResource = createBescheidResource();
component.bescheidDraftStateResource$ = of(createStateResource(bescheidResource));
wizardDialogRef.keydownEvents = of(createKeydownKeyboardEvent('a'));
......@@ -387,9 +398,9 @@ describe('BescheidWizardContainerComponent', () => {
expect(component.cancelWizard).not.toHaveBeenCalled();
});
it('should not close if bescheid resource loading', () => {
it('should not cancel if bescheid resource loading', () => {
const bescheidResource: BescheidResource = createBescheidResource();
component.bescheidDraftStateResource$ = of(createStateResource(bescheidResource, true));
component.bescheidStateResource = createStateResource(bescheidResource, true);
wizardDialogRef.keydownEvents = of(createKeydownKeyboardEvent(ESCAPE_KEY));
component.handleEscapeKey();
......
......@@ -11,10 +11,10 @@ import { VorgangWithEingangLinkRel, VorgangWithEingangResource } from '@alfa-cli
import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
import { Component, Inject, OnDestroy, OnInit, ViewContainerRef } from '@angular/core';
import { hasLink } from '@ngxp/rest';
import { Observable, Subscription, filter, first, of, switchMap, tap } from 'rxjs';
import { Observable, Subscription, filter, first, map, of, tap } from 'rxjs';
import {
OzgcloudDialogCommandResult,
isDialogCanceled,
isDialogCancelled,
isDialogSuccessfullyCompleted,
} from '../../../../ui/src/lib/ui/ozgcloud-dialog/ozgcloud-dialog.result';
import {
......@@ -38,6 +38,7 @@ export class BescheidWizardContainerComponent implements OnInit, OnDestroy {
);
public activeStep: number = 1;
bescheidStateResource: StateResource<BescheidResource> = createEmptyStateResource();
isCancelDialogOpen: boolean = false;
vorgangWithEingangResource: VorgangWithEingangResource;
keydownEventsSubscription: Subscription;
......@@ -54,6 +55,7 @@ export class BescheidWizardContainerComponent implements OnInit, OnDestroy {
}
ngOnInit(): void {
this.bescheidService.init();
this.formService.setVorgangWithEingangResource(this.vorgangWithEingangResource);
this.subscribeToBescheidResource();
this.handleEscapeKey();
......@@ -67,6 +69,7 @@ export class BescheidWizardContainerComponent implements OnInit, OnDestroy {
if (hasLink(this.vorgangWithEingangResource, VorgangWithEingangLinkRel.BESCHEID_DRAFT)) {
this.bescheidDraftStateResource$ = this.bescheidService.getBescheidDraft().pipe(
tap((bescheidDraftStateResource: StateResource<BescheidResource>) => {
this.bescheidStateResource = bescheidDraftStateResource;
if (isLoaded(bescheidDraftStateResource)) {
this.formService.patchValues(bescheidDraftStateResource.resource);
}
......@@ -79,7 +82,7 @@ export class BescheidWizardContainerComponent implements OnInit, OnDestroy {
this.keydownEventsSubscription = this.dialogRef.keydownEvents
.pipe(
filter(isEscapeKey),
switchMap(() => this.bescheidDraftStateResource$),
map(() => this.bescheidStateResource),
filter(isNotLoading),
)
.subscribe((bescheidStateResource: StateResource<BescheidResource>) => {
......@@ -120,7 +123,7 @@ export class BescheidWizardContainerComponent implements OnInit, OnDestroy {
this.bescheidService.reloadDependentResources();
this.dialogRef.close();
}
if (isDialogCanceled(result)) {
if (isDialogCancelled(result)) {
this.dialogRef.close();
}
});
......
......@@ -34,6 +34,7 @@ describe('BescheidFormService', () => {
beforeEach(() => {
bescheidService = mock(BescheidService);
bescheidService.createBescheid.mockReturnValue(of(EMPTY));
bescheidService.updateBescheid.mockReturnValue(of(EMPTY));
service = new BescheidFormService(new UntypedFormBuilder(), useFromMock(bescheidService));
service.setVorgangWithEingangResource(vorgangWithEingangResource);
});
......@@ -46,6 +47,8 @@ describe('BescheidFormService', () => {
});
it('should create bescheid', () => {
service.isPatch = jest.fn().mockReturnValue(false);
bescheidService.existBescheid.mockReturnValue(false);
const formValue: Bescheid = createBescheid();
service.getBescheidFormValue = jest.fn().mockReturnValue(formValue);
......@@ -56,6 +59,27 @@ describe('BescheidFormService', () => {
formValue,
);
});
it('should update bescheid if patched', () => {
service.isPatch = jest.fn().mockReturnValue(true);
const formValue: Bescheid = createBescheid();
service.getBescheidFormValue = jest.fn().mockReturnValue(formValue);
service.submit();
expect(bescheidService.updateBescheid).toHaveBeenCalledWith(formValue);
});
it('should update bescheid if exists', () => {
service.isPatch = jest.fn().mockReturnValue(false);
bescheidService.existsBescheidDraft.mockReturnValue(true);
const formValue: Bescheid = createBescheid();
service.getBescheidFormValue = jest.fn().mockReturnValue(formValue);
service.submit();
expect(bescheidService.updateBescheid).toHaveBeenCalledWith(formValue);
});
});
describe('patchValues', () => {
......
......@@ -64,6 +64,9 @@ export class BescheidFormService extends AbstractFormService {
`Can't submit because ${this.constructor.name} is missing vorgang with eingang resource`,
);
}
if (this.isPatch() || this.bescheidService.existsBescheidDraft()) {
return this.bescheidService.updateBescheid(this.getBescheidFormValue());
}
return this.bescheidService.createBescheid(
this.vorgangWithEingangResource,
this.getBescheidFormValue(),
......
......@@ -3,27 +3,27 @@ import { createSuccessfullyDoneCommandStateResource } from '../../../../../comma
import {
createDialogCancelResult,
createDialogResult,
isDialogCanceled,
isDialogCancelled,
isDialogSuccessfullyCompleted,
} from './ozgcloud-dialog.result';
describe('ozgcloud-dialog.result', () => {
describe('isDialogCanceled', () => {
describe('isDialogCancelled', () => {
it('should return true', () => {
expect(isDialogCanceled({ stateResource: createEmptyStateResource() })).toBeTruthy();
expect(isDialogCancelled({ stateResource: createEmptyStateResource() })).toBeTruthy();
});
it('should return false for null result', () => {
expect(isDialogCanceled(null)).toBeFalsy();
expect(isDialogCancelled(null)).toBeFalsy();
});
it('should return false for null state resource', () => {
expect(isDialogCanceled({ stateResource: null })).toBeFalsy();
expect(isDialogCancelled({ stateResource: null })).toBeFalsy();
});
it('should return false for non empty state resource', () => {
expect(
isDialogCanceled({ stateResource: createSuccessfullyDoneCommandStateResource() }),
isDialogCancelled({ stateResource: createSuccessfullyDoneCommandStateResource() }),
).toBeFalsy();
});
});
......
......@@ -20,7 +20,7 @@ export function createDialogResult(
return { stateResource };
}
export function isDialogCanceled(dialogResult?: OzgcloudDialogCommandResult): boolean {
export function isDialogCancelled(dialogResult?: OzgcloudDialogCommandResult): boolean {
return dialogResult && isEmptyStateResource(dialogResult.stateResource);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment