@@ -3,7 +3,7 @@ import { Editor, engineConfig } from '@alilc/lowcode-editor-core'; |
3 | 3 | import { Designer } from '../../../../src/designer/designer'; |
4 | 4 | import { DocumentModel } from '../../../../src/document/document-model'; |
5 | 5 | 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'; |
7 | 7 | import { shellModelFactory } from '../../../../../engine/src/modules/shell-model-factory'; |
8 | 8 |
|
9 | 9 | const slotNodeImportMockFn = jest.fn(); |
@@ -24,9 +24,16 @@ const mockOwner = { |
24 | 24 | remove: slotNodeRemoveMockFn, |
25 | 25 | }; |
26 | 26 | }, |
27 | | - designer: {}, |
| 27 | + designer: { |
| 28 | + editor: { |
| 29 | + eventBus: { |
| 30 | + emit: jest.fn(), |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
28 | 34 | }, |
29 | 35 | isInited: true, |
| 36 | + emitPropChange: jest.fn(), |
30 | 37 | }; |
31 | 38 |
|
32 | 39 | const mockPropsInst = { |
@@ -564,3 +571,98 @@ describe('其他导出函数', () => { |
564 | 571 | expect(isValidArrayIndex('2', 1)).toBeFalsy(); |
565 | 572 | }); |
566 | 573 | }); |
| 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 | +}); |