This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
3,201 additions
and
4 deletions.
There are no files selected for viewing
343 changes: 343 additions & 0 deletions
343
...ntract_wrappers/modules/transfer_manager/__tests__/count_transfer_manager_wrapper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
// CountTransferManager test | ||
import { mock, instance, reset, when, verify, objectContaining } from 'ts-mockito'; | ||
import { BigNumber } from '@0x/utils'; | ||
import { Web3Wrapper } from '@0x/web3-wrapper'; | ||
import { CountTransferManagerContract, SecurityTokenContract, PolyTokenEvents } from '@polymathnetwork/abi-wrappers'; | ||
import { getMockedPolyResponse, MockedCallMethod, MockedSendMethod } from '../../../../test_utils/mocked_methods'; | ||
import CountTransferManagerWrapper from '../count_transfer_manager_wrapper'; | ||
import ContractFactory from '../../../../factories/contractFactory'; | ||
import ModuleWrapper from '../../module_wrapper'; | ||
import { numberToBigNumber } from '../../../../utils/convert'; | ||
|
||
describe('CountTransferManagerWrapper', () => { | ||
let target: CountTransferManagerWrapper; | ||
let mockedWrapper: Web3Wrapper; | ||
let mockedContract: CountTransferManagerContract; | ||
let mockedContractFactory: ContractFactory; | ||
let mockedSecurityTokenContract: SecurityTokenContract; | ||
|
||
beforeAll(() => { | ||
mockedWrapper = mock(Web3Wrapper); | ||
mockedContract = mock(CountTransferManagerContract); | ||
mockedContractFactory = mock(ContractFactory); | ||
mockedSecurityTokenContract = mock(SecurityTokenContract); | ||
|
||
const myContractPromise = Promise.resolve(instance(mockedContract)); | ||
target = new CountTransferManagerWrapper( | ||
instance(mockedWrapper), | ||
myContractPromise, | ||
instance(mockedContractFactory), | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
reset(mockedWrapper); | ||
reset(mockedContract); | ||
reset(mockedContractFactory); | ||
reset(mockedSecurityTokenContract); | ||
}); | ||
|
||
describe('Types', () => { | ||
test('should extend ModuleWrapper', async () => { | ||
expect(target instanceof ModuleWrapper).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Paused', () => { | ||
test('should get isPaused', async () => { | ||
// Address expected | ||
const expectedResult = true; | ||
// Mocked method | ||
const mockedMethod = mock(MockedCallMethod); | ||
// Stub the method | ||
when(mockedContract.paused).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when(mockedMethod.callAsync()).thenResolve(expectedResult); | ||
|
||
// Real call | ||
const result = await target.paused(); | ||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.paused).once(); | ||
verify(mockedMethod.callAsync()).once(); | ||
}); | ||
}); | ||
|
||
describe('Pause/Unpause', () => { | ||
test('should call to pause', async () => { | ||
// Pause Result expected | ||
const expectedPauseResult = false; | ||
// Mocked method | ||
const mockedPauseMethod = mock(MockedCallMethod); | ||
// Stub the method | ||
when(mockedContract.paused).thenReturn(instance(mockedPauseMethod)); | ||
// Stub the request | ||
when(mockedPauseMethod.callAsync()).thenResolve(expectedPauseResult); | ||
|
||
// Owner Address expected | ||
const expectedOwnerResult = '0x5555555555555555555555555555555555555555'; | ||
// Security Token Address expected | ||
const expectedSecurityTokenAddress = '0x3333333333333333333333333333333333333333'; | ||
|
||
// Setup get Security Token Address | ||
const mockedGetSecurityTokenAddressMethod = mock(MockedCallMethod); | ||
when(mockedContract.securityToken).thenReturn(instance(mockedGetSecurityTokenAddressMethod)); | ||
when(mockedGetSecurityTokenAddressMethod.callAsync()).thenResolve(expectedSecurityTokenAddress); | ||
|
||
when(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).thenResolve( | ||
instance(mockedSecurityTokenContract), | ||
); | ||
const mockedSecurityTokenOwnerMethod = mock(MockedCallMethod); | ||
when(mockedSecurityTokenOwnerMethod.callAsync()).thenResolve(expectedOwnerResult); | ||
when(mockedSecurityTokenContract.owner).thenReturn(instance(mockedSecurityTokenOwnerMethod)); | ||
|
||
// Mock web3 wrapper owner | ||
when(mockedWrapper.getAvailableAddressesAsync()).thenResolve([expectedOwnerResult]); | ||
|
||
const mockedParams = { | ||
txData: {}, | ||
safetyFactor: 10, | ||
}; | ||
const expectedResult = getMockedPolyResponse(); | ||
// Mocked method | ||
const mockedMethod = mock(MockedSendMethod); | ||
// Stub the method | ||
when(mockedContract.pause).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).thenResolve( | ||
expectedResult, | ||
); | ||
|
||
// Real call | ||
const result = await target.pause(mockedParams); | ||
|
||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.pause).once(); | ||
verify(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).once(); | ||
verify(mockedContract.paused).once(); | ||
verify(mockedPauseMethod.callAsync()).once(); | ||
verify(mockedContract.securityToken).once(); | ||
verify(mockedGetSecurityTokenAddressMethod.callAsync()).once(); | ||
verify(mockedSecurityTokenOwnerMethod.callAsync()).once(); | ||
verify(mockedSecurityTokenContract.owner).once(); | ||
verify(mockedWrapper.getAvailableAddressesAsync()).once(); | ||
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).once(); | ||
}); | ||
|
||
test('should call to unpause', async () => { | ||
// Pause Result expected | ||
const expectedPauseResult = true; | ||
// Mocked method | ||
const mockedPauseMethod = mock(MockedCallMethod); | ||
// Stub the method | ||
when(mockedContract.paused).thenReturn(instance(mockedPauseMethod)); | ||
// Stub the request | ||
when(mockedPauseMethod.callAsync()).thenResolve(expectedPauseResult); | ||
|
||
// Owner Address expected | ||
const expectedOwnerResult = '0x5555555555555555555555555555555555555555'; | ||
// Security Token Address expected | ||
const expectedSecurityTokenAddress = '0x3333333333333333333333333333333333333333'; | ||
|
||
// Setup get Security Token Address | ||
const mockedGetSecurityTokenAddressMethod = mock(MockedCallMethod); | ||
when(mockedContract.securityToken).thenReturn(instance(mockedGetSecurityTokenAddressMethod)); | ||
when(mockedGetSecurityTokenAddressMethod.callAsync()).thenResolve(expectedSecurityTokenAddress); | ||
|
||
when(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).thenResolve( | ||
instance(mockedSecurityTokenContract), | ||
); | ||
const mockedSecurityTokenOwnerMethod = mock(MockedCallMethod); | ||
when(mockedSecurityTokenOwnerMethod.callAsync()).thenResolve(expectedOwnerResult); | ||
when(mockedSecurityTokenContract.owner).thenReturn(instance(mockedSecurityTokenOwnerMethod)); | ||
|
||
// Mock web3 wrapper owner | ||
when(mockedWrapper.getAvailableAddressesAsync()).thenResolve([expectedOwnerResult]); | ||
|
||
const mockedParams = { | ||
txData: {}, | ||
safetyFactor: 10, | ||
}; | ||
const expectedResult = getMockedPolyResponse(); | ||
// Mocked method | ||
const mockedMethod = mock(MockedSendMethod); | ||
// Stub the method | ||
when(mockedContract.unpause).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).thenResolve( | ||
expectedResult, | ||
); | ||
|
||
// Real call | ||
const result = await target.unpause(mockedParams); | ||
|
||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.unpause).once(); | ||
verify(mockedMethod.sendTransactionAsync(mockedParams.txData, mockedParams.safetyFactor)).once(); | ||
verify(mockedContract.paused).once(); | ||
verify(mockedPauseMethod.callAsync()).once(); | ||
verify(mockedContract.securityToken).once(); | ||
verify(mockedGetSecurityTokenAddressMethod.callAsync()).once(); | ||
verify(mockedSecurityTokenOwnerMethod.callAsync()).once(); | ||
verify(mockedSecurityTokenContract.owner).once(); | ||
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).once(); | ||
verify(mockedWrapper.getAvailableAddressesAsync()).once(); | ||
}); | ||
}); | ||
|
||
describe('MaxHolderCount', () => { | ||
test('should get maxHolderCount', async () => { | ||
// Address expected | ||
const expectedResult = new BigNumber(1); | ||
// Mocked method | ||
const mockedMethod = mock(MockedCallMethod); | ||
// Stub the method | ||
when(mockedContract.maxHolderCount).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when(mockedMethod.callAsync()).thenResolve(expectedResult); | ||
|
||
// Real call | ||
const result = await target.maxHolderCount(); | ||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.maxHolderCount).once(); | ||
verify(mockedMethod.callAsync()).once(); | ||
}); | ||
}); | ||
|
||
describe('VerifyTransfer', () => { | ||
test('should verify Transfer', async () => { | ||
const mockedParams = { | ||
from: '0x1111111111111111111111111111111111111111', | ||
to: '0x2222222222222222222222222222222222222222', | ||
amount: new BigNumber(10), | ||
data: 'Data', | ||
isTransfer: true, | ||
txData: {}, | ||
safetyFactor: 10, | ||
}; | ||
const expectedResult = getMockedPolyResponse(); | ||
// Mocked method | ||
const mockedMethod = mock(MockedSendMethod); | ||
// Stub the method | ||
when(mockedContract.verifyTransfer).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when( | ||
mockedMethod.sendTransactionAsync( | ||
mockedParams.from, | ||
mockedParams.to, | ||
objectContaining(mockedParams.amount), | ||
mockedParams.data, | ||
mockedParams.isTransfer, | ||
mockedParams.txData, | ||
mockedParams.safetyFactor, | ||
), | ||
).thenResolve(expectedResult); | ||
|
||
// Real call | ||
const result = await target.verifyTransfer(mockedParams); | ||
|
||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.verifyTransfer).once(); | ||
verify( | ||
mockedMethod.sendTransactionAsync( | ||
mockedParams.from, | ||
mockedParams.to, | ||
objectContaining(mockedParams.amount), | ||
mockedParams.data, | ||
mockedParams.isTransfer, | ||
mockedParams.txData, | ||
mockedParams.safetyFactor, | ||
), | ||
).once(); | ||
}); | ||
}); | ||
|
||
describe('Change Holder Count', () => { | ||
test('should change holder count', async () => { | ||
// Owner Address expected | ||
const expectedOwnerResult = '0x5555555555555555555555555555555555555555'; | ||
// Security Token Address expected | ||
const expectedSecurityTokenAddress = '0x3333333333333333333333333333333333333333'; | ||
|
||
// Setup get Security Token Address | ||
const mockedGetSecurityTokenAddressMethod = mock(MockedCallMethod); | ||
when(mockedContract.securityToken).thenReturn(instance(mockedGetSecurityTokenAddressMethod)); | ||
when(mockedGetSecurityTokenAddressMethod.callAsync()).thenResolve(expectedSecurityTokenAddress); | ||
when(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).thenResolve( | ||
instance(mockedSecurityTokenContract), | ||
); | ||
const mockedSecurityTokenOwnerMethod = mock(MockedCallMethod); | ||
when(mockedSecurityTokenOwnerMethod.callAsync()).thenResolve(expectedOwnerResult); | ||
when(mockedSecurityTokenContract.owner).thenReturn(instance(mockedSecurityTokenOwnerMethod)); | ||
|
||
// Mock web3 wrapper owner | ||
when(mockedWrapper.getAvailableAddressesAsync()).thenResolve([expectedOwnerResult]); | ||
|
||
const mockedParams = { | ||
maxHolderCount: 20, | ||
txData: {}, | ||
safetyFactor: 10, | ||
}; | ||
const expectedResult = getMockedPolyResponse(); | ||
// Mocked method | ||
const mockedMethod = mock(MockedSendMethod); | ||
// Stub the method | ||
when(mockedContract.changeHolderCount).thenReturn(instance(mockedMethod)); | ||
// Stub the request | ||
when( | ||
mockedMethod.sendTransactionAsync( | ||
objectContaining(numberToBigNumber(mockedParams.maxHolderCount)), | ||
mockedParams.txData, | ||
mockedParams.safetyFactor, | ||
), | ||
).thenResolve(expectedResult); | ||
|
||
// Real call | ||
const result = await target.changeHolderCount(mockedParams); | ||
|
||
// Result expectation | ||
expect(result).toBe(expectedResult); | ||
// Verifications | ||
verify(mockedContract.changeHolderCount).once(); | ||
verify( | ||
mockedMethod.sendTransactionAsync( | ||
objectContaining(numberToBigNumber(mockedParams.maxHolderCount)), | ||
mockedParams.txData, | ||
mockedParams.safetyFactor, | ||
), | ||
).once(); | ||
verify(mockedSecurityTokenOwnerMethod.callAsync()).once(); | ||
verify(mockedSecurityTokenContract.owner).once(); | ||
verify(mockedContract.securityToken).once(); | ||
verify(mockedGetSecurityTokenAddressMethod.callAsync()).once(); | ||
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).once(); | ||
verify(mockedWrapper.getAvailableAddressesAsync()).once(); | ||
}); | ||
}); | ||
|
||
describe('SubscribeAsync', () => { | ||
test('should throw as eventName does not belong to CountTransferManager', async () => { | ||
// Mocked parameters | ||
const mockedParams = { | ||
eventName: PolyTokenEvents.Transfer, | ||
indexFilterValues: {}, | ||
callback: () => {}, | ||
isVerbose: false, | ||
}; | ||
|
||
// Real call | ||
await expect(target.subscribeAsync(mockedParams)).rejects.toEqual( | ||
new Error(`Expected eventName to be one of: 'ModifyHolderCount', 'Pause', 'Unpause', encountered: Transfer`), | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.