|
| 1 | +import sinon from 'sinon'; |
| 2 | +import assert from 'assert'; |
| 3 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 4 | +import { CoinMap, coins, Erc20Coin } from '@bitgo/statics'; |
| 5 | +import { register, registerWithCoinMap } from '../../src/register'; |
| 6 | +import { Erc20Token } from '../../src/erc20Token'; |
| 7 | +import { Erc721Token } from '../../src/erc721Token'; |
| 8 | + |
| 9 | +describe('ETH Register', function () { |
| 10 | + let bitgo: BitGoAPI; |
| 11 | + let registerSpy: sinon.SinonSpy; |
| 12 | + let registerWithBaseCoinSpy: sinon.SinonSpy; |
| 13 | + |
| 14 | + beforeEach(function () { |
| 15 | + bitgo = new BitGoAPI({ env: 'test' }); |
| 16 | + registerSpy = sinon.spy(bitgo, 'register'); |
| 17 | + registerWithBaseCoinSpy = sinon.spy(bitgo, 'registerWithBaseCoin'); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(function () { |
| 21 | + registerSpy.restore(); |
| 22 | + registerWithBaseCoinSpy.restore(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('register', function () { |
| 26 | + it('should register base coins and token constructors', function () { |
| 27 | + register(bitgo); |
| 28 | + |
| 29 | + const registeredNames = registerSpy.getCalls().map((call) => call.args[0]); |
| 30 | + |
| 31 | + // Base coins should be registered |
| 32 | + assert.ok(registeredNames.includes('eth')); |
| 33 | + assert.ok(registeredNames.includes('gteth')); |
| 34 | + assert.ok(registeredNames.includes('teth')); |
| 35 | + assert.ok(registeredNames.includes('hteth')); |
| 36 | + |
| 37 | + // ERC20 and ERC721 tokens should be registered |
| 38 | + const erc20Count = Erc20Token.createTokenConstructors().length; |
| 39 | + const erc721Count = Erc721Token.createTokenConstructors().length; |
| 40 | + assert.strictEqual(registerSpy.callCount, 4 + erc20Count + erc721Count); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('registerWithCoinMap', function () { |
| 45 | + it('should call register internally for base coins and tokens', function () { |
| 46 | + registerWithCoinMap(bitgo, coins); |
| 47 | + |
| 48 | + const registeredNames = registerSpy.getCalls().map((call) => call.args[0]); |
| 49 | + |
| 50 | + // Base coins should be registered via register() |
| 51 | + assert.ok(registeredNames.includes('eth')); |
| 52 | + assert.ok(registeredNames.includes('gteth')); |
| 53 | + assert.ok(registeredNames.includes('teth')); |
| 54 | + assert.ok(registeredNames.includes('hteth')); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should register dynamic ERC20 tokens via registerWithBaseCoin', function () { |
| 58 | + registerWithCoinMap(bitgo, coins); |
| 59 | + |
| 60 | + // registerWithBaseCoin should have been called for dynamic tokens |
| 61 | + assert.ok(registerWithBaseCoinSpy.callCount > 0); |
| 62 | + |
| 63 | + // Each call should pass a valid baseCoin from the coinMap |
| 64 | + for (let i = 0; i < registerWithBaseCoinSpy.callCount; i++) { |
| 65 | + const call = registerWithBaseCoinSpy.getCall(i); |
| 66 | + const baseCoin = call.args[1]; |
| 67 | + assert.ok(coins.has(baseCoin.name), `${baseCoin.name} should exist in the coin map`); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it('should not call registerWithBaseCoin when coin map has no ERC20 tokens', function () { |
| 72 | + // Create a coin map with only base coins (no ERC20 tokens) |
| 73 | + const baseCoinOnly = coins.filter((coin) => !(coin instanceof Erc20Coin)); |
| 74 | + const limitedCoinMap = CoinMap.fromCoins(baseCoinOnly); |
| 75 | + |
| 76 | + registerWithCoinMap(bitgo, limitedCoinMap); |
| 77 | + |
| 78 | + // registerWithBaseCoin should not be called since no ERC20 tokens are in the map |
| 79 | + assert.strictEqual(registerWithBaseCoinSpy.callCount, 0); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments