Skip to content

Commit

Permalink
ON-39065 # FORM predicate type in evaluateConditionalPredicate
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanButton committed Apr 16, 2024
1 parent 519e94e commit 6322c43
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## Added

- support for `FORM` predicate type in `evaluateConditionalPredicate`

## [6.0.1] - 2024-03-14

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/conditionalLogicService/evaluateConditionalPredicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormElementsCtrl } from '../types'
import { typeCastService, formElementsService } from '..'
import evaluateConditionalOptionsPredicate from './evaluateConditionalOptionsPredicate'
import { conditionallyShowByPredicate } from './conditionallyShowElement'
import { flattenFormElements } from '../formElementsService'

const fnMap = {
'>': (lhs: number, rhs: number) => lhs > rhs,
Expand Down Expand Up @@ -139,6 +140,30 @@ export default function evaluateConditionalPredicate({
}
return
}
case 'FORM':
if (predicate.predicate) {
if (
!predicateElement ||
predicateElement.type !== 'form' ||
!predicateElement.elements
) {
return
}
const formModel = formElementsCtrl.model[predicateElement.name] as {
[name: string]: unknown
}
return evaluateConditionalPredicate({
predicate: predicate.predicate,
formElementsCtrl: {
flattenedElements: flattenFormElements(predicateElement.elements),
model: formModel,
parentFormElementsCtrl: formElementsCtrl.parentFormElementsCtrl
? formElementsCtrl.parentFormElementsCtrl
: formElementsCtrl,
},
})
}
return
case 'OPTIONS':
default: {
const optionsPredicateElement =
Expand Down
108 changes: 108 additions & 0 deletions tests/conditionalLogicService/evaluateConditionalPredicate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConditionTypes, FormTypes } from '@oneblink/types'
import evaluateConditionalPredicate from '../../src/conditionalLogicService/evaluateConditionalPredicate'
import { flattenFormElements } from '../../src/formElementsService'

describe('evaluateConditionalPredicate', () => {
const predicate: ConditionTypes.ConditionalPredicate = {
Expand Down Expand Up @@ -42,6 +43,47 @@ describe('evaluateConditionalPredicate', () => {
isDataLookup: false,
isElementLookup: false,
}
const nestedPredicate: ConditionTypes.ConditionalPredicate = {
elementId: 'formFormElement',
type: 'FORM',
predicate: predicate,
}

const nestedPredicateElement: FormTypes.FormElement = {
id: 'predicateNumber',
name: 'predicateNumber',
label: 'predicateNumber',
type: 'number',
required: false,
conditionallyShow: true,
conditionallyShowPredicates: [nestedPredicate],
isDataLookup: false,
isElementLookup: false,
isSlider: false,
}
const childFormFormElementPredicate: ConditionTypes.ConditionalPredicate = {
elementId: 'childFormFormElement',
type: 'FORM',
predicate: predicate,
}
const parentFormFormElementPredicate: ConditionTypes.ConditionalPredicate = {
elementId: 'parentFormFormElement',
type: 'FORM',
predicate: childFormFormElementPredicate,
}

const deeplyNestedPredicateElement: FormTypes.FormElement = {
id: 'predicateNumber',
name: 'predicateNumber',
label: 'predicateNumber',
type: 'number',
required: false,
conditionallyShow: true,
conditionallyShowPredicates: [parentFormFormElementPredicate],
isDataLookup: false,
isElementLookup: false,
isSlider: false,
}

test('should show root element', () => {
const isShown = evaluateConditionalPredicate({
Expand Down Expand Up @@ -108,4 +150,70 @@ describe('evaluateConditionalPredicate', () => {
})
expect(isShown).toBe(predicateElement)
})

test('should show element conditionally shown from element within a form element', () => {
const formFormElement: FormTypes.FormFormElement = {
id: 'formFormElement',
formId: 1,
name: 'formFormElement',
type: 'form',
conditionallyShow: false,
elements: [comparisonElement],
}
const isShown = evaluateConditionalPredicate({
predicate: nestedPredicate,
formElementsCtrl: {
flattenedElements: flattenFormElements([
formFormElement,
nestedPredicateElement,
]),
model: {
formFormElement: {
comparisonNumber: 1,
},
predicateNumber: 1,
},
},
})

expect(isShown).toBe(nestedPredicateElement)
})

test('should show element conditionally shown from element within a deeply nested form element', () => {
const childFormFormElement: FormTypes.FormFormElement = {
id: 'childFormFormElement',
formId: 1,
name: 'childFormFormElement',
type: 'form',
conditionallyShow: false,
elements: [comparisonElement],
}
const parentFormFormElement: FormTypes.FormFormElement = {
id: 'parentFormFormElement',
formId: 1,
name: 'parentFormFormElement',
type: 'form',
conditionallyShow: false,
elements: [childFormFormElement],
}
const isShown = evaluateConditionalPredicate({
predicate: parentFormFormElementPredicate,
formElementsCtrl: {
flattenedElements: flattenFormElements([
parentFormFormElement,
deeplyNestedPredicateElement,
]),
model: {
parentFormFormElement: {
childFormFormElement: {
comparisonNumber: 1,
},
},
predicateNumber: 1,
},
},
})

expect(isShown).toBe(deeplyNestedPredicateElement)
})
})

0 comments on commit 6322c43

Please sign in to comment.