From 4bebb526d0000aea58f457cb343ef518585e1706 Mon Sep 17 00:00:00 2001 From: Valentin Serra Date: Tue, 16 Apr 2024 11:48:13 +0200 Subject: [PATCH] chore: add expression field test coverage Related to #1151 --- .../form-js-viewer/test/spec/Form.spec.js | 41 ++++++++++++++++++- .../test/spec/chain-expressions.json | 41 +++++++++++++++++++ .../test/spec/cyclical-expressions.json | 27 ++++++++++++ .../render/components/helper/mocks/index.js | 11 ++++- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 packages/form-js-viewer/test/spec/chain-expressions.json create mode 100644 packages/form-js-viewer/test/spec/cyclical-expressions.json diff --git a/packages/form-js-viewer/test/spec/Form.spec.js b/packages/form-js-viewer/test/spec/Form.spec.js index cafa3ae2d..83f59516c 100644 --- a/packages/form-js-viewer/test/spec/Form.spec.js +++ b/packages/form-js-viewer/test/spec/Form.spec.js @@ -1,4 +1,4 @@ -import { act, fireEvent, screen } from '@testing-library/preact/pure'; +import { act, fireEvent, screen, waitFor } from '@testing-library/preact/pure'; import { createForm, Form, schemaVersion } from '../../src'; @@ -12,6 +12,8 @@ import conditionErrorsDynamicListSchema from './condition-errors-dynamic-list.js import dynamicListVariablesSchema from './dynamic-list-variables.json'; import dynamicListTableFilterInteractionSchema from './dynamic-list-table-filter-interaction.json'; import complexExpressionsSchema from './complex-expressions.json'; +import cyclicalExpressionsSchema from './cyclical-expressions.json'; +import chainExpressionsSchema from './chain-expressions.json'; import hiddenFieldsConditionalSchema from './hidden-fields-conditional.json'; import hiddenFieldsExpressionSchema from './hidden-fields-expression.json'; import disabledSchema from './disabled.json'; @@ -1541,6 +1543,43 @@ describe('Form', function () { // then expect(container.querySelectorAll('.fjs-layout-row')).to.have.length(5); }); + + it('should render cyclical expressions', async function () { + const initialData = { + exprA: 2, + exprB: 3, + }; + + // given + await bootstrapForm({ + initialData, + container, + schema: cyclicalExpressionsSchema, + }); + + // then + expect(container.querySelectorAll('.fjs-layout-row')).to.have.length(2); + }); + + it('should render chain expressions', async function () { + // given + await bootstrapForm({ + container, + schema: chainExpressionsSchema, + }); + + // then + expect(container.querySelectorAll('.fjs-layout-row')).to.have.length(4); + + await waitFor(() => { + expect(form._getState().data).to.eql({ + staticValue: 10, + exprA: 20, + exprB: 40, + exprC: 50, + }); + }); + }); }); describe('integration - hidden fields', function () { diff --git a/packages/form-js-viewer/test/spec/chain-expressions.json b/packages/form-js-viewer/test/spec/chain-expressions.json new file mode 100644 index 000000000..bc07a731f --- /dev/null +++ b/packages/form-js-viewer/test/spec/chain-expressions.json @@ -0,0 +1,41 @@ +{ + "components": [ + { + "computeOn": "change", + "type": "expression", + "id": "Field_staticValue", + "key": "staticValue", + "expression": "=10" + }, + { + "computeOn": "change", + "type": "expression", + "id": "Field_exprA", + "key": "exprA", + "expression": "=staticValue + 10" + }, + { + "computeOn": "change", + "type": "expression", + "id": "Field_exprB", + "key": "exprB", + "expression": "=exprA * 2" + }, + { + "computeOn": "change", + "type": "expression", + "id": "Field_exprC", + "key": "exprC", + "expression": "=exprB + 10" + } + ], + "type": "default", + "id": "Form_non_cyclic_example", + "executionPlatform": "Camunda Cloud", + "executionPlatformVersion": "8.5.0", + "exporter": { + "name": "Camunda Modeler", + "version": "5.22.0" + }, + "schemaVersion": 16 +} diff --git a/packages/form-js-viewer/test/spec/cyclical-expressions.json b/packages/form-js-viewer/test/spec/cyclical-expressions.json new file mode 100644 index 000000000..da70fb97f --- /dev/null +++ b/packages/form-js-viewer/test/spec/cyclical-expressions.json @@ -0,0 +1,27 @@ +{ + "components": [ + { + "computeOn": "change", + "type": "expression", + "id": "Field_exprA", + "key": "exprA", + "expression": "=exprB + 10" + }, + { + "computeOn": "change", + "type": "expression", + "id": "Field_exprB", + "key": "exprB", + "expression": "=exprA * 2" + } + ], + "type": "default", + "id": "Form_cyclic_example", + "executionPlatform": "Camunda Cloud", + "executionPlatformVersion": "8.5.0", + "exporter": { + "name": "Camunda Modeler", + "version": "5.22.0" + }, + "schemaVersion": 16 +} diff --git a/packages/form-js-viewer/test/spec/render/components/helper/mocks/index.js b/packages/form-js-viewer/test/spec/render/components/helper/mocks/index.js index 753880fc5..0d26457f0 100644 --- a/packages/form-js-viewer/test/spec/render/components/helper/mocks/index.js +++ b/packages/form-js-viewer/test/spec/render/components/helper/mocks/index.js @@ -1,5 +1,11 @@ import { Injector } from 'didi'; -import { MarkdownRenderer, FeelersTemplating, FormFields, RepeatRenderManager } from '../../../../../../src'; +import { + MarkdownRenderer, + FeelersTemplating, + FormFields, + RepeatRenderManager, + ExpressionLoopPreventer, +} from '../../../../../../src'; export function createMockInjector(services = {}, options = {}) { const injector = new Injector([_createMockModule(services, options)]); @@ -29,6 +35,9 @@ function _createMockModule(services, options) { // using actual implementations in testing formFields: services.formFields ? ['value', services.formFields] : ['type', FormFields], templating: services.templating ? ['value', services.templating] : ['type', FeelersTemplating], + expressionLoopPreventer: services.expressionLoopPreventer + ? ['value', services.expressionLoopPreventer] + : ['type', ExpressionLoopPreventer], repeatRenderManager: services.repeatRenderManager ? ['value', services.repeatRenderManager] : ['type', RepeatRenderManager],