|
| 1 | +// PolymathRegistryWrapper test |
| 2 | +import { mock, instance, reset, when, verify } from 'ts-mockito'; |
| 3 | +import { Web3Wrapper } from '@0x/web3-wrapper'; |
| 4 | +import { ERC20DetailedContract, SecurityTokenRegistryEvents } from '@polymathnetwork/abi-wrappers'; |
| 5 | +import { BigNumber } from '@0x/utils'; |
| 6 | +import ERC20TokenWrapper from '../erc20_wrapper'; |
| 7 | +import AlternativeERC20TokenWrapper from '../alternative_erc20_wrapper'; |
| 8 | +import { MockedCallMethod } from '../../../test_utils/mocked_methods'; |
| 9 | +import { bytes32ToString, stringToBytes32 } from '../../../utils/convert'; |
| 10 | + |
| 11 | +describe('AlternativeERC20TokenWrapper', () => { |
| 12 | + // Declare ERC20DetailedTokenWrapper object |
| 13 | + let target: AlternativeERC20TokenWrapper; |
| 14 | + let mockedWrapper: Web3Wrapper; |
| 15 | + let mockedContract: ERC20DetailedContract; |
| 16 | + |
| 17 | + beforeAll(() => { |
| 18 | + mockedWrapper = mock(Web3Wrapper); |
| 19 | + mockedContract = mock(ERC20DetailedContract); |
| 20 | + |
| 21 | + const myContractPromise = Promise.resolve(instance(mockedContract)); |
| 22 | + target = new AlternativeERC20TokenWrapper(instance(mockedWrapper), myContractPromise); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + reset(mockedWrapper); |
| 27 | + reset(mockedContract); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('Types', () => { |
| 31 | + test('should extend ERC20TokenWrapper', async () => { |
| 32 | + expect(target instanceof ERC20TokenWrapper).toBe(true); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('name', () => { |
| 37 | + test('should call to name', async () => { |
| 38 | + const expectedResult = stringToBytes32('string'); |
| 39 | + // Mocked method |
| 40 | + const mockedMethod = mock(MockedCallMethod); |
| 41 | + // Stub the method |
| 42 | + when(mockedContract.name).thenReturn(instance(mockedMethod)); |
| 43 | + // Stub the request |
| 44 | + when(mockedMethod.callAsync()).thenResolve(expectedResult); |
| 45 | + |
| 46 | + // Real call |
| 47 | + const result = await target.name(); |
| 48 | + // Result expectation |
| 49 | + expect(result).toBe(bytes32ToString(expectedResult)); |
| 50 | + // Verifications |
| 51 | + verify(mockedContract.name).once(); |
| 52 | + verify(mockedMethod.callAsync()).once(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('symbol', () => { |
| 57 | + test('should call to symbol', async () => { |
| 58 | + const expectedResult = stringToBytes32('string'); |
| 59 | + // Mocked method |
| 60 | + const mockedMethod = mock(MockedCallMethod); |
| 61 | + // Stub the method |
| 62 | + when(mockedContract.symbol).thenReturn(instance(mockedMethod)); |
| 63 | + // Stub the request |
| 64 | + when(mockedMethod.callAsync()).thenResolve(expectedResult); |
| 65 | + |
| 66 | + // Real call |
| 67 | + const result = await target.symbol(); |
| 68 | + // Result expectation |
| 69 | + expect(result).toBe(bytes32ToString(expectedResult)); |
| 70 | + // Verifications |
| 71 | + verify(mockedContract.symbol).once(); |
| 72 | + verify(mockedMethod.callAsync()).once(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('isValidContract', () => { |
| 77 | + test('should call to isValidContract', async () => { |
| 78 | + const expectedBNResult = new BigNumber(1); |
| 79 | + const expectedStringResult = stringToBytes32('string'); |
| 80 | + |
| 81 | + const mockedTotalSupplyMethod = mock(MockedCallMethod); |
| 82 | + when(mockedContract.totalSupply).thenReturn(instance(mockedTotalSupplyMethod)); |
| 83 | + when(mockedTotalSupplyMethod.callAsync()).thenResolve(expectedBNResult); |
| 84 | + |
| 85 | + const mockedSymbolMethod = mock(MockedCallMethod); |
| 86 | + when(mockedContract.symbol).thenReturn(instance(mockedSymbolMethod)); |
| 87 | + when(mockedSymbolMethod.callAsync()).thenResolve(expectedStringResult); |
| 88 | + |
| 89 | + const mockedNameMethod = mock(MockedCallMethod); |
| 90 | + when(mockedContract.name).thenReturn(instance(mockedNameMethod)); |
| 91 | + when(mockedSymbolMethod.callAsync()).thenResolve(expectedStringResult); |
| 92 | + |
| 93 | + const expectedIsValidResult = false; |
| 94 | + const result = await target.isValidContract(); |
| 95 | + expect(result).toBe(expectedIsValidResult); |
| 96 | + |
| 97 | + verify(mockedContract.totalSupply).once(); |
| 98 | + verify(mockedContract.symbol).once(); |
| 99 | + verify(mockedContract.name).once(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('SubscribeAsync', () => { |
| 104 | + test('should throw as eventName does not belong to SecurityTokenRegistryEvents', async () => { |
| 105 | + // Mocked parameters |
| 106 | + const mockedParams = { |
| 107 | + eventName: SecurityTokenRegistryEvents.ChangeExpiryLimit, |
| 108 | + indexFilterValues: {}, |
| 109 | + callback: () => {}, |
| 110 | + isVerbose: false, |
| 111 | + }; |
| 112 | + |
| 113 | + // Real call |
| 114 | + await expect(target.subscribeAsync(mockedParams)).rejects.toEqual( |
| 115 | + new Error(`Expected eventName to be one of: 'Transfer', 'Approval', encountered: ChangeExpiryLimit`), |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments