diff --git a/lib/core/form/models/form-rules.model.spec.ts b/lib/core/form/models/form-rules.model.spec.ts index a89f14322b0..d7f7b8fd12f 100644 --- a/lib/core/form/models/form-rules.model.spec.ts +++ b/lib/core/form/models/form-rules.model.spec.ts @@ -22,6 +22,10 @@ import { TranslateModule } from '@ngx-translate/core'; import { ByPassFormRuleManager, FormRulesManager, formRulesManagerFactory, FORM_RULES_MANAGER } from '../models/form-rules.model'; import { Injector } from '@angular/core'; import { TestBed } from '@angular/core/testing'; +import { FormModel } from '../components/widgets/core/form.model'; +import { FormRulesEvent } from '../events/form-rules.event'; +import { FormEvent } from '../events/form.event'; +import { FormService } from '../services/form.service'; class CustomRuleManager extends FormRulesManager { protected getRules() { @@ -37,6 +41,7 @@ describe('Form Rules', () => { let injector: Injector; const customRuleManager = new CustomRuleManager(null); + let formService: FormService; describe('Injection token provided', () => { setupTestBed({ @@ -55,6 +60,7 @@ describe('Form Rules', () => { beforeEach(() => { injector = TestBed.inject(Injector); + formService = TestBed.inject(FormService); }); it('factory function should not return bypass service', () => { @@ -69,9 +75,29 @@ describe('Form Rules', () => { rulesManager.initialize(null); expect(customRuleManager.initialize).toHaveBeenCalled(); }); + + it('should send the form loaded event when initialized', async (done) => { + const rulesManager = new CustomRuleManager(formService); + const getRulesSpy = spyOn(rulesManager, 'getRules').and.returnValue({}); + const formModel = new FormModel({ id: 'mock' }, {}, false); + const formEvent = new FormEvent(formModel); + const event = new FormRulesEvent('formLoaded', formEvent); + + formService.formRulesEvent.subscribe(formRulesEvent => { + expect(formRulesEvent).toEqual(event); + done(); + }); + + rulesManager.initialize(formModel); + expect(getRulesSpy).toHaveBeenCalled(); + }); }); describe('Injection token not provided', () => { + let formModel: FormModel; + let rulesManager: FormRulesManager; + let getRulesSpy: jasmine.Spy; + setupTestBed({ imports: [ TranslateModule.forRoot(), @@ -82,12 +108,28 @@ describe('Form Rules', () => { beforeEach(() => { injector = TestBed.inject(Injector); + rulesManager = formRulesManagerFactory(injector); + getRulesSpy = spyOn(rulesManager, 'getRules'); }); it('factory function should return bypass service', () => { - const rulesManager = formRulesManagerFactory(injector); - expect(rulesManager instanceof ByPassFormRuleManager).toBeTruthy(); }); + + it('should get rules when form is not readonly', () => { + formModel = new FormModel({}, {}, false); + + rulesManager.initialize(formModel); + + expect(getRulesSpy).toHaveBeenCalled(); + }); + + it('should not get rules when form is readonly', () => { + formModel = new FormModel({}, {}, true); + + rulesManager.initialize(formModel); + + expect(getRulesSpy).not.toHaveBeenCalled(); + }); }); }); diff --git a/lib/core/form/models/form-rules.model.ts b/lib/core/form/models/form-rules.model.ts index 32f652405d9..f03d1512c07 100644 --- a/lib/core/form/models/form-rules.model.ts +++ b/lib/core/form/models/form-rules.model.ts @@ -18,6 +18,7 @@ import { InjectionToken, Injector } from '@angular/core'; import { Subject } from 'rxjs'; import { filter, takeUntil } from 'rxjs/operators'; +import { FormEvent } from '../events'; import { FormRulesEvent } from '../events/form-rules.event'; import { FormModel, FormService } from '../public-api'; @@ -45,16 +46,21 @@ export abstract class FormRulesManager { } this.formModel = formModel; - const rules = this.getRules(); - - if (!!rules) { - this.formService.formRulesEvent - .pipe( - filter(event => !!event.form.id && event.form.id === formModel?.id), - takeUntil(this.onDestroy$) - ).subscribe(event => { - this.handleRuleEvent(event, rules); - }); + + if (!this.formModel.readOnly) { + const rules = this.getRules(); + + if (!!rules) { + this.formService.formRulesEvent + .pipe( + filter(event => !!event.form.id && event.form.id === formModel?.id), + takeUntil(this.onDestroy$) + ).subscribe(event => { + this.handleRuleEvent(event, rules); + }); + + this.formService.formRulesEvent.next(new FormRulesEvent('formLoaded', new FormEvent(formModel))); + } } this.initialized = true; diff --git a/lib/core/form/services/form.service.spec.ts b/lib/core/form/services/form.service.spec.ts index 0998eb32f89..2f4afb9f390 100644 --- a/lib/core/form/services/form.service.spec.ts +++ b/lib/core/form/services/form.service.spec.ts @@ -21,9 +21,6 @@ import { FormService } from './form.service'; import { setupTestBed } from '../../testing/setup-test-bed'; import { CoreTestingModule } from '../../testing/core.testing.module'; import { TranslateModule } from '@ngx-translate/core'; -import { FormEvent, ValidateDynamicTableRowEvent, ValidateFormEvent, ValidateFormFieldEvent } from '../events'; -import { take } from 'rxjs/operators'; -import { FormModel } from '../components/widgets/core/form.model'; declare let jasmine: any; @@ -412,68 +409,4 @@ describe('Form service', () => { }); }); }); - - describe('Form rules', () => { - const event = new FormEvent('mock'); - - it('should emit the formLoaded in the formRulesEvent observable', async(done) => { - service.formRulesEvent.pipe(take(1)).subscribe(formRuleEvent => { - expect(formRuleEvent.event).toBeFalsy(); - expect(formRuleEvent.field).toBeFalsy(); - expect(formRuleEvent.form).toEqual('mock'); - expect(formRuleEvent.type).toEqual('formLoaded'); - done(); - }); - - service.formLoaded.next(event); - }); - - it('should emit the formDataRefreshed in the formRulesEvent observable', async(done) => { - service.formRulesEvent.pipe(take(1)).subscribe(formRuleEvent => { - expect(formRuleEvent.event).toBeFalsy(); - expect(formRuleEvent.field).toBeFalsy(); - expect(formRuleEvent.form).toEqual('mock'); - expect(formRuleEvent.type).toEqual('formDataRefreshed'); - done(); - }); - - service.formDataRefreshed.next(event); - }); - - it('should emit the formValidated in the formRulesEvent observable', async(done) => { - service.formRulesEvent.pipe(take(1)).subscribe(formRuleEvent => { - expect(formRuleEvent.event).toBeFalsy(); - expect(formRuleEvent.field).toBeFalsy(); - expect(formRuleEvent.form).toEqual('mock'); - expect(formRuleEvent.type).toEqual('formValidated'); - done(); - }); - - service.validateForm.next(new ValidateFormEvent('mock')); - }); - - it('should emit the fieldValidated in the formRulesEvent observable', async(done) => { - service.formRulesEvent.pipe(take(1)).subscribe(formRuleEvent => { - expect(formRuleEvent.event).toBeFalsy(); - expect(formRuleEvent.field).toBeFalsy(); - expect(formRuleEvent.form).toEqual('mock'); - expect(formRuleEvent.type).toEqual('fieldValidated'); - done(); - }); - - service.validateFormField.next(new ValidateFormFieldEvent('mock', null)); - }); - - it('should emit the fieldDynamicTableRowValidated in the formRulesEvent observable', async(done) => { - service.formRulesEvent.pipe(take(1)).subscribe(formRuleEvent => { - expect(formRuleEvent.event).toBeFalsy(); - expect(formRuleEvent.field).toBeFalsy(); - expect(formRuleEvent.form).toEqual('mock'); - expect(formRuleEvent.type).toEqual('fieldDynamicTableRowValidated'); - done(); - }); - - service.validateDynamicTableRow.next(new ValidateDynamicTableRowEvent('mock' as unknown as FormModel, null, null, null)); - }); - }); }); diff --git a/lib/core/form/services/form.service.ts b/lib/core/form/services/form.service.ts index 49fd879763a..ac86f238ac3 100644 --- a/lib/core/form/services/form.service.ts +++ b/lib/core/form/services/form.service.ts @@ -136,12 +136,6 @@ export class FormService implements FormValidationService { constructor(private ecmModelService: EcmModelService, private apiService: AlfrescoApiService, protected logService: LogService) { - - this.formLoaded.subscribe(event => this.formRulesEvent.next(new FormRulesEvent('formLoaded', event))); - this.formDataRefreshed.subscribe(event => this.formRulesEvent.next(new FormRulesEvent('formDataRefreshed', event))); - this.validateForm.subscribe(event => this.formRulesEvent.next(new FormRulesEvent('formValidated', event))); - this.validateFormField.subscribe(event => this.formRulesEvent.next(new FormRulesEvent('fieldValidated', event))); - this.validateDynamicTableRow.subscribe(event => this.formRulesEvent.next(new FormRulesEvent('fieldDynamicTableRowValidated', event))); } /**