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

OZG-7473 extract method

Based on CR comment.
parent 11fa4e18
No related branches found
No related tags found
1 merge request!104Administration: Neu hinzugefügte Felder für Statistik speichern
import { ADMIN_FORMSERVICE } from '@admin-client/shared'; import { ADMIN_FORMSERVICE } from '@admin-client/shared';
import { NavigationService } from '@alfa-client/navigation-shared'; import { NavigationService } from '@alfa-client/navigation-shared';
import { AbstractFormService, createStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared'; import { AbstractFormService, createStateResource, isLoaded, StateResource } from '@alfa-client/tech-shared';
import { dispatchEventFromFixture, getMockComponent, mock, Mock, MockEvent } from '@alfa-client/test-utils'; 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 { faker } from '@faker-js/faker/.';
...@@ -17,10 +17,10 @@ import { AdminSaveButtonComponent } from './admin-save-button.component'; ...@@ -17,10 +17,10 @@ import { AdminSaveButtonComponent } from './admin-save-button.component';
jest.mock('@alfa-client/tech-shared', () => { jest.mock('@alfa-client/tech-shared', () => {
return { return {
...jest.requireActual('@alfa-client/tech-shared'), ...jest.requireActual('@alfa-client/tech-shared'),
isValidStateResource: jest.fn(), isLoaded: jest.fn(),
}; };
}); });
const isValidStateResourceMock: jest.Mock = isValidStateResource as jest.Mock; const isLoadedMock: jest.Mock = isLoaded as jest.Mock;
describe('AdminSaveButtonComponent', () => { describe('AdminSaveButtonComponent', () => {
let component: AdminSaveButtonComponent; let component: AdminSaveButtonComponent;
...@@ -61,7 +61,32 @@ describe('AdminSaveButtonComponent', () => { ...@@ -61,7 +61,32 @@ describe('AdminSaveButtonComponent', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
describe('component', () => {
describe('on successful form submission', () => {
it('should navigate to success link path', () => {
const successLinkPath: string = faker.internet.url();
component.successLinkPath = successLinkPath;
isLoadedMock.mockReturnValue(true);
component._navigateOnSuccessfulSubmission(createStateResource(createDummyResource()));
expect(navigationService.navigate).toHaveBeenCalledWith(successLinkPath);
});
it('should NOT navigate', () => {
isLoadedMock.mockReturnValue(false);
component._navigateOnSuccessfulSubmission(createStateResource(createDummyResource()));
expect(navigationService.navigate).not.toHaveBeenCalled();
});
});
describe('on submit', () => { describe('on submit', () => {
beforeEach(() => {
component._navigateOnSuccessfulSubmission = jest.fn();
});
it('should call formService', () => { it('should call formService', () => {
dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK);
...@@ -75,22 +100,11 @@ describe('AdminSaveButtonComponent', () => { ...@@ -75,22 +100,11 @@ describe('AdminSaveButtonComponent', () => {
}); });
it('should navigate on successful submit', () => { it('should navigate on successful submit', () => {
isValidStateResourceMock.mockReturnValue(true);
const linkPath: string = faker.internet.url();
component.successLinkPath = linkPath;
dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK);
component.stateResource$.subscribe(); component.stateResource$.subscribe();
expect(navigationService.navigate).toHaveBeenCalledWith(linkPath); expect(component._navigateOnSuccessfulSubmission).toHaveBeenCalledWith(stateResource);
}); });
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 { NavigationService } from '@alfa-client/navigation-shared'; import { NavigationService } from '@alfa-client/navigation-shared';
import { AbstractFormService, createEmptyStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared'; import { AbstractFormService, createEmptyStateResource, isLoaded, StateResource } from '@alfa-client/tech-shared';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Component, inject, Input } from '@angular/core'; import { Component, inject, Input } from '@angular/core';
import { Resource } from '@ngxp/rest'; import { Resource } from '@ngxp/rest';
...@@ -24,10 +24,14 @@ export class AdminSaveButtonComponent { ...@@ -24,10 +24,14 @@ export class AdminSaveButtonComponent {
public submit(): void { public submit(): void {
this.stateResource$ = this.formService.submit().pipe( this.stateResource$ = this.formService.submit().pipe(
tap((stateResource: StateResource<Resource>) => { tap((stateResource: StateResource<Resource>) => {
if (isValidStateResource(stateResource)) { this._navigateOnSuccessfulSubmission(stateResource);
this.navigationService.navigate(this.successLinkPath);
}
}), }),
); );
} }
_navigateOnSuccessfulSubmission(stateResource: StateResource<Resource>) {
if (isLoaded(stateResource)) {
this.navigationService.navigate(this.successLinkPath);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment