Select Git revision
Jenkinsfile
formservice.abstract.spec.ts 6.33 KiB
/*
* Copyright (C) 2022 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
import { CommandResource } from '@alfa-client/command-shared';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { Resource } from '@ngxp/rest';
import { cold } from 'jest-marbles';
import {
createApiError,
createInvalidParam,
createIssue,
createProblemDetail,
} from 'libs/tech-shared/test/error';
import { Observable, of } from 'rxjs';
import { AbstractFormService } from './formservice.abstract';
import {
StateResource,
createEmptyStateResource,
createErrorStateResource,
} from '../resource/resource.util';
import { ApiError, HttpError, InvalidParam, Issue, ProblemDetail } from '../tech.model';
import * as ValidationUtil from '../validation/tech.validation.util';
describe('AbstractFormService', () => {
let formService: AbstractFormService;
beforeEach(() => {
formService = new TestFormService(new UntypedFormBuilder());
});
it('should create', () => {
expect(formService).toBeTruthy();
});
describe('submit', () => {
describe('with api error', () => {
const stateResourceWithError: StateResource<ApiError> =
createErrorStateResource(createApiError());
beforeEach(() => {
TestFormService.SUBMIT_OBSERVABLE = () => of(stateResourceWithError);
formService.handleResponse = jest.fn((stateResource) => stateResource);
});
it('should call handle response for api error', (done) => {
formService.submit().subscribe(() => {
expect(formService.handleResponse).toHaveBeenCalledWith(stateResourceWithError);
done();
});
});
it('should return state resource observable', () => {
const submitObservable: Observable<StateResource<Resource | HttpError>> =
formService.submit();
expect(submitObservable).toBeObservable(cold('(a|)', { a: stateResourceWithError }));
});
});
});
describe('handleResponse', () => {
const apiError: ApiError = createApiError();
const stateResource: StateResource<CommandResource> = createErrorStateResource(apiError);
beforeEach(() => {
formService.handleError = jest.fn();
});
it('should handleError on validation error', () => {
formService.handleResponse({ ...stateResource, loading: false });
expect(formService.handleError).toHaveBeenCalledWith(apiError);
});
it('should return stateresource while loading', () => {
const commandStateResource: StateResource<CommandResource> = createEmptyStateResource(true);
const result: StateResource<Resource | HttpError> =
formService.handleResponse(commandStateResource);
expect(result).toBe(commandStateResource);
});
});
describe('handle error', () => {
it('should set problem detail error', () => {
formService.setErrorByProblemDetail = jest.fn();
const problemDetail: ProblemDetail = createProblemDetail();
formService.handleError(problemDetail);
expect(formService.setErrorByProblemDetail).toHaveBeenCalledWith(problemDetail);
});
it('should set api error', () => {
formService.setErrorByApiError = jest.fn();
const apiError: ApiError = createApiError();
formService.handleError(apiError);
expect(formService.setErrorByApiError).toHaveBeenCalledWith(apiError);
});
});
describe('set error by api error', () => {
const issue: Issue = createIssue();
const apiError: ApiError = createApiError([issue]);
it('should call setIssueValidationError', () => {
const setInvalidParamValidationErrorSpy: jest.SpyInstance<void> = jest
.spyOn(ValidationUtil, 'setIssueValidationError')
.mockImplementation();
formService.setErrorByApiError(apiError);
expect(setInvalidParamValidationErrorSpy).toHaveBeenCalledWith(
formService.form,
issue,
TestFormService.PATH_PREFIX,
);
});
});
describe('set error by problem detail', () => {
const invalidParam: InvalidParam = createInvalidParam();
const problemDetail: ProblemDetail = createProblemDetail([invalidParam]);
it('should call setInvalidParamValidationError', () => {
const setInvalidParamValidationErrorSpy: jest.SpyInstance<void> = jest
.spyOn(ValidationUtil, 'setInvalidParamValidationError')
.mockImplementation();
formService.setErrorByProblemDetail(problemDetail);
expect(setInvalidParamValidationErrorSpy).toHaveBeenCalledWith(
formService.form,
invalidParam,
TestFormService.PATH_PREFIX,
);
});
});
describe('patch', () => {
it('should set form value', () => {
const formValue: { [key: string]: string } = { [TestFormService.FIELD]: 'huhu' };
formService.patch(formValue);
expect(formService.form.value).toEqual(formValue);
});
});
});
class TestFormService extends AbstractFormService {
public static readonly FIELD: string = 'attribute';
public static readonly PATH_PREFIX: string = 'path-prefix';
public static SUBMIT_OBSERVABLE = () => of(createEmptyStateResource());
constructor(formBuilder: UntypedFormBuilder) {
super(formBuilder);
}
protected initForm(): UntypedFormGroup {
return this.formBuilder.group({
[TestFormService.FIELD]: new UntypedFormControl(null),
});
}
protected doSubmit(): Observable<StateResource<any>> {
return TestFormService.SUBMIT_OBSERVABLE();
}
protected getPathPrefix(): string {
return TestFormService.PATH_PREFIX;
}
}