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