fix: fix issue for null values in the alignFields util (#6152)

This commit is contained in:
Yuxin 2024-08-08 11:19:32 +08:00 committed by GitHub
parent 117d4513bf
commit 3aa9bbbfa2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,42 @@
import { createGraph } from '@@/utils';
describe('bug: plugin-history-align-fields', () => {
it('fix alignFields util', async () => {
const graph = createGraph({
plugins: [{ type: 'history', key: 'history' }],
data: {
nodes: [
{
id: 'node-1',
type: 'rect',
style: { x: 50, y: 100 },
data: {
aaa: {
bbb: false,
ccc: true,
ddd: '1234',
},
},
},
],
},
});
await graph.render();
expect(graph.getNodeData('node-1').style).toEqual({ x: 50, y: 100 });
expect(graph.getNodeData('node-1').data!.aaa).toEqual({
bbb: false,
ccc: true,
ddd: '1234',
});
await graph.translateElementBy('node-1', [100, 100]);
expect(graph.getNodeData('node-1').style).toEqual({ x: 150, y: 200, z: 0 });
expect(graph.getNodeData('node-1').data!.aaa).toEqual({
bbb: false,
ccc: true,
ddd: '1234',
});
});
});

View File

@ -18,7 +18,7 @@ export function alignFields(refObject: Record<string, any>, targetObject: Record
if (isObject(refObject[key]) && !Array.isArray(refObject[key]) && refObject[key] !== null) { if (isObject(refObject[key]) && !Array.isArray(refObject[key]) && refObject[key] !== null) {
if (!targetObject[key]) targetObject[key] = {}; if (!targetObject[key]) targetObject[key] = {};
alignFields(refObject[key], targetObject[key]); alignFields(refObject[key], targetObject[key]);
} else if (!targetObject[key]) { } else if (targetObject[key] === undefined) {
targetObject[key] = inferDefaultValue(key); targetObject[key] = inferDefaultValue(key);
} }
} }