Skip to content

Commit

Permalink
fix: tests that needed to be updated to return from stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Wiebe committed Jan 30, 2020
1 parent bd37b9c commit 8b9860b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 29 deletions.
9 changes: 5 additions & 4 deletions src/procedures/__tests__/CreateCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@ describe('CreateCheckpoint', () => {

describe('createCheckpoint', () => {
test('should add a transaction to the queue to create a new checkpoint', async () => {
const createCheckpointArgsSpy = sinon.spy();
const createCheckpointArgsStub = sinon.stub();
createCheckpointArgsStub.returns([{}]);
const addTransactionStub = stub(target, 'addTransaction');
securityTokenMock.mock('createCheckpoint', Promise.resolve('CreateCheckpoint'));
const { createCheckpoint } = securityTokenMock.getMockInstance();
addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsSpy);
addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsStub);

// Real call
await target.prepareTransactions();

// Verifications
expect(createCheckpointArgsSpy.getCall(0).args[0]).toEqual({});
expect(createCheckpointArgsSpy.callCount).toEqual(1);
expect(createCheckpointArgsStub.getCall(0).args[0]).toEqual({});
expect(createCheckpointArgsStub.callCount).toEqual(1);
expect(
addTransactionStub
.getCall(0)
Expand Down
50 changes: 33 additions & 17 deletions src/procedures/__tests__/CreateDividendDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('CreateDividendDistribution', () => {
tokenAddress: params.erc20Address,
});
expect(approveErc20ArgsSpy.callCount).toBe(1);

expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({
maturity: params.maturityDate,
expiry: params.expiryDate,
Expand All @@ -149,27 +150,35 @@ describe('CreateDividendDistribution', () => {
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
PolyTransactionTag.CreateErc20DividendDistribution
);
expect(addTransactionStub.callCount).toEqual(1);

expect(addTransactionStub.callCount).toEqual(1);
expect(addProcedureStub.callCount).toEqual(1);
});

test('should send add a transaction to the queue to create arc20 dividend distribution with taxWitholding data', async () => {
const taxWithholdingAddress = '0x5555555555555555555555555555555555555555';
const taxWithholdingPercentage = 50;
target = new CreateDividendDistribution(
{
...params,
taxWithholdings: [
{
address: '0x5555555555555555555555555555555555555555',
percentage: 50,
address: taxWithholdingAddress,
percentage: taxWithholdingPercentage,
},
],
},
contextMock.getMockInstance()
);
const addProcedureSpy = spy(target, 'addProcedure');
const createDividendWithCheckpointAndExclusionsArgsSpy = sinon.spy();
const setWithholdingArgsSpy = sinon.spy();
const approveErc20ArgsSpy = sinon.spy();
const addProcedureStub = stub(target, 'addProcedure');
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);

const createDividendWithCheckpointAndExclusionsArgsStub = sinon.stub();
createDividendWithCheckpointAndExclusionsArgsStub.returns([{}]);
const setWithholdingArgsStub = sinon.stub();
setWithholdingArgsStub.returns([{}]);

const addTransactionStub = stub(target, 'addTransaction');
erc20DividendsMock.mock(
'createDividendWithCheckpointAndExclusions',
Expand All @@ -181,16 +190,22 @@ describe('CreateDividendDistribution', () => {
const { setWithholding } = erc20DividendsMock.getMockInstance();
addTransactionStub
.withArgs(createDividendWithCheckpointAndExclusions)
.returns(createDividendWithCheckpointAndExclusionsArgsSpy);
addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsSpy);
.returns(createDividendWithCheckpointAndExclusionsArgsStub);
addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsStub);

// Real call
await target.prepareTransactions();

// Verifications
expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
amount: params.amount,
spender: dividendsModuleAddress,
tokenAddress: params.erc20Address,
});
expect(approveErc20ArgsSpy.callCount).toBe(1);

expect(createDividendWithCheckpointAndExclusionsArgsSpy.getCall(0).args[0]).toEqual({
expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({
maturity: params.maturityDate,
expiry: params.expiryDate,
token: params.erc20Address,
Expand All @@ -199,7 +214,7 @@ describe('CreateDividendDistribution', () => {
name: params.name,
excluded: [],
});
expect(createDividendWithCheckpointAndExclusionsArgsSpy.callCount).toEqual(1);
expect(createDividendWithCheckpointAndExclusionsArgsStub.callCount).toEqual(1);
expect(
addTransactionStub
.getCall(0)
Expand All @@ -210,12 +225,12 @@ describe('CreateDividendDistribution', () => {
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
PolyTransactionTag.CreateErc20DividendDistribution
);
expect(setWithholdingArgsStub.getCall(0).args[0].investors).toEqual([taxWithholdingAddress]);
expect(setWithholdingArgsStub.getCall(0).args[0].withholding[0].toNumber()).toEqual(
taxWithholdingPercentage
);

expect(setWithholdingArgsSpy.getCall(0).args[0]).toEqual({
investors: ['0x5555555555555555555555555555555555555555'],
withholding: [50],
});
expect(setWithholdingArgsSpy.callCount).toEqual(1);
expect(setWithholdingArgsStub.callCount).toEqual(1);
expect(
addTransactionStub
.getCall(1)
Expand All @@ -224,8 +239,9 @@ describe('CreateDividendDistribution', () => {
expect(addTransactionStub.getCall(1).lastArg.tag).toEqual(
PolyTransactionTag.SetErc20TaxWithholding
);

expect(addTransactionStub.callCount).toEqual(2);
expect(addProcedureSpy.callCount).toEqual(1);
expect(addProcedureStub.callCount).toEqual(1);
});

test('should throw if there is no valid security token supplied', async () => {
Expand Down
78 changes: 70 additions & 8 deletions src/procedures/__tests__/CreateSecurityToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ describe('CreateSecurityToken', () => {
});

test('should add the transaction to the queue to create the security token and approve erc20 transfer', async () => {
const addProcedureSpy = spy(target, 'addProcedure');
const generateNewSecurityTokenArgsSpy = sinon.spy();
const approveErc20ArgsSpy = sinon.spy();
const addProcedureStub = stub(target, 'addProcedure');
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);

const generateNewSecurityTokenArgsStub = sinon.stub();
generateNewSecurityTokenArgsStub.returns([{}]);

const addTransactionStub = stub(target, 'addTransaction');
securityTokenRegistryMock.mock(
'generateNewSecurityToken',
Expand All @@ -162,21 +167,27 @@ describe('CreateSecurityToken', () => {
const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance();
addTransactionStub
.withArgs(generateNewSecurityToken)
.returns(generateNewSecurityTokenArgsSpy);
.returns(generateNewSecurityTokenArgsStub);

// Real call
await target.prepareTransactions();

// Verifications
expect(generateNewSecurityTokenArgsSpy.getCall(0).args[0]).toEqual({
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
amount: costInPoly,
spender: params.address,
});
expect(approveErc20ArgsSpy.callCount).toBe(1);

expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({
name: params.name,
ticker: params.symbol,
tokenDetails: '',
divisible: params.divisible,
protocolVersion: '0',
treasuryWallet: params.owner,
});
expect(generateNewSecurityTokenArgsSpy.callCount).toEqual(1);
expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1);

expect(
addTransactionStub
Expand All @@ -191,20 +202,71 @@ describe('CreateSecurityToken', () => {
PolyTransactionTag.CreateSecurityToken
);
expect(addTransactionStub.callCount).toEqual(1);
expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
expect(addProcedureSpy.callCount).toEqual(1);
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
expect(addProcedureStub.callCount).toEqual(1);
});

test('should add the transaction to the queue to create the security token with a treasury wallet', async () => {
const customTreasuryWallet = '0x5555555555555555555555555555555555555555';
target = new CreateSecurityToken(
{
...params,
treasuryWallet: '0x5', // Extra argument of treasuryWallet
treasuryWallet: customTreasuryWallet, // Extra argument of treasuryWallet
},
contextMock.getMockInstance()
);
const approveErc20ArgsSpy = sinon.spy();
const addProcedureStub = stub(target, 'addProcedure');
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);

const generateNewSecurityTokenArgsStub = sinon.stub();
generateNewSecurityTokenArgsStub.returns([{}]);

const addTransactionStub = stub(target, 'addTransaction');
securityTokenRegistryMock.mock(
'generateNewSecurityToken',
Promise.resolve('GenerateNewSecurityToken')
);
const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance();
addTransactionStub
.withArgs(generateNewSecurityToken)
.returns(generateNewSecurityTokenArgsStub);

// Real call
await target.prepareTransactions();

// Verifications
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
amount: costInPoly,
spender: params.address,
});
expect(approveErc20ArgsSpy.callCount).toBe(1);

expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({
name: params.name,
ticker: params.symbol,
tokenDetails: '',
divisible: params.divisible,
protocolVersion: '0',
treasuryWallet: customTreasuryWallet,
});
expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1);

expect(
addTransactionStub
.getCall(0)
.calledWith(securityTokenRegistryMock.getMockInstance().generateNewSecurityToken)
).toEqual(true);
expect(addTransactionStub.getCall(0).lastArg.fees).toEqual({
usd: costInUsd,
poly: costInPoly,
});
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
PolyTransactionTag.CreateSecurityToken
);
expect(addTransactionStub.callCount).toEqual(1);
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
expect(addProcedureStub.callCount).toEqual(1);
});
});
});

0 comments on commit 8b9860b

Please sign in to comment.