Skip to content

Commit

Permalink
Fix reules event filtering when no form is provided (#7592)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinezga committed Apr 22, 2022
1 parent 039f167 commit 6174f8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 23 additions & 6 deletions lib/core/form/models/form-rules.model.spec.ts
Expand Up @@ -26,6 +26,7 @@ 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';
import { getTestScheduler } from 'jasmine-marbles';

class CustomRuleManager extends FormRulesManager<any> {
protected getRules() {
Expand Down Expand Up @@ -76,20 +77,36 @@ describe('Form Rules', () => {
expect(customRuleManager.initialize).toHaveBeenCalled();
});

it('should send the form loaded event when initialized', async (done) => {
it('should send the form loaded event when initialized', () => {
const rulesManager = new CustomRuleManager(formService);
const getRulesSpy = spyOn<any>(rulesManager, 'getRules').and.returnValue({});
const handleRuleEventSpy = spyOn<any>(rulesManager, 'handleRuleEvent');
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);
getTestScheduler().flush();

expect(getRulesSpy).toHaveBeenCalled();
expect(handleRuleEventSpy).toHaveBeenCalledWith(event, {});
});

it('should not receive the form event when event has no form', () => {
const rulesManager = new CustomRuleManager(formService);
spyOn<any>(rulesManager, 'getRules').and.returnValue({});
const handleRuleEventSpy = spyOn<any>(rulesManager, 'handleRuleEvent');
const formModel = new FormModel({ id: 'mock' }, {}, false);
const formEvent = new FormEvent(new FormModel(null));
const event = new FormRulesEvent('formLoaded', formEvent);

rulesManager.initialize(formModel);

formService.formRulesEvent.next(event);

getTestScheduler().flush();

expect(handleRuleEventSpy).not.toHaveBeenCalledWith(event, jasmine.anything());
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/core/form/models/form-rules.model.ts
Expand Up @@ -53,7 +53,7 @@ export abstract class FormRulesManager<T> {
if (!!rules) {
this.formService.formRulesEvent
.pipe(
filter(event => !!event.form.id && event.form.id === formModel?.id),
filter(event => !!event?.form?.id && event.form.id === formModel?.id),
takeUntil(this.onDestroy$)
).subscribe(event => {
this.handleRuleEvent(event, rules);
Expand Down

0 comments on commit 6174f8e

Please sign in to comment.