|
| 1 | +import { TypedEventEmitter } from '@imtbl/auth'; |
| 2 | +import { WalletEvents, WalletEventMap } from '../types'; |
| 3 | +import { ProviderEvent } from './types'; |
| 4 | +import { ZkEvmProvider } from './zkEvmProvider'; |
| 5 | +import { WalletConfiguration } from '../config'; |
| 6 | + |
| 7 | +jest.mock('viem', () => { |
| 8 | + const actual = jest.requireActual<typeof import('viem')>('viem'); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + createPublicClient: jest.fn(() => ({})), |
| 12 | + http: jest.fn(() => ({})), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +jest.mock('./relayerClient', () => ({ |
| 17 | + RelayerClient: jest.fn().mockImplementation(() => ({})), |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('../guardian', () => jest.fn().mockImplementation(() => ({ |
| 21 | + withConfirmationScreen: () => (fn: () => Promise<any>) => fn(), |
| 22 | +}))); |
| 23 | + |
| 24 | +jest.mock('./sessionActivity/sessionActivity', () => ({ |
| 25 | + trackSessionActivity: jest.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +const mockUserWithZkEvm = { |
| 29 | + profile: { sub: 'user-123' }, |
| 30 | + accessToken: 'token', |
| 31 | + zkEvm: { ethAddress: '0x1234567890123456789012345678901234567890' }, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('ZkEvmProvider', () => { |
| 35 | + const walletEventEmitter = new TypedEventEmitter<WalletEventMap>(); |
| 36 | + const config = new WalletConfiguration({ |
| 37 | + passportDomain: 'https://passport.immutable.com', |
| 38 | + zkEvmRpcUrl: 'https://rpc.test.immutable.com', |
| 39 | + relayerUrl: 'https://relayer.test.immutable.com', |
| 40 | + indexerMrBasePath: 'https://api.test.immutable.com', |
| 41 | + }); |
| 42 | + |
| 43 | + const mockGetUser = jest.fn().mockResolvedValue(null); |
| 44 | + const mockEthSigner = { |
| 45 | + getAddress: jest.fn().mockResolvedValue('0xabc'), |
| 46 | + signMessage: jest.fn(), |
| 47 | + signTypedData: jest.fn(), |
| 48 | + }; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + jest.clearAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('emits accountsChanged when LOGGED_IN event is received with zkEvm user', () => { |
| 55 | + const provider = new ZkEvmProvider({ |
| 56 | + getUser: mockGetUser, |
| 57 | + clientId: 'test-client', |
| 58 | + config, |
| 59 | + multiRollupApiClients: {} as any, |
| 60 | + walletEventEmitter, |
| 61 | + guardianClient: {} as any, |
| 62 | + ethSigner: mockEthSigner as any, |
| 63 | + user: null, |
| 64 | + sessionActivityApiUrl: null, |
| 65 | + }); |
| 66 | + |
| 67 | + const accountsChangedHandler = jest.fn(); |
| 68 | + provider.on(ProviderEvent.ACCOUNTS_CHANGED, accountsChangedHandler); |
| 69 | + |
| 70 | + walletEventEmitter.emit(WalletEvents.LOGGED_IN, mockUserWithZkEvm as any); |
| 71 | + |
| 72 | + expect(accountsChangedHandler).toHaveBeenCalledWith([mockUserWithZkEvm.zkEvm.ethAddress]); |
| 73 | + }); |
| 74 | +}); |
0 commit comments