PromucFlow_constructor/app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts

258 lines
7.1 KiB
TypeScript
Raw Normal View History

import type { FlattenedWidgetProps } from "WidgetProvider/constants";
import {
accessNestedObjectValue,
handleImageWidgetWhenPasting,
handleJSONFormWidgetWhenPasting,
handleTextWidgetWhenPasting,
handleJSONFormPropertiesListedInDynamicBindingPath,
fix: Handles button binding during buildingblock pasting (#33674) ## Description This pull request adds the handleButtonDynamicTriggerPathList function to the BuildingBlockAdditionSagas.ts file. This function is responsible for handling the dynamic trigger path list for button widgets. It iterates over the widgetNameMap and updates the dynamicTriggerPathList of each button widget with the newWidgetName. Additionally, unit tests have been added to ensure the correct functionality of the handleButtonDynamicTriggerPathList function. This change improves the functionality of button widgets when pasting building block widgets. Fixes #33658 _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Widget" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/9268894716> > Commit: a675a5c8a752fea76c130e8bdc1b76e44453ee98 > Cypress dashboard url: <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9268894716&attempt=1" target="_blank">Click here!</a> <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No --------- Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
2024-05-29 04:09:39 +00:00
handleButtonDynamicTriggerPathList,
} from ".";
import {
widget,
expectedImageUpdate,
widget2,
emptywidget,
expectedTextUpdate,
expectedSourceDataUpdate,
} from "./PasteWidgetUtils.fixture";
const widgetNameMap = {
table1: "table1Copy",
};
fix: Handles button binding during buildingblock pasting (#33674) ## Description This pull request adds the handleButtonDynamicTriggerPathList function to the BuildingBlockAdditionSagas.ts file. This function is responsible for handling the dynamic trigger path list for button widgets. It iterates over the widgetNameMap and updates the dynamicTriggerPathList of each button widget with the newWidgetName. Additionally, unit tests have been added to ensure the correct functionality of the handleButtonDynamicTriggerPathList function. This change improves the functionality of button widgets when pasting building block widgets. Fixes #33658 _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Widget" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/9268894716> > Commit: a675a5c8a752fea76c130e8bdc1b76e44453ee98 > Cypress dashboard url: <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9268894716&attempt=1" target="_blank">Click here!</a> <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No --------- Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
2024-05-29 04:09:39 +00:00
import { klona } from "klona";
function testIndividualWidgetPasting(
widgetNameMap: Record<string, string>,
widget: FlattenedWidgetProps,
handler: (
widgetNameMap: Record<string, string>,
widget: FlattenedWidgetProps,
) => void,
expectedWidget: FlattenedWidgetProps,
) {
handler(widgetNameMap, widget);
expect(widget).toEqual(expectedWidget);
}
describe("handleImageWidgetWhenPasting", () => {
it("1. replaces old widget names with new widget names in the image property", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget } as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
expectedImageUpdate as any as FlattenedWidgetProps,
);
});
it("2. does not replace anything if the image property does not contain old widget names", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget2 } as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
widget2 as any as FlattenedWidgetProps,
);
});
it("3. handles empty widget name map", () => {
testIndividualWidgetPasting(
{},
widget as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
widget as any as FlattenedWidgetProps,
);
});
it("4. handles empty image property", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...emptywidget } as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
emptywidget as any as FlattenedWidgetProps,
);
});
});
describe("handleTextWidgetWhenPasting", () => {
it("1. should replace old widget names with new widget names in the widget text", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget } as any as FlattenedWidgetProps,
handleTextWidgetWhenPasting,
expectedTextUpdate as any as FlattenedWidgetProps,
);
});
it("2. should not modify the widget text if there are no old widget names", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget2 } as any as FlattenedWidgetProps,
handleTextWidgetWhenPasting,
widget2 as any as FlattenedWidgetProps,
);
});
it("3. should not modify the widget text if the widget name map is empty", () => {
testIndividualWidgetPasting(
{},
widget as any as FlattenedWidgetProps,
handleTextWidgetWhenPasting,
widget as any as FlattenedWidgetProps,
);
});
it("4. handles empty text property", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...emptywidget } as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
emptywidget as any as FlattenedWidgetProps,
);
});
});
describe("handleJSONFormWidgetWhenPasting", () => {
it("should replace the old widget name with the new widget name in the source data", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget } as any as FlattenedWidgetProps,
handleJSONFormWidgetWhenPasting,
expectedSourceDataUpdate as any as FlattenedWidgetProps,
);
});
it("should not replace anything if the old widget name is not present in the source data", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...widget2 } as any as FlattenedWidgetProps,
handleJSONFormWidgetWhenPasting,
widget2 as any as FlattenedWidgetProps,
);
});
it("4. handles empty sourceData property", () => {
testIndividualWidgetPasting(
widgetNameMap,
{ ...emptywidget } as any as FlattenedWidgetProps,
handleImageWidgetWhenPasting,
emptywidget as any as FlattenedWidgetProps,
);
});
});
describe("accessNestedObjectValue", () => {
it("1. should replace the old value with the new value in a nested object", () => {
const obj = {
foo: {
bar: {
baz: "oldValue",
},
},
};
const oldValue = "oldValue";
const newValue = "newValue";
accessNestedObjectValue(obj, "foo.bar.baz", oldValue, newValue);
expect(obj.foo.bar.baz).toEqual(newValue);
});
it("2. should not replace the value if it does not match the old value", () => {
const obj = {
foo: {
bar: {
baz: "value",
},
},
};
const oldValue = "oldValue";
const newValue = "newValue";
accessNestedObjectValue(obj, "foo.bar.baz", oldValue, newValue);
expect(obj.foo.bar.baz).toEqual("value");
});
it("3. should return undefined if the path does not exist in the object", () => {
const obj = {
foo: {
bar: {
baz: "value",
},
},
};
const oldValue = "oldValue";
const newValue = "newValue";
const result = accessNestedObjectValue(
obj,
"foo.bar.qux",
oldValue,
newValue,
);
expect(result).toBeUndefined();
});
});
describe("handleJSONFormPropertiesListedInDynamicBindingPath", () => {
it("1. should replace the oldName with the newName in the dynamicBindingPathList of the widget", () => {
const widget = {
dynamicBindingPathList: [
{ key: "schema.__rootSchema__.property1" },
{ key: "defaultValue" },
{ key: "property3" },
],
defaultValue: "{{oldName.val}}",
schema: {
__rootSchema__: {
property1: "{{oldName.test}}",
},
},
};
const oldName = "oldName";
const newName = "newName";
handleJSONFormPropertiesListedInDynamicBindingPath(
widget as any as FlattenedWidgetProps,
oldName,
newName,
);
expect(widget.schema.__rootSchema__.property1).toEqual("{{newName.test}}");
expect(widget.defaultValue).toEqual("{{newName.val}}");
expect(widget.dynamicBindingPathList).toEqual([
{ key: "schema.__rootSchema__.property1" },
{ key: "defaultValue" },
{ key: "property3" },
]);
});
});
fix: Handles button binding during buildingblock pasting (#33674) ## Description This pull request adds the handleButtonDynamicTriggerPathList function to the BuildingBlockAdditionSagas.ts file. This function is responsible for handling the dynamic trigger path list for button widgets. It iterates over the widgetNameMap and updates the dynamicTriggerPathList of each button widget with the newWidgetName. Additionally, unit tests have been added to ensure the correct functionality of the handleButtonDynamicTriggerPathList function. This change improves the functionality of button widgets when pasting building block widgets. Fixes #33658 _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Widget" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/9268894716> > Commit: a675a5c8a752fea76c130e8bdc1b76e44453ee98 > Cypress dashboard url: <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=9268894716&attempt=1" target="_blank">Click here!</a> <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No --------- Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
2024-05-29 04:09:39 +00:00
describe("handleButtonDynamicTriggerPathList", () => {
const widget = {
dynamicTriggerPathList: [{ key: "onClick" }],
onClick: "{{oldName.val}}",
} as any as FlattenedWidgetProps;
it("1. should replace old widget names with new widget names in dynamic trigger paths", () => {
const widgetNameMap = {
oldName: "newName",
};
const button = klona(widget);
handleButtonDynamicTriggerPathList(widgetNameMap, button);
expect(button.onClick).toEqual("{{newName.val}}");
});
it("2. should do nothing if the widgetNameMap does not contain names in dynamic trigger paths", () => {
const widgetNameMap = {
oldWidget1: "newWidget1",
};
const button = klona(widget);
handleButtonDynamicTriggerPathList(widgetNameMap, button);
expect(button.onClick).toEqual("{{oldName.val}}");
});
});