|
1 | 1 | import type { Patch } from 'immer'; |
2 | | -import * as sinon from 'sinon'; |
| 2 | +import sinon from 'sinon'; |
3 | 3 |
|
4 | 4 | import { Messenger } from './Messenger'; |
5 | 5 |
|
@@ -556,4 +556,172 @@ describe('Messenger', () => { |
556 | 556 |
|
557 | 557 | expect(handler.callCount).toBe(0); |
558 | 558 | }); |
| 559 | + |
| 560 | + describe('registerMethodActionHandlers', () => { |
| 561 | + it('should register action handlers for specified methods on the given messenger client', () => { |
| 562 | + type TestActions = |
| 563 | + | { type: 'TestService:getType'; handler: () => string } |
| 564 | + | { |
| 565 | + type: 'TestService:getCount'; |
| 566 | + handler: () => number; |
| 567 | + }; |
| 568 | + |
| 569 | + const messenger = new Messenger<TestActions, never>(); |
| 570 | + |
| 571 | + class TestService { |
| 572 | + name = 'TestService'; |
| 573 | + |
| 574 | + getType() { |
| 575 | + return 'api'; |
| 576 | + } |
| 577 | + |
| 578 | + getCount() { |
| 579 | + return 42; |
| 580 | + } |
| 581 | + } |
| 582 | + |
| 583 | + const service = new TestService(); |
| 584 | + const methodNames = ['getType', 'getCount'] as const; |
| 585 | + |
| 586 | + messenger.registerMethodActionHandlers(service, methodNames); |
| 587 | + |
| 588 | + const state = messenger.call('TestService:getType'); |
| 589 | + expect(state).toBe('api'); |
| 590 | + |
| 591 | + const count = messenger.call('TestService:getCount'); |
| 592 | + expect(count).toBe(42); |
| 593 | + }); |
| 594 | + |
| 595 | + it('should bind action handlers to the given messenger client', () => { |
| 596 | + type TestAction = { |
| 597 | + type: 'TestService:getPrivateValue'; |
| 598 | + handler: () => string; |
| 599 | + }; |
| 600 | + const messenger = new Messenger<TestAction, never>(); |
| 601 | + |
| 602 | + class TestService { |
| 603 | + name = 'TestService'; |
| 604 | + |
| 605 | + privateValue = 'secret'; |
| 606 | + |
| 607 | + getPrivateValue() { |
| 608 | + return this.privateValue; |
| 609 | + } |
| 610 | + } |
| 611 | + |
| 612 | + const service = new TestService(); |
| 613 | + messenger.registerMethodActionHandlers(service, ['getPrivateValue']); |
| 614 | + |
| 615 | + const result = messenger.call('TestService:getPrivateValue'); |
| 616 | + expect(result).toBe('secret'); |
| 617 | + }); |
| 618 | + |
| 619 | + it('should handle async methods', async () => { |
| 620 | + type TestAction = { |
| 621 | + type: 'TestService:fetchData'; |
| 622 | + handler: (id: string) => Promise<string>; |
| 623 | + }; |
| 624 | + const messenger = new Messenger<TestAction, never>(); |
| 625 | + |
| 626 | + class TestService { |
| 627 | + name = 'TestService'; |
| 628 | + |
| 629 | + async fetchData(id: string) { |
| 630 | + return `data-${id}`; |
| 631 | + } |
| 632 | + } |
| 633 | + |
| 634 | + const service = new TestService(); |
| 635 | + messenger.registerMethodActionHandlers(service, ['fetchData']); |
| 636 | + |
| 637 | + const result = await messenger.call('TestService:fetchData', '123'); |
| 638 | + expect(result).toBe('data-123'); |
| 639 | + }); |
| 640 | + |
| 641 | + it('should not throw when given an empty methodNames array', () => { |
| 642 | + type TestAction = { type: 'TestController:test'; handler: () => void }; |
| 643 | + const messenger = new Messenger<TestAction, never>(); |
| 644 | + |
| 645 | + class TestController { |
| 646 | + name = 'TestController'; |
| 647 | + } |
| 648 | + |
| 649 | + const controller = new TestController(); |
| 650 | + const methodNames: readonly string[] = []; |
| 651 | + |
| 652 | + expect(() => { |
| 653 | + messenger.registerMethodActionHandlers( |
| 654 | + controller, |
| 655 | + methodNames as never[], |
| 656 | + ); |
| 657 | + }).not.toThrow(); |
| 658 | + }); |
| 659 | + |
| 660 | + it('should skip non-function properties', () => { |
| 661 | + type TestAction = { |
| 662 | + type: 'TestController:getValue'; |
| 663 | + handler: () => string; |
| 664 | + }; |
| 665 | + const messenger = new Messenger<TestAction, never>(); |
| 666 | + |
| 667 | + class TestController { |
| 668 | + name = 'TestController'; |
| 669 | + |
| 670 | + readonly nonFunction = 'not a function'; |
| 671 | + |
| 672 | + getValue() { |
| 673 | + return 'test'; |
| 674 | + } |
| 675 | + } |
| 676 | + |
| 677 | + const controller = new TestController(); |
| 678 | + messenger.registerMethodActionHandlers(controller, ['getValue']); |
| 679 | + |
| 680 | + // getValue should be registered |
| 681 | + expect(messenger.call('TestController:getValue')).toBe('test'); |
| 682 | + |
| 683 | + // nonFunction should not be registered |
| 684 | + expect(() => { |
| 685 | + // @ts-expect-error - This is a test |
| 686 | + messenger.call('TestController:nonFunction'); |
| 687 | + }).toThrow( |
| 688 | + 'A handler for TestController:nonFunction has not been registered', |
| 689 | + ); |
| 690 | + }); |
| 691 | + |
| 692 | + it('should work with class inheritance', () => { |
| 693 | + type TestActions = |
| 694 | + | { type: 'ChildController:baseMethod'; handler: () => string } |
| 695 | + | { type: 'ChildController:childMethod'; handler: () => string }; |
| 696 | + |
| 697 | + const messenger = new Messenger<TestActions, never>(); |
| 698 | + |
| 699 | + class BaseController { |
| 700 | + name = 'BaseController'; |
| 701 | + |
| 702 | + baseMethod() { |
| 703 | + return 'base method'; |
| 704 | + } |
| 705 | + } |
| 706 | + |
| 707 | + class ChildController extends BaseController { |
| 708 | + name = 'ChildController'; |
| 709 | + |
| 710 | + childMethod() { |
| 711 | + return 'child method'; |
| 712 | + } |
| 713 | + } |
| 714 | + |
| 715 | + const controller = new ChildController(); |
| 716 | + messenger.registerMethodActionHandlers(controller, [ |
| 717 | + 'baseMethod', |
| 718 | + 'childMethod', |
| 719 | + ]); |
| 720 | + |
| 721 | + expect(messenger.call('ChildController:baseMethod')).toBe('base method'); |
| 722 | + expect(messenger.call('ChildController:childMethod')).toBe( |
| 723 | + 'child method', |
| 724 | + ); |
| 725 | + }); |
| 726 | + }); |
559 | 727 | }); |
0 commit comments