Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Merge 7255be7 into 071b9fd
Browse files Browse the repository at this point in the history
  • Loading branch information
onigiri-x committed May 30, 2019
2 parents 071b9fd + 7255be7 commit 2ca01d4
Show file tree
Hide file tree
Showing 10 changed files with 3,201 additions and 4 deletions.
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`),
);
});
});
});

0 comments on commit 2ca01d4

Please sign in to comment.