Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions src/process/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5106,6 +5106,191 @@ describe('Process Tests', function () {
processSync(context);
expect((context.scope as ValidationScope).errors).to.have.length(1);
});

it('Should not return the error for required component with logic where result var is used', async function () {
const form = {
components: [
{
label: 'Registration number required',
applyMaskOn: 'change',
tableView: true,
case: 'uppercase',
validate: {
required: true,
maxLength: 50,
},
validateWhenHidden: false,
key: 'plateNumber2',
logic: [
{
name: 'keep letter and number',
trigger: {
type: 'javascript',
javascript: 'result=row[component.key];',
},
actions: [
{
name: 'set',
type: 'value',
value: "value=result.replace(/[^a-zA-Z0-9]/g, '');\n",
},
],
},
],
type: 'textfield',
input: true,
keyModified: true,
},
{
label: 'Submit',
showValidations: false,
tableView: false,
key: 'submit',
type: 'button',
input: true,
},
],
};
const submission = {
data: { plateNumber2: 'TEST', submit: true },
};
const context = {
form,
submission,
data: submission.data,
components: form.components,
processors: ProcessTargets.submission,
scope: {},
};
processSync(context as any);
context.processors = ProcessTargets.evaluator;
processSync(context as any);
assert.equal(context.data.plateNumber2, 'TEST');
assert.equal((context.scope as any).errors.length, 0);
});

it('Should return the value formatted by logic where result var is used', async function () {
const form = {
components: [
{
label: 'Registration number required',
applyMaskOn: 'change',
tableView: true,
case: 'uppercase',
validate: {
required: true,
maxLength: 50,
},
validateWhenHidden: false,
key: 'plateNumber2',
logic: [
{
name: 'keep letter and number',
trigger: {
type: 'javascript',
javascript: 'result=row[component.key];',
},
actions: [
{
name: 'set',
type: 'value',
value: "value=result.replace(/[^a-zA-Z0-9]/g, '');\n",
},
],
},
],
type: 'textfield',
input: true,
keyModified: true,
},
{
label: 'Submit',
showValidations: false,
tableView: false,
key: 'submit',
type: 'button',
input: true,
},
],
};
const submission = {
data: { plateNumber2: 'TEST 123 TEST', submit: true },
};
const context = {
form,
submission,
data: submission.data,
components: form.components,
processors: ProcessTargets.submission,
scope: {},
};
processSync(context as any);
context.processors = ProcessTargets.evaluator;
processSync(context as any);
assert.equal(context.data.plateNumber2, 'TEST123TEST');
assert.equal((context.scope as any).errors.length, 0);
});

it('Should return the error for required component with logic where result var is used', async function () {
const form = {
components: [
{
label: 'Registration number required',
applyMaskOn: 'change',
tableView: true,
case: 'uppercase',
validate: {
required: true,
maxLength: 50,
},
validateWhenHidden: false,
key: 'plateNumber2',
logic: [
{
name: 'keep letter and number',
trigger: {
type: 'javascript',
javascript: 'result=row[component.key];',
},
actions: [
{
name: 'set',
type: 'value',
value: "value=result.replace(/[^a-zA-Z0-9]/g, '');\n",
},
],
},
],
type: 'textfield',
input: true,
keyModified: true,
},
{
label: 'Submit',
showValidations: false,
tableView: false,
key: 'submit',
type: 'button',
input: true,
},
],
};
const submission = {
data: { plateNumber2: '', submit: true },
};
const context = {
form,
submission,
data: submission.data,
components: form.components,
processors: ProcessTargets.submission,
scope: {},
};
processSync(context as any);
context.processors = ProcessTargets.evaluator;
processSync(context as any);
assert.equal((context.scope as any).errors.length, 1);
});
});
});

Expand Down
1 change: 1 addition & 0 deletions src/types/process/logic/LogicContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { ProcessorContext } from '../ProcessorContext';
import { LogicScope } from './LogicScope';
export type LogicContext = ProcessorContext<LogicScope> & {
populated?: any;
result?: any;
};
17 changes: 14 additions & 3 deletions src/utils/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
LogicActionPropertyString,
LogicActionValue,
} from 'types/AdvancedLogic';
import { get, set, clone, isEqual, assign } from 'lodash';
import { get, set, clone, isEqual, assign, unset } from 'lodash';
import { evaluate, interpolate } from 'modules/jsonlogic';
import { registerEphermalState } from './utils';
import { getComponentAbsolutePath } from './formUtil';
Expand All @@ -31,6 +31,9 @@ export const hasLogic = (context: LogicContext): boolean => {

export const checkTrigger = (context: LogicContext, trigger: any): boolean => {
let shouldTrigger: boolean | null = false;
if (!trigger) {
return false;
}
switch (trigger.type) {
case 'simple':
if (isLegacyConditional(trigger.simple)) {
Expand Down Expand Up @@ -136,6 +139,7 @@ export function setValueProperty(context: LogicContext, action: LogicActionValue
const oldValue = get(data, path);
const newValue = evaluate(context, action.value, 'value', (evalContext: any) => {
evalContext.value = clone(oldValue);
evalContext.result = context.result;
});
if (
!isEqual(oldValue, newValue) &&
Expand All @@ -159,6 +163,7 @@ export function setMergeComponentSchema(
'schema',
(evalContext: any) => {
evalContext.value = clone(oldValue);
evalContext.result = context.result;
},
);
const merged = assign({}, component, schema);
Expand All @@ -181,10 +186,13 @@ export const applyActions = (context: LogicContext): boolean => {
}
return logic.reduce((changed, logicItem) => {
const { actions, trigger } = logicItem;
if (!trigger || !actions || !actions.length || !checkTrigger(context, trigger)) {
const result = checkTrigger(context, trigger);
if (!trigger || !actions || !actions.length || !result) {
return changed;
}
return actions.reduce((changed, action) => {
// remove trigger result of current logic block to the evaluation context
context.result = result;
const actionsResult = actions.reduce((changed, action) => {
switch (action.type) {
case 'property':
if (setActionProperty(context, action)) {
Expand All @@ -204,5 +212,8 @@ export const applyActions = (context: LogicContext): boolean => {
return changed;
}
}, changed);
// remove result of current logic block from context
unset(context, 'result');
return actionsResult;
}, false);
};
Loading