Skip to content

Commit e3a1989

Browse files
liujupingJackLian
authored andcommitted
fix(prop): emit event when unset prop
1 parent dfe6878 commit e3a1989

File tree

2 files changed

+132
-17
lines changed

2 files changed

+132
-17
lines changed

packages/designer/src/document/node/props/prop.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ export class Prop implements IProp, IPropParent {
353353
@action
354354
setValue(val: IPublicTypeCompositeValue) {
355355
if (val === this._value) return;
356-
const editor = this.owner.document?.designer.editor;
357356
const oldValue = this._value;
358357
this._value = val;
359358
this._code = null;
@@ -386,22 +385,31 @@ export class Prop implements IProp, IPropParent {
386385
this.setupItems();
387386

388387
if (oldValue !== this._value) {
389-
const propsInfo = {
390-
key: this.key,
391-
prop: this,
392-
oldValue,
393-
newValue: this._value,
394-
};
395-
396-
editor?.eventBus.emit(GlobalEvent.Node.Prop.InnerChange, {
397-
node: this.owner as any,
398-
...propsInfo,
399-
});
400-
401-
this.owner?.emitPropChange?.(propsInfo);
388+
this.emitChange({ oldValue });
402389
}
403390
}
404391

392+
emitChange = ({
393+
oldValue,
394+
}: {
395+
oldValue: IPublicTypeCompositeValue | UNSET;
396+
}) => {
397+
const editor = this.owner.document?.designer.editor;
398+
const propsInfo = {
399+
key: this.key,
400+
prop: this,
401+
oldValue,
402+
newValue: this.type === 'unset' ? undefined : this._value,
403+
};
404+
405+
editor?.eventBus.emit(GlobalEvent.Node.Prop.InnerChange, {
406+
node: this.owner as any,
407+
...propsInfo,
408+
});
409+
410+
this.owner?.emitPropChange?.(propsInfo);
411+
};
412+
405413
getValue(): IPublicTypeCompositeValue {
406414
return this.export(IPublicEnumTransformStage.Serilize);
407415
}
@@ -462,7 +470,12 @@ export class Prop implements IProp, IPropParent {
462470
*/
463471
@action
464472
unset() {
465-
this._type = 'unset';
473+
if (this._type !== 'unset') {
474+
this._type = 'unset';
475+
this.emitChange({
476+
oldValue: this._value,
477+
});
478+
}
466479
}
467480

468481
/**

packages/designer/tests/document/node/props/prop.test.ts

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Editor, engineConfig } from '@alilc/lowcode-editor-core';
33
import { Designer } from '../../../../src/designer/designer';
44
import { DocumentModel } from '../../../../src/document/document-model';
55
import { Prop, isProp, isValidArrayIndex } from '../../../../src/document/node/props/prop';
6-
import { IPublicEnumTransformStage } from '@alilc/lowcode-types';
6+
import { GlobalEvent, IPublicEnumTransformStage } from '@alilc/lowcode-types';
77
import { shellModelFactory } from '../../../../../engine/src/modules/shell-model-factory';
88

99
const slotNodeImportMockFn = jest.fn();
@@ -24,9 +24,16 @@ const mockOwner = {
2424
remove: slotNodeRemoveMockFn,
2525
};
2626
},
27-
designer: {},
27+
designer: {
28+
editor: {
29+
eventBus: {
30+
emit: jest.fn(),
31+
},
32+
},
33+
},
2834
},
2935
isInited: true,
36+
emitPropChange: jest.fn(),
3037
};
3138

3239
const mockPropsInst = {
@@ -564,3 +571,98 @@ describe('其他导出函数', () => {
564571
expect(isValidArrayIndex('2', 1)).toBeFalsy();
565572
});
566573
});
574+
575+
describe('setValue with event', () => {
576+
let propInstance;
577+
let mockEmitChange;
578+
let mockEventBusEmit;
579+
let mockEmitPropChange;
580+
581+
beforeEach(() => {
582+
// Initialize the instance of your class
583+
propInstance = new Prop(mockPropsInst, true, 'stringProp');;
584+
585+
// Mock necessary methods and properties
586+
mockEmitChange = jest.spyOn(propInstance, 'emitChange');
587+
propInstance.owner = {
588+
document: {
589+
designer: {
590+
editor: {
591+
eventBus: {
592+
emit: jest.fn(),
593+
},
594+
},
595+
},
596+
},
597+
emitPropChange: jest.fn(),
598+
};
599+
mockEventBusEmit = jest.spyOn(propInstance.owner.document.designer.editor.eventBus, 'emit');
600+
mockEmitPropChange = jest.spyOn(propInstance.owner, 'emitPropChange');
601+
});
602+
603+
afterEach(() => {
604+
jest.restoreAllMocks();
605+
});
606+
607+
it('should correctly handle string values and emit changes', () => {
608+
const oldValue = propInstance._value;
609+
const newValue = 'new string value';
610+
611+
propInstance.setValue(newValue);
612+
613+
const expectedPartialPropsInfo = expect.objectContaining({
614+
key: propInstance.key,
615+
newValue, // You can specifically test only certain keys
616+
oldValue,
617+
});
618+
619+
expect(propInstance.getValue()).toBe(newValue);
620+
expect(propInstance.type).toBe('literal');
621+
expect(mockEmitChange).toHaveBeenCalledWith({ oldValue });
622+
expect(mockEventBusEmit).toHaveBeenCalledWith(GlobalEvent.Node.Prop.InnerChange, expectedPartialPropsInfo);
623+
expect(mockEmitPropChange).toHaveBeenCalledWith(expectedPartialPropsInfo);
624+
});
625+
626+
it('should handle object values and set type to map', () => {
627+
const oldValue = propInstance._value;
628+
const newValue = 234;
629+
630+
const expectedPartialPropsInfo = expect.objectContaining({
631+
key: propInstance.key,
632+
newValue, // You can specifically test only certain keys
633+
oldValue,
634+
});
635+
636+
propInstance.setValue(newValue);
637+
638+
expect(propInstance.getValue()).toEqual(newValue);
639+
expect(propInstance.type).toBe('literal');
640+
expect(mockEmitChange).toHaveBeenCalledWith({ oldValue });
641+
expect(mockEventBusEmit).toHaveBeenCalledWith(GlobalEvent.Node.Prop.InnerChange, expectedPartialPropsInfo);
642+
expect(mockEmitPropChange).toHaveBeenCalledWith(expectedPartialPropsInfo);
643+
});
644+
645+
it('should has event when unset call', () => {
646+
const oldValue = propInstance._value;
647+
648+
propInstance.unset();
649+
650+
const expectedPartialPropsInfo = expect.objectContaining({
651+
key: propInstance.key,
652+
newValue: undefined, // You can specifically test only certain keys
653+
oldValue,
654+
});
655+
656+
expect(propInstance.getValue()).toEqual(undefined);
657+
expect(propInstance.type).toBe('unset');
658+
expect(mockEmitChange).toHaveBeenCalledWith({
659+
oldValue,
660+
newValue: undefined,
661+
});
662+
expect(mockEventBusEmit).toHaveBeenCalledWith(GlobalEvent.Node.Prop.InnerChange, expectedPartialPropsInfo);
663+
expect(mockEmitPropChange).toHaveBeenCalledWith(expectedPartialPropsInfo);
664+
665+
propInstance.unset();
666+
expect(mockEmitChange).toHaveBeenCalledTimes(1);
667+
});
668+
});

0 commit comments

Comments
 (0)