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
179 changes: 179 additions & 0 deletions src/process/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@
form: '65ea368b705068f84a93c87a',
};

const errors: any = [];

Check warning on line 902 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
_,
form,
Expand Down Expand Up @@ -1039,7 +1039,7 @@
},
},
};
const errors: any = [];

Check warning on line 1042 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
form,
submission,
Expand Down Expand Up @@ -1174,9 +1174,9 @@
},
};
processSync(context);
assert.equal((context.scope as any).errors.length, 2);

Check warning on line 1177 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
assert.equal((context.scope as any).errors[0].ruleName, 'array');

Check warning on line 1178 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
assert.equal((context.scope as any).errors[1].ruleName, 'required');

Check warning on line 1179 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
});

it('Sets a value based on advanced conditions', async function () {
Expand Down Expand Up @@ -1275,7 +1275,7 @@
const submission = {
data: { test: '1' },
};
const context: any = {

Check warning on line 1278 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
form,
submission,
data: submission.data,
Expand Down Expand Up @@ -1543,7 +1543,7 @@
form: '65e8786fc5dacf667eef12fc',
};

const errors: any = [];

Check warning on line 1546 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
form,
submission,
Expand Down Expand Up @@ -7147,5 +7147,184 @@
assert(!context.data.hasOwnProperty('fname'));
assert(!context.data.hasOwnProperty('lname'));
});

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: Processors,
scope: {},
};
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: Processors,
scope: {},
};
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: Processors,
scope: {},
};
processSync(context as any);
assert.equal((context.scope as any).errors.length, 1);
});
});
});
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;
};
18 changes: 15 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 'utils/utils';
import { setComponentScope } from 'utils/formUtil';

Expand All @@ -30,6 +30,10 @@ 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 @@ -139,6 +143,7 @@ export function setValueProperty(context: LogicContext, action: LogicActionValue
const oldValue = get(data, path);
const newValue = evaluate(action.value, context, 'value', false, (evalContext: any) => {
evalContext.value = clone(oldValue);
evalContext.result = context.result;
});
if (
!isEqual(oldValue, newValue) &&
Expand Down Expand Up @@ -167,6 +172,7 @@ export function setMergeComponentSchema(
false,
(evalContext: any) => {
evalContext.value = clone(oldValue);
evalContext.result = context.result;
},
);
const merged = assign({}, component, schema);
Expand All @@ -189,10 +195,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 @@ -212,5 +221,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