|
| 1 | +import { |
| 2 | + ClientRequestMethod, |
| 3 | + type GetStellarAccountActivationStatusJsonRpcRequest, |
| 4 | +} from './api'; |
| 5 | +import { GetStellarAccountActivationStatusHandler } from './getStellarAccountActivationStatus'; |
| 6 | +import { KnownCaip2ChainId } from '../../api'; |
| 7 | +import { AccountService } from '../../services/account'; |
| 8 | +import { generateStellarKeyringAccount } from '../../services/account/__mocks__/account.fixtures'; |
| 9 | +import { NetworkService } from '../../services/network'; |
| 10 | +import { mockOnChainAccountService } from '../../services/on-chain-account/__mocks__/onChainAccount.fixtures'; |
| 11 | +import { getTestWallet } from '../../services/wallet/__mocks__/wallet.fixtures'; |
| 12 | +import { logger } from '../../utils/logger'; |
| 13 | + |
| 14 | +jest.mock('../../utils/logger'); |
| 15 | + |
| 16 | +describe('GetStellarAccountActivationStatusHandler', () => { |
| 17 | + const accountId = '11111111-1111-4111-8111-111111111111'; |
| 18 | + const scope = KnownCaip2ChainId.Mainnet; |
| 19 | + |
| 20 | + function buildRequest(): GetStellarAccountActivationStatusJsonRpcRequest { |
| 21 | + return { |
| 22 | + jsonrpc: '2.0', |
| 23 | + id: 1, |
| 24 | + method: ClientRequestMethod.GetStellarAccountActivationStatus, |
| 25 | + params: { accountId, scope }, |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + it('returns activated true when Horizon has the account', async () => { |
| 30 | + const wallet = getTestWallet(); |
| 31 | + const account = generateStellarKeyringAccount( |
| 32 | + accountId, |
| 33 | + wallet.address, |
| 34 | + 'entropy-source-1', |
| 35 | + 0, |
| 36 | + ); |
| 37 | + |
| 38 | + const resolveAccountSpy = jest |
| 39 | + .spyOn(AccountService.prototype, 'resolveAccount') |
| 40 | + .mockResolvedValue({ account }); |
| 41 | + const getAccountOrNullSpy = jest |
| 42 | + .spyOn(NetworkService.prototype, 'getAccountOrNull') |
| 43 | + .mockResolvedValue({} as never); |
| 44 | + |
| 45 | + const { accountService } = mockOnChainAccountService(); |
| 46 | + const networkService = new NetworkService({ logger }); |
| 47 | + const handler = new GetStellarAccountActivationStatusHandler({ |
| 48 | + logger, |
| 49 | + accountService, |
| 50 | + networkService, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(await handler.handle(buildRequest())).toStrictEqual({ |
| 54 | + activated: true, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(resolveAccountSpy).toHaveBeenCalledWith({ accountId }); |
| 58 | + expect(getAccountOrNullSpy).toHaveBeenCalledWith(account.address, scope); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns activated false when Horizon has no account', async () => { |
| 62 | + const wallet = getTestWallet(); |
| 63 | + const account = generateStellarKeyringAccount( |
| 64 | + accountId, |
| 65 | + wallet.address, |
| 66 | + 'entropy-source-1', |
| 67 | + 0, |
| 68 | + ); |
| 69 | + |
| 70 | + jest |
| 71 | + .spyOn(AccountService.prototype, 'resolveAccount') |
| 72 | + .mockResolvedValue({ account }); |
| 73 | + const getAccountOrNullSpy = jest |
| 74 | + .spyOn(NetworkService.prototype, 'getAccountOrNull') |
| 75 | + .mockResolvedValue(null); |
| 76 | + |
| 77 | + const { accountService } = mockOnChainAccountService(); |
| 78 | + const networkService = new NetworkService({ logger }); |
| 79 | + const handler = new GetStellarAccountActivationStatusHandler({ |
| 80 | + logger, |
| 81 | + accountService, |
| 82 | + networkService, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(await handler.handle(buildRequest())).toStrictEqual({ |
| 86 | + activated: false, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(getAccountOrNullSpy).toHaveBeenCalledWith(account.address, scope); |
| 90 | + }); |
| 91 | +}); |
0 commit comments