PromucFlow_constructor/app/client/src/utils/DynamicBindingsUtil.test.ts

215 lines
5.3 KiB
TypeScript
Raw Normal View History

2019-12-02 03:38:11 +00:00
// import RealmExecutor from "jsExecution/RealmExecutor";
import {
mockExecute,
mockRegisterLibrary,
} from "../../test/__mocks__/RealmExecutorMock";
2020-01-17 09:28:26 +00:00
import {
dependencySortedEvaluateDataTree,
getDynamicValue,
2020-01-30 13:23:04 +00:00
getEntityDependencies,
2020-01-17 09:28:26 +00:00
parseDynamicString,
} from "./DynamicBindingUtils";
2020-02-18 10:41:52 +00:00
import { DataTree, ENTITY_TYPE } from "entities/DataTree/dataTreeFactory";
import { RenderModes, WidgetTypes } from "constants/WidgetConstants";
2020-02-18 10:41:52 +00:00
jest.mock("jsExecution/RealmExecutor", () => {
return jest.fn().mockImplementation(() => {
return { execute: mockExecute, registerLibrary: mockRegisterLibrary };
});
});
2019-12-02 03:38:11 +00:00
beforeAll(() => {
mockRegisterLibrary.mockClear();
mockExecute.mockClear();
});
2019-11-14 09:28:51 +00:00
it("Gets the value from the data tree", () => {
const dynamicBinding = "{{GetUsers.data}}";
2020-02-18 10:41:52 +00:00
const nameBindingsWithData: DataTree = {
2020-01-30 13:23:04 +00:00
GetUsers: {
2020-02-18 10:41:52 +00:00
data: { text: "correct data" },
config: {
pluginId: "",
2020-02-18 10:41:52 +00:00
id: "id",
name: "text",
actionConfiguration: {},
pageId: "",
jsonPathKeys: [],
datasource: { id: "" },
pluginType: "1",
},
isLoading: false,
ENTITY_TYPE: ENTITY_TYPE.ACTION,
run: jest.fn(),
2019-11-14 09:28:51 +00:00
},
};
2020-02-18 10:41:52 +00:00
const actualValue = { result: { text: "correct data" } };
2019-12-02 03:38:11 +00:00
const value = getDynamicValue(dynamicBinding, nameBindingsWithData);
2019-11-14 09:28:51 +00:00
expect(value).toEqual(actualValue);
});
2019-12-02 09:51:18 +00:00
describe.each([
["{{A}}", ["{{A}}"]],
["A {{B}}", ["A ", "{{B}}"]],
[
"Hello {{Customer.Name}}, the status for your order id {{orderId}} is {{status}}",
[
"Hello ",
"{{Customer.Name}}",
", the status for your order id ",
"{{orderId}}",
" is ",
"{{status}}",
],
],
[
"{{data.map(datum => {return {id: datum}})}}",
["{{data.map(datum => {return {id: datum}})}}"],
],
["{{}}{{}}}", ["{{}}", "{{}}", "}"]],
["{{{}}", ["{{{}}"]],
["{{ {{", ["{{ {{"]],
["}} }}", ["}} }}"]],
["}} {{", ["}} {{"]],
])("Parse the dynamic string(%s, %j)", (dynamicString, expected) => {
test(`returns ${expected}`, () => {
expect(parseDynamicString(dynamicString as string)).toStrictEqual(expected);
});
});
const baseWidgetProps = {
parentColumnSpace: 0,
parentRowSpace: 0,
parentId: "0",
type: WidgetTypes.BUTTON_WIDGET,
renderMode: RenderModes.CANVAS,
leftColumn: 0,
rightColumn: 0,
topRow: 0,
bottomRow: 0,
isLoading: false,
};
2020-01-17 09:28:26 +00:00
it("evaluates the data tree", () => {
const input: DataTree = {
2020-01-17 09:28:26 +00:00
widget1: {
...baseWidgetProps,
widgetId: "1",
widgetName: "widget1",
2020-01-17 09:28:26 +00:00
displayValue: "{{widget2.computedProperty}}",
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
2020-01-17 09:28:26 +00:00
},
widget2: {
...baseWidgetProps,
widgetId: "2",
widgetName: "widget2",
2020-01-17 09:28:26 +00:00
computedProperty: "{{ widget2.data[widget2.index] }}",
data: "{{ apiData.data }}",
2020-01-17 09:28:26 +00:00
index: 2,
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
2020-01-17 09:28:26 +00:00
},
apiData: {
config: {
id: "123",
pageId: "1234",
datasource: {},
name: "api",
actionConfiguration: {},
jsonPathKeys: [],
pluginId: "plugin",
},
run: (onSuccess, onError) => ({
type: "RUN_ACTION",
payload: {
actionId: "",
onSuccess: "",
onError: "",
},
}),
isLoading: false,
data: ["wrong value", "still wrong", "correct"],
ENTITY_TYPE: ENTITY_TYPE.ACTION,
2020-01-17 09:28:26 +00:00
},
};
const dynamicBindings = {
"widget1.displayValue": ["widget2.computedProperty"],
"widget2.computedProperty": ["widget2.data", "widget2.index"],
"widget2.data": ["apiData.data"],
};
const sortedDeps = [
"apiData.data",
"widget2.data",
"widget2.index",
"widget2.computedProperty",
"widget1.displayValue",
2020-01-17 09:28:26 +00:00
];
const output: DataTree = {
2020-01-17 09:28:26 +00:00
widget1: {
...baseWidgetProps,
widgetId: "1",
widgetName: "widget1",
2020-01-17 09:28:26 +00:00
displayValue: "correct",
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
2020-01-17 09:28:26 +00:00
},
widget2: {
...baseWidgetProps,
widgetId: "2",
widgetName: "widget2",
2020-01-17 09:28:26 +00:00
computedProperty: "correct",
data: ["wrong value", "still wrong", "correct"],
index: 2,
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
2020-01-17 09:28:26 +00:00
},
apiData: {
config: {
id: "123",
pageId: "1234",
datasource: {},
name: "api",
actionConfiguration: {},
jsonPathKeys: [],
pluginId: "plugin",
},
run: (onSuccess, onError) => ({
type: "RUN_ACTION",
payload: {
actionId: "",
onSuccess: "",
onError: "",
},
}),
isLoading: false,
data: ["wrong value", "still wrong", "correct"],
ENTITY_TYPE: ENTITY_TYPE.ACTION,
2020-01-17 09:28:26 +00:00
},
};
const result = dependencySortedEvaluateDataTree(
input,
dynamicBindings,
sortedDeps,
);
2020-01-17 09:28:26 +00:00
expect(result).toEqual(output);
});
2020-01-30 13:23:04 +00:00
it("finds dependencies of a entity", () => {
const depMap: Array<[string, string]> = [
["Widget5.text", "Widget2.data.visible"],
["Widget1.options", "Action1.data"],
["Widget2.text", "Widget1.selectedOption"],
["Widget3.text", "Widget4.selectedRow.name"],
["Widget6.label", "Action1.data.label"],
];
const entity = "Action1";
const result = ["Widget1", "Widget2", "Widget5", "Widget6"];
const actual = getEntityDependencies(depMap, entity);
expect(actual).toEqual(result);
});