fix(prop): emit event when delete prop

This commit is contained in:
liujuping 2024-01-30 14:34:53 +08:00 committed by 林熠
parent 80bb7102b6
commit 557a462b9f
2 changed files with 30 additions and 0 deletions

View File

@ -570,6 +570,7 @@ export class Prop implements IProp, IPropParent {
@action @action
remove() { remove() {
this.parent.delete(this); this.parent.delete(this);
this.unset();
} }
/** /**

View File

@ -34,11 +34,14 @@ const mockOwner = {
}, },
isInited: true, isInited: true,
emitPropChange: jest.fn(), emitPropChange: jest.fn(),
delete() {},
}; };
const mockPropsInst = { const mockPropsInst = {
owner: mockOwner, owner: mockOwner,
delete() {},
}; };
mockPropsInst.props = mockPropsInst; mockPropsInst.props = mockPropsInst;
describe('Prop 类测试', () => { describe('Prop 类测试', () => {
@ -595,6 +598,7 @@ describe('setValue with event', () => {
}, },
}, },
emitPropChange: jest.fn(), emitPropChange: jest.fn(),
delete() {},
}; };
mockEventBusEmit = jest.spyOn(propInstance.owner.document.designer.editor.eventBus, 'emit'); mockEventBusEmit = jest.spyOn(propInstance.owner.document.designer.editor.eventBus, 'emit');
mockEmitPropChange = jest.spyOn(propInstance.owner, 'emitPropChange'); mockEmitPropChange = jest.spyOn(propInstance.owner, 'emitPropChange');
@ -665,4 +669,29 @@ describe('setValue with event', () => {
propInstance.unset(); propInstance.unset();
expect(mockEmitChange).toHaveBeenCalledTimes(1); expect(mockEmitChange).toHaveBeenCalledTimes(1);
}); });
// remove
it('should has event when remove call', () => {
const oldValue = propInstance._value;
propInstance.remove();
const expectedPartialPropsInfo = expect.objectContaining({
key: propInstance.key,
newValue: undefined, // You can specifically test only certain keys
oldValue,
});
expect(propInstance.getValue()).toEqual(undefined);
// expect(propInstance.type).toBe('unset');
expect(mockEmitChange).toHaveBeenCalledWith({
oldValue,
newValue: undefined,
});
expect(mockEventBusEmit).toHaveBeenCalledWith(GlobalEvent.Node.Prop.InnerChange, expectedPartialPropsInfo);
expect(mockEmitPropChange).toHaveBeenCalledWith(expectedPartialPropsInfo);
propInstance.remove();
expect(mockEmitChange).toHaveBeenCalledTimes(1);
});
}); });