|
| 1 | +import { Cardano } from '@cardano-sdk/core'; |
| 2 | +import { Cip30Event, Cip30EventName, Cip30EventRegistry } from '../../src/WalletApi/Cip30EventRegistry'; |
| 3 | +import { Subject } from 'rxjs'; |
| 4 | + |
| 5 | +describe('Cip30EventRegistry', () => { |
| 6 | + let cip30Event$: Subject<Cip30Event>; |
| 7 | + let registry: Cip30EventRegistry; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + cip30Event$ = new Subject(); |
| 11 | + registry = new Cip30EventRegistry(cip30Event$); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + registry.shutdown(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should register and trigger networkChange callback', () => { |
| 19 | + const callback = jest.fn(); |
| 20 | + registry.register(Cip30EventName.networkChange, callback); |
| 21 | + |
| 22 | + const networkId: Cardano.NetworkId = 1; |
| 23 | + cip30Event$.next({ data: networkId, eventName: Cip30EventName.networkChange }); |
| 24 | + |
| 25 | + expect(callback).toHaveBeenCalledWith(networkId); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should register and trigger accountChange callback', () => { |
| 29 | + const callback = jest.fn(); |
| 30 | + registry.register(Cip30EventName.accountChange, callback); |
| 31 | + |
| 32 | + const addresses: Cardano.BaseAddress[] = [{} as unknown as Cardano.BaseAddress]; |
| 33 | + cip30Event$.next({ data: addresses, eventName: Cip30EventName.accountChange }); |
| 34 | + |
| 35 | + expect(callback).toHaveBeenCalledWith(addresses); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should deregister networkChange callback', () => { |
| 39 | + const callback = jest.fn(); |
| 40 | + registry.register(Cip30EventName.networkChange, callback); |
| 41 | + registry.deregister(Cip30EventName.networkChange, callback); |
| 42 | + |
| 43 | + const networkId: Cardano.NetworkId = 1; |
| 44 | + cip30Event$.next({ data: networkId, eventName: Cip30EventName.networkChange }); |
| 45 | + |
| 46 | + expect(callback).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should deregister accountChange callback', () => { |
| 50 | + const callback = jest.fn(); |
| 51 | + registry.register(Cip30EventName.accountChange, callback); |
| 52 | + registry.deregister(Cip30EventName.accountChange, callback); |
| 53 | + |
| 54 | + const addresses: Cardano.BaseAddress[] = [{} as unknown as Cardano.BaseAddress]; |
| 55 | + cip30Event$.next({ data: addresses, eventName: Cip30EventName.accountChange }); |
| 56 | + |
| 57 | + expect(callback).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle multiple callbacks for the same event', () => { |
| 61 | + const callback1 = jest.fn(); |
| 62 | + const callback2 = jest.fn(); |
| 63 | + registry.register(Cip30EventName.networkChange, callback1); |
| 64 | + registry.register(Cip30EventName.networkChange, callback2); |
| 65 | + |
| 66 | + const networkId: Cardano.NetworkId = 1; |
| 67 | + cip30Event$.next({ data: networkId, eventName: Cip30EventName.networkChange }); |
| 68 | + |
| 69 | + expect(callback1).toHaveBeenCalledWith(networkId); |
| 70 | + expect(callback2).toHaveBeenCalledWith(networkId); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not trigger callbacks after shutdown', () => { |
| 74 | + const callback = jest.fn(); |
| 75 | + registry.register(Cip30EventName.networkChange, callback); |
| 76 | + |
| 77 | + registry.shutdown(); |
| 78 | + |
| 79 | + const networkId: Cardano.NetworkId = 1; |
| 80 | + cip30Event$.next({ data: networkId, eventName: Cip30EventName.networkChange }); |
| 81 | + |
| 82 | + expect(callback).not.toHaveBeenCalled(); |
| 83 | + expect(cip30Event$.observed).toBeFalsy(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should not register callbacks after shutdown', () => { |
| 87 | + const callback = jest.fn(); |
| 88 | + registry.shutdown(); |
| 89 | + registry.register(Cip30EventName.networkChange, callback); |
| 90 | + |
| 91 | + const networkId: Cardano.NetworkId = 1; |
| 92 | + cip30Event$.next({ data: networkId, eventName: Cip30EventName.networkChange }); |
| 93 | + |
| 94 | + expect(callback).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments