mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
AAE-30864 Refactored services to accept injected validators (#10660)
* [AAE-30864] refactored services to accept injected validators * [AAE-30864] updated documentation, applied pr comments
This commit is contained in:
@@ -16,10 +16,11 @@
|
||||
*/
|
||||
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { FormCloudService } from './form-cloud.service';
|
||||
import { FORM_CLOUD_SERVICE_FIELD_VALIDATORS_TOKEN, FormCloudService } from './form-cloud.service';
|
||||
import { of } from 'rxjs';
|
||||
import { ProcessServiceCloudTestingModule } from '../../testing/process-service-cloud.testing.module';
|
||||
import { AdfHttpClient } from '@alfresco/adf-core/api';
|
||||
import { FORM_FIELD_VALIDATORS, FormFieldValidator } from '@alfresco/adf-core';
|
||||
|
||||
const mockTaskResponseBody = {
|
||||
entry: { id: 'id', name: 'name', formKey: 'form-key' }
|
||||
@@ -27,6 +28,12 @@ const mockTaskResponseBody = {
|
||||
|
||||
const mockFormResponseBody = { formRepresentation: { id: 'form-id', name: 'task-form', taskId: 'task-id' } };
|
||||
|
||||
const fakeValidator = {
|
||||
supportedTypes: ['test'],
|
||||
isSupported: () => true,
|
||||
validate: () => true
|
||||
} as FormFieldValidator;
|
||||
|
||||
describe('Form Cloud service', () => {
|
||||
let service: FormCloudService;
|
||||
let adfHttpClient: AdfHttpClient;
|
||||
@@ -37,7 +44,8 @@ describe('Form Cloud service', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [ProcessServiceCloudTestingModule]
|
||||
imports: [ProcessServiceCloudTestingModule],
|
||||
providers: [{ provide: FORM_CLOUD_SERVICE_FIELD_VALIDATORS_TOKEN, useValue: [fakeValidator] }]
|
||||
});
|
||||
service = TestBed.inject(FormCloudService);
|
||||
adfHttpClient = TestBed.inject(AdfHttpClient);
|
||||
@@ -68,6 +76,14 @@ describe('Form Cloud service', () => {
|
||||
expect(result.id).toBe(formId);
|
||||
expect(result.name).toBe('task-form');
|
||||
});
|
||||
|
||||
it('should create form with injected validators', () => {
|
||||
const formId = 'form-id';
|
||||
const json = { formRepresentation: { id: formId, name: 'task-form', taskId: 'task-id', formDefinition: {} } };
|
||||
const result = service.parseForm(json, undefined, undefined);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.fieldValidators).toEqual([...FORM_FIELD_VALIDATORS, fakeValidator]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Task tests', () => {
|
||||
|
@@ -15,8 +15,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { FormValues, FormModel, FormFieldOption } from '@alfresco/adf-core';
|
||||
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
|
||||
import { FormValues, FormModel, FormFieldOption, FormFieldValidator } from '@alfresco/adf-core';
|
||||
import { Observable, from, EMPTY } from 'rxjs';
|
||||
import { expand, map, reduce, switchMap } from 'rxjs/operators';
|
||||
import { TaskDetailsCloudModel } from '../../task/models/task-details-cloud.model';
|
||||
@@ -27,18 +27,25 @@ import { FormContent } from '../../services/form-fields.interfaces';
|
||||
import { FormCloudServiceInterface } from './form-cloud.service.interface';
|
||||
import { AdfHttpClient } from '@alfresco/adf-core/api';
|
||||
|
||||
export const FORM_CLOUD_SERVICE_FIELD_VALIDATORS_TOKEN = new InjectionToken<FormFieldValidator[]>('FORM_CLOUD_SERVICE_FIELD_VALIDATORS_TOKEN');
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class FormCloudService extends BaseCloudService implements FormCloudServiceInterface {
|
||||
private _uploadApi: UploadApi;
|
||||
private fieldValidators: FormFieldValidator[];
|
||||
get uploadApi(): UploadApi {
|
||||
this._uploadApi = this._uploadApi ?? new UploadApi(this.apiService.getInstance());
|
||||
return this._uploadApi;
|
||||
}
|
||||
|
||||
constructor(adfHttpClient: AdfHttpClient) {
|
||||
constructor(
|
||||
adfHttpClient: AdfHttpClient,
|
||||
@Optional() @Inject(FORM_CLOUD_SERVICE_FIELD_VALIDATORS_TOKEN) injectedFieldValidators?: FormFieldValidator[]
|
||||
) {
|
||||
super(adfHttpClient);
|
||||
this.fieldValidators = injectedFieldValidators || [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,7 +226,7 @@ export class FormCloudService extends BaseCloudService implements FormCloudServi
|
||||
formValues[variable.name] = variable.value;
|
||||
});
|
||||
|
||||
return new FormModel(flattenForm, formValues, readOnly);
|
||||
return new FormModel(flattenForm, formValues, readOnly, undefined, undefined, this.fieldValidators);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user