Skip to content
Snippets Groups Projects
Verified Commit c9cdbdd9 authored by Sebastian Bergandy's avatar Sebastian Bergandy :keyboard:
Browse files

OZG-7473 navigate to list after save

Sub task: OZG-7884
parent 6f730e42
Branches
Tags
1 merge request!104Administration: Neu hinzugefügte Felder für Statistik speichern
import { StateResource, createStateResource } from '@alfa-client/tech-shared'; import { createStateResource, StateResource } from '@alfa-client/tech-shared';
import { Mock, mock } from '@alfa-client/test-utils'; import { Mock, mock } from '@alfa-client/test-utils';
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { expect } from '@jest/globals';
import { singleCold } from 'libs/tech-shared/test/marbles'; import { singleCold } from 'libs/tech-shared/test/marbles';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { import {
...@@ -76,4 +77,14 @@ describe('AggregationMappingService', () => { ...@@ -76,4 +77,14 @@ describe('AggregationMappingService', () => {
expect(loadedAggregationMappingResource).toBeObservable(singleCold(aggregationMappingStateResource)); expect(loadedAggregationMappingResource).toBeObservable(singleCold(aggregationMappingStateResource));
}); });
}); });
describe('refresh list', () => {
it('should call list resource service', () => {
listResourceService.refresh = jest.fn();
service.refreshList();
expect(listResourceService.refresh).toHaveBeenCalled();
});
});
}); });
import { StateResource } from '@alfa-client/tech-shared'; import { StateResource } from '@alfa-client/tech-shared';
import { Injectable, inject } from '@angular/core'; import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { AggregationMappingListResourceService } from './aggregation-mapping-resource.service'; import { AggregationMappingListResourceService } from './aggregation-mapping-resource.service';
import { AggregationMapping, AggregationMappingListResource, AggregationMappingResource } from './aggregation-mapping.model'; import { AggregationMapping, AggregationMappingListResource, AggregationMappingResource } from './aggregation-mapping.model';
...@@ -15,4 +15,8 @@ export class AggregationMappingService { ...@@ -15,4 +15,8 @@ export class AggregationMappingService {
public create(toCreate: AggregationMapping): Observable<StateResource<AggregationMappingResource>> { public create(toCreate: AggregationMapping): Observable<StateResource<AggregationMappingResource>> {
return this.listService.create(toCreate); return this.listService.create(toCreate);
} }
public refreshList(): void {
this.listService.refresh();
}
} }
import { ADMIN_FORMSERVICE } from '@admin-client/shared'; import { ADMIN_FORMSERVICE } from '@admin-client/shared';
import { AbstractFormService, createStateResource, StateResource } from '@alfa-client/tech-shared'; import { NavigationService } from '@alfa-client/navigation-shared';
import { dispatchEventFromFixture, getMockComponent, Mock, MockEvent } from '@alfa-client/test-utils'; import { AbstractFormService, createStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared';
import { dispatchEventFromFixture, getMockComponent, mock, Mock, MockEvent } from '@alfa-client/test-utils';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { faker } from '@faker-js/faker/.';
import { expect } from '@jest/globals';
import { Resource } from '@ngxp/rest'; import { Resource } from '@ngxp/rest';
import { ButtonWithSpinnerComponent } from '@ods/component'; import { ButtonWithSpinnerComponent } from '@ods/component';
import { getDataTestIdOf } from 'libs/tech-shared/test/data-test'; import { getDataTestIdOf } from 'libs/tech-shared/test/data-test';
import { singleCold } from 'libs/tech-shared/test/marbles'; import { singleColdCompleted } from 'libs/tech-shared/test/marbles';
import { createDummyResource } from 'libs/tech-shared/test/resource'; import { createDummyResource } from 'libs/tech-shared/test/resource';
import { MockComponent } from 'ng-mocks'; import { MockComponent } from 'ng-mocks';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { AdminSaveButtonComponent } from './admin-save-button.component'; import { AdminSaveButtonComponent } from './admin-save-button.component';
jest.mock('@alfa-client/tech-shared', () => {
return {
...jest.requireActual('@alfa-client/tech-shared'),
isValidStateResource: jest.fn(),
};
});
const isValidStateResourceMock: jest.Mock = isValidStateResource as jest.Mock;
describe('AdminSaveButtonComponent', () => { describe('AdminSaveButtonComponent', () => {
let component: AdminSaveButtonComponent; let component: AdminSaveButtonComponent;
let fixture: ComponentFixture<AdminSaveButtonComponent>; let fixture: ComponentFixture<AdminSaveButtonComponent>;
let navigationService: Mock<NavigationService>;
let formService: Mock<AbstractFormService<Resource>>; let formService: Mock<AbstractFormService<Resource>>;
const saveButton: string = getDataTestIdOf('save'); const saveButton: string = getDataTestIdOf('save');
...@@ -22,7 +34,8 @@ describe('AdminSaveButtonComponent', () => { ...@@ -22,7 +34,8 @@ describe('AdminSaveButtonComponent', () => {
const stateResource: StateResource<Resource> = createStateResource(createDummyResource()); const stateResource: StateResource<Resource> = createStateResource(createDummyResource());
beforeEach(async () => { beforeEach(async () => {
formService = <any>{ submit: jest.fn().mockReturnValue(singleCold(stateResource)) }; formService = <any>{ submit: jest.fn().mockReturnValue(of(stateResource)) };
navigationService = mock(NavigationService);
await TestBed.configureTestingModule({ await TestBed.configureTestingModule({
imports: [AdminSaveButtonComponent], imports: [AdminSaveButtonComponent],
...@@ -32,6 +45,10 @@ describe('AdminSaveButtonComponent', () => { ...@@ -32,6 +45,10 @@ describe('AdminSaveButtonComponent', () => {
provide: ADMIN_FORMSERVICE, provide: ADMIN_FORMSERVICE,
useValue: formService, useValue: formService,
}, },
{
provide: NavigationService,
useValue: navigationService,
},
], ],
}).compileComponents(); }).compileComponents();
...@@ -54,7 +71,26 @@ describe('AdminSaveButtonComponent', () => { ...@@ -54,7 +71,26 @@ describe('AdminSaveButtonComponent', () => {
it('should assign state resource', () => { it('should assign state resource', () => {
dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK);
expect(component.stateResource$).toBeObservable(singleCold(stateResource)); expect(component.stateResource$).toBeObservable(singleColdCompleted(stateResource));
});
it('should navigate on successful submit', () => {
isValidStateResourceMock.mockReturnValue(true);
const linkPath: string = faker.internet.url();
component.successLinkPath = linkPath;
dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK);
component.stateResource$.subscribe();
expect(navigationService.navigate).toHaveBeenCalledWith(linkPath);
});
it('should NOT navigate on invalid state resource', () => {
isValidStateResourceMock.mockReturnValue(false);
dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK);
component.stateResource$.subscribe();
expect(navigationService.navigate).not.toHaveBeenCalled();
}); });
}); });
......
import { ADMIN_FORMSERVICE } from '@admin-client/shared'; import { ADMIN_FORMSERVICE } from '@admin-client/shared';
import { AbstractFormService, createEmptyStateResource, StateResource } from '@alfa-client/tech-shared'; import { NavigationService } from '@alfa-client/navigation-shared';
import { AbstractFormService, createEmptyStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Component, inject } from '@angular/core'; import { Component, inject, Input } from '@angular/core';
import { Resource } from '@ngxp/rest'; import { Resource } from '@ngxp/rest';
import { ButtonWithSpinnerComponent } from '@ods/component'; import { ButtonWithSpinnerComponent } from '@ods/component';
import { Observable, of } from 'rxjs'; import { Observable, of, tap } from 'rxjs';
@Component({ @Component({
selector: 'admin-save-button', selector: 'admin-save-button',
...@@ -13,11 +14,20 @@ import { Observable, of } from 'rxjs'; ...@@ -13,11 +14,20 @@ import { Observable, of } from 'rxjs';
templateUrl: './admin-save-button.component.html', templateUrl: './admin-save-button.component.html',
}) })
export class AdminSaveButtonComponent { export class AdminSaveButtonComponent {
private formService: AbstractFormService<Resource> = inject(ADMIN_FORMSERVICE); @Input() successLinkPath: string;
private readonly formService: AbstractFormService<Resource> = inject(ADMIN_FORMSERVICE);
private readonly navigationService: NavigationService = inject(NavigationService);
public stateResource$: Observable<StateResource<Resource>> = of(createEmptyStateResource<Resource>()); public stateResource$: Observable<StateResource<Resource>> = of(createEmptyStateResource<Resource>());
public submit(): void { public submit(): void {
this.stateResource$ = this.formService.submit(); this.stateResource$ = this.formService.submit().pipe(
tap((stateResource: StateResource<Resource>) => {
if (isValidStateResource(stateResource)) {
this.navigationService.navigate(this.successLinkPath);
}
}),
);
} }
} }
...@@ -93,6 +93,14 @@ describe('StatistikContainerComponent', () => { ...@@ -93,6 +93,14 @@ describe('StatistikContainerComponent', () => {
}); });
}); });
describe('on destroy', () => {
it('should refresh aggregation mapping list', () => {
component.ngOnDestroy();
expect(aggregationMappingService.refreshList).toHaveBeenCalled();
});
});
describe('template', () => { describe('template', () => {
describe('aggregation mapping list', () => { describe('aggregation mapping list', () => {
it('should exists', () => { it('should exists', () => {
......
...@@ -25,7 +25,7 @@ import { AggregationMappingListResource, AggregationMappingService } from '@admi ...@@ -25,7 +25,7 @@ import { AggregationMappingListResource, AggregationMappingService } from '@admi
import { ROUTES } from '@admin-client/shared'; import { ROUTES } from '@admin-client/shared';
import { StateResource } from '@alfa-client/tech-shared'; import { StateResource } from '@alfa-client/tech-shared';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core'; import { Component, inject, OnDestroy, OnInit } from '@angular/core';
import { RoutingButtonComponent } from '@ods/component'; import { RoutingButtonComponent } from '@ods/component';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { AggregationMappingListComponent } from '../aggregation-mapping-list/aggregation-mapping-list.component'; import { AggregationMappingListComponent } from '../aggregation-mapping-list/aggregation-mapping-list.component';
...@@ -37,7 +37,7 @@ import { AggregationMappingListComponent } from '../aggregation-mapping-list/agg ...@@ -37,7 +37,7 @@ import { AggregationMappingListComponent } from '../aggregation-mapping-list/agg
imports: [CommonModule, RoutingButtonComponent, AggregationMappingListComponent], imports: [CommonModule, RoutingButtonComponent, AggregationMappingListComponent],
providers: [AggregationMappingService], providers: [AggregationMappingService],
}) })
export class StatistikContainerComponent implements OnInit { export class StatistikContainerComponent implements OnInit, OnDestroy {
private service = inject(AggregationMappingService); private service = inject(AggregationMappingService);
public listStateResource$: Observable<StateResource<AggregationMappingListResource>>; public listStateResource$: Observable<StateResource<AggregationMappingListResource>>;
...@@ -47,4 +47,8 @@ export class StatistikContainerComponent implements OnInit { ...@@ -47,4 +47,8 @@ export class StatistikContainerComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
this.listStateResource$ = this.service.getList(); this.listStateResource$ = this.service.getList();
} }
ngOnDestroy(): void {
this.service.refreshList();
}
} }
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
</ods-button> </ods-button>
<div class="mt-4 flex gap-4"> <div class="mt-4 flex gap-4">
<admin-save-button /> <admin-save-button [successLinkPath]="Routes.STATISTIK" />
<admin-cancel-button [linkPath]="Routes.STATISTIK" /> <admin-cancel-button [linkPath]="Routes.STATISTIK" />
</div> </div>
</div> </div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment