Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kie-issues#1376: The width information are not being deleted from DMN when the expression is deleted in the new Boxed Expression Editor #2602

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions packages/dmn-editor/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { config, babelTransform, typescriptTransform } = require("@kie-tools/jest-base/jest.config");

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...config,
testEnvironment: "node",
transform: {
...babelTransform,
...typescriptTransform,
},
};
10 changes: 9 additions & 1 deletion packages/dmn-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
],
"scripts": {
"build:dev": "rimraf dist && pnpm copy:css && tsc -p tsconfig.json",
"build:prod": "rimraf dist && pnpm copy:css && pnpm lint && tsc -p tsconfig.json && pnpm test-e2e",
"build:prod": "rimraf dist && pnpm copy:css && pnpm lint && tsc -p tsconfig.json && pnpm test-e2e && pnpm test",
"build:storybook": "storybook build -o dist-storybook",
"copy:css": "copyfiles -u 1 \"src/**/*.{sass,scss,css}\" dist/",
"lint": "run-script-if --bool \"$(build-env linters.run)\" --then \"kie-tools--eslint ./src\"",
"start": "run-script-os",
"start:linux:darwin": "cross-env STORYBOOK_PORT=$(build-env dmnEditor.storybook.port) pnpm kie-tools--storybook --storybookArgs=\"dev --no-open\"",
"start:win32": "pnpm powershell \"cross-env STORYBOOK_PORT=$(build-env dmnEditor.storybook.port) pnpm kie-tools--storybook --storybookArgs='dev --no-open'",
"test": "run-script-if --ignore-errors \"$(build-env tests.ignoreFailures)\" --bool \"$(build-env tests.run)\" --then \"jest --silent --verbose --passWithNoTests\"",
"test-e2e": "run-script-if --ignore-errors \"$(build-env endToEndTests.ignoreFailures)\" --bool \"$(build-env endToEndTests.run)\" --then \"pnpm rimraf ./dist-tests-e2e\" \"pnpm test-e2e:run\"",
"test-e2e:open": "pnpm exec playwright show-report dist-tests-e2e/reports",
"test-e2e:run": "pnpm exec playwright test"
Expand Down Expand Up @@ -65,6 +66,7 @@
"@babel/preset-typescript": "^7.22.5",
"@kie-tools-core/webpack-base": "workspace:*",
"@kie-tools/eslint": "workspace:*",
"@kie-tools/jest-base": "workspace:*",
"@kie-tools/playwright-base": "workspace:*",
"@kie-tools/root-env": "workspace:*",
"@kie-tools/storybook-base": "workspace:*",
Expand All @@ -79,6 +81,8 @@
"@storybook/react-webpack5": "^7.3.2",
"@types/d3-drag": "^3.0.3",
"@types/d3-selection": "^3.0.6",
"@types/jest": "^29.5.12",
"@types/jest-when": "^3.5.5",
"@types/lodash": "^4.14.168",
"@types/node": "^20.14.2",
"@types/react": "^17.0.6",
Expand All @@ -89,11 +93,15 @@
"cross-env": "^7.0.3",
"deep-object-diff": "^1.1.9",
"file-loader": "^6.2.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"jest-when": "^3.6.0",
"lodash": "^4.17.21",
"rimraf": "^3.0.2",
"run-script-os": "^1.1.6",
"start-server-and-test": "^2.0.3",
"storybook": "^7.3.2",
"ts-jest": "^29.1.5",
"typescript": "^5.5.3",
"webpack": "^5.94.0",
"webpack-cli": "^4.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export function BoxedExpressionScreen({ container }: { container: React.RefObjec
const dmnEditorStoreApi = useDmnEditorStoreApi();

const thisDmn = useDmnEditorStore((s) => s.dmn);
const diagram = useDmnEditorStore((s) => s.diagram);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaning. This constant wast not being used.


const activeDrgElementId = useDmnEditorStore((s) => s.boxedExpressionEditor.activeDrgElementId);
const isPropertiesPanelOpen = useDmnEditorStore((s) => s.boxedExpressionEditor.propertiesPanel.isOpen);
Expand Down
15 changes: 2 additions & 13 deletions packages/dmn-editor/src/mutations/updateExpressionWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,9 @@ export function updateExpressionWidths({
drdIndex: number;
widthsById: Map<string, number[]>;
}): void {
const { widthsExtension, widths } = addOrGetDrd({ definitions, drdIndex });
const componentWidthsMap = widths.reduce(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that we were always reloading all the widths from the definitions into the widthsById.

They are loaded the first time here:

And then the Boxed Expression Editor is who changes the widthsById.

The updateExpressionWidth should only update the definitions from widthsById, not fill the widthsById.

(acc, e) =>
e["@_dmnElementRef"]
? acc.set(
e["@_dmnElementRef"],
(e["kie:width"] ?? []).map((vv) => vv.__$$text)
)
: acc,
new Map<string, number[]>()
);
const { widthsExtension } = addOrGetDrd({ definitions, drdIndex });

widthsById.forEach((v, k) => componentWidthsMap.set(k, v));
widthsExtension["kie:ComponentWidths"] = [...componentWidthsMap.entries()].map(([k, v]) => ({
widthsExtension["kie:ComponentWidths"] = [...widthsById.entries()].map(([k, v]) => ({
"@_dmnElementRef": k,
"kie:width": v.map((vv) => ({ __$$text: vv })),
}));
Expand Down
242 changes: 242 additions & 0 deletions packages/dmn-editor/tests/mutations/updateExpressionWidth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Normalized } from "@kie-tools/dmn-editor/dist/normalization/normalize";
import { DMN15__tDefinitions } from "@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
import { generateUuid } from "@kie-tools/boxed-expression-component/dist/api";
import { KIE__tComponentsWidthsExtension } from "@kie-tools/dmn-marshaller/dist/schemas/kie-1_0/ts-gen/types";
import { updateExpressionWidths } from "@kie-tools/dmn-editor/dist/mutations/updateExpressionWidths";

describe("updateExpressionWidth", () => {
const drdIndex = 0;

test("when a definition is deleted from widthsFromId map, it should also be deleted from definitions", () => {
const widthDefinitionsForElementA = { dmnElementRef: "a", widths: [10, 150, 110] };
const widthDefinitionsForElementB = { dmnElementRef: "b", widths: [20, 250, 210] };
const widthDefinitionsForElementC = { dmnElementRef: "c", widths: [30, 350, 310] };
const widthDefinitionsForElementD = { dmnElementRef: "d", widths: [40, 450, 410] };

const definitions = createDefinitionsWithComponentWidths([
widthDefinitionsForElementA,
widthDefinitionsForElementB,
widthDefinitionsForElementC,
widthDefinitionsForElementD,
]);

const widthsById: Map<string, number[]> = new Map([
[widthDefinitionsForElementA.dmnElementRef, widthDefinitionsForElementA.widths],
[widthDefinitionsForElementB.dmnElementRef, widthDefinitionsForElementB.widths],
[widthDefinitionsForElementC.dmnElementRef, widthDefinitionsForElementC.widths],
]);

updateExpressionWidths({ definitions, drdIndex, widthsById });

expect(
definitionContainsWidthExtension({
widthDefinition: widthDefinitionsForElementA,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: widthDefinitionsForElementB,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: widthDefinitionsForElementC,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: widthDefinitionsForElementD,
definitions: definitions,
})
).toBeFalsy();
});

test("when a definition is changed in widthsFromId map, it should be changed in definitions", () => {
const originalWidthDefinitionsForElementA = { dmnElementRef: "a", widths: [1, 1, 1] };
const originalWidthDefinitionsForElementB = { dmnElementRef: "b", widths: [2, 2, 2] };
const originalWidthDefinitionsForElementC = { dmnElementRef: "c", widths: [3, 3, 3] };

const changedWidthDefinitionsForElementA = { dmnElementRef: "a", widths: [11, 11, 11, 11, 11] };
const changedWidthDefinitionsForElementB = { dmnElementRef: "b", widths: [20, 20, 20] };
const changedWidthDefinitionsForElementC = { dmnElementRef: "c", widths: [3, 3] };

const definitions = createDefinitionsWithComponentWidths([
originalWidthDefinitionsForElementA,
originalWidthDefinitionsForElementB,
originalWidthDefinitionsForElementC,
]);

const widthsById: Map<string, number[]> = new Map([
[changedWidthDefinitionsForElementA.dmnElementRef, changedWidthDefinitionsForElementA.widths],
[changedWidthDefinitionsForElementB.dmnElementRef, changedWidthDefinitionsForElementB.widths],
[changedWidthDefinitionsForElementC.dmnElementRef, changedWidthDefinitionsForElementC.widths],
]);

updateExpressionWidths({ definitions, drdIndex, widthsById });

expect(
definitionContainsWidthExtension({
widthDefinition: originalWidthDefinitionsForElementA,
definitions: definitions,
})
).toBeFalsy();

expect(
definitionContainsWidthExtension({
widthDefinition: originalWidthDefinitionsForElementB,
definitions: definitions,
})
).toBeFalsy();

expect(
definitionContainsWidthExtension({
widthDefinition: originalWidthDefinitionsForElementC,
definitions: definitions,
})
).toBeFalsy();

expect(
definitionContainsWidthExtension({
widthDefinition: changedWidthDefinitionsForElementA,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: changedWidthDefinitionsForElementB,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: changedWidthDefinitionsForElementC,
definitions: definitions,
})
).toBeTruthy();
});

test("when a definition is added in widthsFromId map, it should also be added in definitions", () => {
const originalWidthDefinitionsForElementA = { dmnElementRef: "a", widths: [1, 1, 1] };
const originalWidthDefinitionsForElementB = { dmnElementRef: "b", widths: [2, 2, 2] };

const newWidthDefinition = { dmnElementRef: "c", widths: [3, 3] };

const definitions = createDefinitionsWithComponentWidths([
originalWidthDefinitionsForElementA,
originalWidthDefinitionsForElementB,
]);

const widthsById: Map<string, number[]> = new Map([
[originalWidthDefinitionsForElementA.dmnElementRef, originalWidthDefinitionsForElementA.widths],
[originalWidthDefinitionsForElementB.dmnElementRef, originalWidthDefinitionsForElementB.widths],
[newWidthDefinition.dmnElementRef, newWidthDefinition.widths],
]);

updateExpressionWidths({ definitions, drdIndex, widthsById });

expect(
definitionContainsWidthExtension({
widthDefinition: originalWidthDefinitionsForElementA,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: originalWidthDefinitionsForElementB,
definitions: definitions,
})
).toBeTruthy();

expect(
definitionContainsWidthExtension({
widthDefinition: newWidthDefinition,
definitions: definitions,
})
).toBeTruthy();
});
});

function definitionContainsWidthExtension(args: {
widthDefinition: { dmnElementRef: string; widths: number[] };
definitions: Normalized<DMN15__tDefinitions>;
}) {
const componentsWidthExtension =
args.definitions?.["dmndi:DMNDI"]?.["dmndi:DMNDiagram"]?.[0]["di:extension"]?.["kie:ComponentsWidthsExtension"];

const element = {
"@_dmnElementRef": args.widthDefinition.dmnElementRef,
"kie:width": args.widthDefinition.widths.map((w) => {
return { __$$text: w };
}),
};

return componentsWidthExtension?.["kie:ComponentWidths"]?.find(
(e) =>
e["@_dmnElementRef"] === element["@_dmnElementRef"] &&
e["kie:width"]?.length === element["kie:width"].length &&
e["kie:width"]?.every((val, index) => val.__$$text === element["kie:width"]?.[index].__$$text)
);
}

function createDefinitionsWithComponentWidths(widthDefinition: { dmnElementRef: string; widths: number[] }[]) {
// Do not inline this variable for type safety. See https://github.com/microsoft/TypeScript/issues/241
const definitions: Normalized<DMN15__tDefinitions> = {
"@_namespace": "https://kie.org/dmn/_982CA10C-B1B9-495C-9CFC-98FAA801BB50",
"@_id": generateUuid(),
"@_name": "my definitions",
"dmndi:DMNDI": {
"dmndi:DMNDiagram": [
{
"@_id": generateUuid(),
"di:extension": createComponentWidthExtension(widthDefinition),
},
],
},
};
return definitions;
}

function createComponentWidthExtension(widthDefinition: { dmnElementRef: string; widths: number[] }[]) {
// Do not inline this variable for type safety. See https://github.com/microsoft/TypeScript/issues/241
const componentsWidthsExtension: KIE__tComponentsWidthsExtension = {
ComponentWidths: [
...widthDefinition.map((w) => {
return {
"@_dmnElementRef": w.dmnElementRef,
width: w.widths.map((w) => {
return { __$$text: w };
}),
};
}),
],
};
return componentsWidthsExtension;
}
3 changes: 3 additions & 0 deletions packages/dmn-editor/tsconfig.tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
Loading