properly handle translated diffs when change is between undefined and an Array (#12577)

This commit is contained in:
Favour Ohanekwu 2022-04-09 06:55:41 -07:00 committed by GitHub
parent 75cfe8df5b
commit b3e30725dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 3 deletions

View File

@ -47,7 +47,7 @@ export const generateDataTreeAction = (
pluginType: action.config.pluginType,
config: action.config.actionConfiguration,
dynamicBindingPathList,
data: action.data ? action.data.body : {},
data: action.data ? action.data.body : undefined,
responseMeta: {
statusCode: action.data?.statusCode,
isExecutionSuccess: action.data?.isExecutionSuccess || false,

View File

@ -1385,6 +1385,7 @@ export default class DataTreeEvaluator {
// In this case if the path exists in the dependency map
// remove it.
else if (fullPropertyPath in this.dependencyMap) {
didUpdateDependencyMap = true;
delete this.dependencyMap[fullPropertyPath];
}
}

View File

@ -8,13 +8,18 @@ import {
PrivateWidgets,
} from "entities/DataTree/dataTreeFactory";
import {
DataTreeDiff,
DataTreeDiffEvent,
getAllPaths,
getAllPrivateWidgetsInDataTree,
getDataTreeWithoutPrivateWidgets,
isPrivateEntityPath,
makeParentsDependOnChildren,
translateDiffEventToDataTreeDiffEvent,
} from "./evaluationUtils";
import { warn as logWarn } from "loglevel";
import { Diff } from "deep-diff";
import { flatten } from "lodash";
// to check if logWarn was called.
// use jest.unmock, if the mock needs to be removed.
@ -274,3 +279,118 @@ describe("makeParentsDependOnChildren", () => {
);
});
});
describe("translateDiffEvent", () => {
it("noop when diff path does not exist", () => {
const noDiffPath: Diff<any, any> = {
kind: "E",
lhs: undefined,
rhs: undefined,
};
const result = translateDiffEventToDataTreeDiffEvent(noDiffPath, {});
expect(result).toStrictEqual({
payload: {
propertyPath: "",
value: "",
},
event: DataTreeDiffEvent.NOOP,
});
});
it("translates new and delete events", () => {
const diffs: Diff<any, any>[] = [
{
kind: "N",
path: ["Widget1"],
rhs: {},
},
{
kind: "N",
path: ["Widget1", "name"],
rhs: "Widget1",
},
{
kind: "D",
path: ["Widget1"],
lhs: {},
},
{
kind: "D",
path: ["Widget1", "name"],
lhs: "Widget1",
},
{
kind: "E",
path: ["Widget2", "name"],
rhs: "test",
lhs: "test2",
},
];
const expectedTranslations: DataTreeDiff[] = [
{
payload: {
propertyPath: "Widget1",
},
event: DataTreeDiffEvent.NEW,
},
{
payload: {
propertyPath: "Widget1.name",
},
event: DataTreeDiffEvent.NEW,
},
{
payload: {
propertyPath: "Widget1",
},
event: DataTreeDiffEvent.DELETE,
},
{
payload: {
propertyPath: "Widget1.name",
},
event: DataTreeDiffEvent.DELETE,
},
{
payload: {
propertyPath: "",
value: "",
},
event: DataTreeDiffEvent.NOOP,
},
];
const actualTranslations = flatten(
diffs.map((diff) => translateDiffEventToDataTreeDiffEvent(diff, {})),
);
expect(expectedTranslations).toStrictEqual(actualTranslations);
});
it("properly categorises the edit events", () => {
const diffs: Diff<any, any>[] = [
{
kind: "E",
path: ["Widget2", "name"],
rhs: "test",
lhs: "test2",
},
];
const expectedTranslations: DataTreeDiff[] = [
{
payload: {
propertyPath: "",
value: "",
},
event: DataTreeDiffEvent.NOOP,
},
];
const actualTranslations = flatten(
diffs.map((diff) => translateDiffEventToDataTreeDiffEvent(diff, {})),
);
expect(expectedTranslations).toStrictEqual(actualTranslations);
});
});

View File

@ -164,11 +164,17 @@ export const translateDiffEventToDataTreeDiffEvent = (
} else if (difference.lhs === undefined || difference.rhs === undefined) {
// Handle static value changes that change structure that can lead to
// old bindings being eligible
if (difference.lhs === undefined && isTrueObject(difference.rhs)) {
if (
difference.lhs === undefined &&
(isTrueObject(difference.rhs) || Array.isArray(difference.rhs))
) {
result.event = DataTreeDiffEvent.NEW;
result.payload = { propertyPath };
}
if (difference.rhs === undefined && isTrueObject(difference.lhs)) {
if (
difference.rhs === undefined &&
(isTrueObject(difference.lhs) || Array.isArray(difference.lhs))
) {
result.event = DataTreeDiffEvent.DELETE;
result.payload = { propertyPath };
}