Skip to content

Commit 8b9860b

Browse files
author
Victor Wiebe
committed
fix: tests that needed to be updated to return from stub
1 parent bd37b9c commit 8b9860b

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

src/procedures/__tests__/CreateCheckpoint.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,19 @@ describe('CreateCheckpoint', () => {
8989

9090
describe('createCheckpoint', () => {
9191
test('should add a transaction to the queue to create a new checkpoint', async () => {
92-
const createCheckpointArgsSpy = sinon.spy();
92+
const createCheckpointArgsStub = sinon.stub();
93+
createCheckpointArgsStub.returns([{}]);
9394
const addTransactionStub = stub(target, 'addTransaction');
9495
securityTokenMock.mock('createCheckpoint', Promise.resolve('CreateCheckpoint'));
9596
const { createCheckpoint } = securityTokenMock.getMockInstance();
96-
addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsSpy);
97+
addTransactionStub.withArgs(createCheckpoint).returns(createCheckpointArgsStub);
9798

9899
// Real call
99100
await target.prepareTransactions();
100101

101102
// Verifications
102-
expect(createCheckpointArgsSpy.getCall(0).args[0]).toEqual({});
103-
expect(createCheckpointArgsSpy.callCount).toEqual(1);
103+
expect(createCheckpointArgsStub.getCall(0).args[0]).toEqual({});
104+
expect(createCheckpointArgsStub.callCount).toEqual(1);
104105
expect(
105106
addTransactionStub
106107
.getCall(0)

src/procedures/__tests__/CreateDividendDistribution.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ describe('CreateDividendDistribution', () => {
127127
tokenAddress: params.erc20Address,
128128
});
129129
expect(approveErc20ArgsSpy.callCount).toBe(1);
130+
130131
expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({
131132
maturity: params.maturityDate,
132133
expiry: params.expiryDate,
@@ -149,27 +150,35 @@ describe('CreateDividendDistribution', () => {
149150
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
150151
PolyTransactionTag.CreateErc20DividendDistribution
151152
);
152-
expect(addTransactionStub.callCount).toEqual(1);
153153

154+
expect(addTransactionStub.callCount).toEqual(1);
154155
expect(addProcedureStub.callCount).toEqual(1);
155156
});
156157

157158
test('should send add a transaction to the queue to create arc20 dividend distribution with taxWitholding data', async () => {
159+
const taxWithholdingAddress = '0x5555555555555555555555555555555555555555';
160+
const taxWithholdingPercentage = 50;
158161
target = new CreateDividendDistribution(
159162
{
160163
...params,
161164
taxWithholdings: [
162165
{
163-
address: '0x5555555555555555555555555555555555555555',
164-
percentage: 50,
166+
address: taxWithholdingAddress,
167+
percentage: taxWithholdingPercentage,
165168
},
166169
],
167170
},
168171
contextMock.getMockInstance()
169172
);
170-
const addProcedureSpy = spy(target, 'addProcedure');
171-
const createDividendWithCheckpointAndExclusionsArgsSpy = sinon.spy();
172-
const setWithholdingArgsSpy = sinon.spy();
173+
const approveErc20ArgsSpy = sinon.spy();
174+
const addProcedureStub = stub(target, 'addProcedure');
175+
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);
176+
177+
const createDividendWithCheckpointAndExclusionsArgsStub = sinon.stub();
178+
createDividendWithCheckpointAndExclusionsArgsStub.returns([{}]);
179+
const setWithholdingArgsStub = sinon.stub();
180+
setWithholdingArgsStub.returns([{}]);
181+
173182
const addTransactionStub = stub(target, 'addTransaction');
174183
erc20DividendsMock.mock(
175184
'createDividendWithCheckpointAndExclusions',
@@ -181,16 +190,22 @@ describe('CreateDividendDistribution', () => {
181190
const { setWithholding } = erc20DividendsMock.getMockInstance();
182191
addTransactionStub
183192
.withArgs(createDividendWithCheckpointAndExclusions)
184-
.returns(createDividendWithCheckpointAndExclusionsArgsSpy);
185-
addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsSpy);
193+
.returns(createDividendWithCheckpointAndExclusionsArgsStub);
194+
addTransactionStub.withArgs(setWithholding).returns(setWithholdingArgsStub);
186195

187196
// Real call
188197
await target.prepareTransactions();
189198

190199
// Verifications
191-
expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
200+
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
201+
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
202+
amount: params.amount,
203+
spender: dividendsModuleAddress,
204+
tokenAddress: params.erc20Address,
205+
});
206+
expect(approveErc20ArgsSpy.callCount).toBe(1);
192207

193-
expect(createDividendWithCheckpointAndExclusionsArgsSpy.getCall(0).args[0]).toEqual({
208+
expect(createDividendWithCheckpointAndExclusionsArgsStub.getCall(0).args[0]).toEqual({
194209
maturity: params.maturityDate,
195210
expiry: params.expiryDate,
196211
token: params.erc20Address,
@@ -199,7 +214,7 @@ describe('CreateDividendDistribution', () => {
199214
name: params.name,
200215
excluded: [],
201216
});
202-
expect(createDividendWithCheckpointAndExclusionsArgsSpy.callCount).toEqual(1);
217+
expect(createDividendWithCheckpointAndExclusionsArgsStub.callCount).toEqual(1);
203218
expect(
204219
addTransactionStub
205220
.getCall(0)
@@ -210,12 +225,12 @@ describe('CreateDividendDistribution', () => {
210225
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
211226
PolyTransactionTag.CreateErc20DividendDistribution
212227
);
228+
expect(setWithholdingArgsStub.getCall(0).args[0].investors).toEqual([taxWithholdingAddress]);
229+
expect(setWithholdingArgsStub.getCall(0).args[0].withholding[0].toNumber()).toEqual(
230+
taxWithholdingPercentage
231+
);
213232

214-
expect(setWithholdingArgsSpy.getCall(0).args[0]).toEqual({
215-
investors: ['0x5555555555555555555555555555555555555555'],
216-
withholding: [50],
217-
});
218-
expect(setWithholdingArgsSpy.callCount).toEqual(1);
233+
expect(setWithholdingArgsStub.callCount).toEqual(1);
219234
expect(
220235
addTransactionStub
221236
.getCall(1)
@@ -224,8 +239,9 @@ describe('CreateDividendDistribution', () => {
224239
expect(addTransactionStub.getCall(1).lastArg.tag).toEqual(
225240
PolyTransactionTag.SetErc20TaxWithholding
226241
);
242+
227243
expect(addTransactionStub.callCount).toEqual(2);
228-
expect(addProcedureSpy.callCount).toEqual(1);
244+
expect(addProcedureStub.callCount).toEqual(1);
229245
});
230246

231247
test('should throw if there is no valid security token supplied', async () => {

src/procedures/__tests__/CreateSecurityToken.ts

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,13 @@ describe('CreateSecurityToken', () => {
152152
});
153153

154154
test('should add the transaction to the queue to create the security token and approve erc20 transfer', async () => {
155-
const addProcedureSpy = spy(target, 'addProcedure');
156-
const generateNewSecurityTokenArgsSpy = sinon.spy();
155+
const approveErc20ArgsSpy = sinon.spy();
156+
const addProcedureStub = stub(target, 'addProcedure');
157+
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);
158+
159+
const generateNewSecurityTokenArgsStub = sinon.stub();
160+
generateNewSecurityTokenArgsStub.returns([{}]);
161+
157162
const addTransactionStub = stub(target, 'addTransaction');
158163
securityTokenRegistryMock.mock(
159164
'generateNewSecurityToken',
@@ -162,21 +167,27 @@ describe('CreateSecurityToken', () => {
162167
const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance();
163168
addTransactionStub
164169
.withArgs(generateNewSecurityToken)
165-
.returns(generateNewSecurityTokenArgsSpy);
170+
.returns(generateNewSecurityTokenArgsStub);
166171

167172
// Real call
168173
await target.prepareTransactions();
169174

170175
// Verifications
171-
expect(generateNewSecurityTokenArgsSpy.getCall(0).args[0]).toEqual({
176+
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
177+
amount: costInPoly,
178+
spender: params.address,
179+
});
180+
expect(approveErc20ArgsSpy.callCount).toBe(1);
181+
182+
expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({
172183
name: params.name,
173184
ticker: params.symbol,
174185
tokenDetails: '',
175186
divisible: params.divisible,
176187
protocolVersion: '0',
177188
treasuryWallet: params.owner,
178189
});
179-
expect(generateNewSecurityTokenArgsSpy.callCount).toEqual(1);
190+
expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1);
180191

181192
expect(
182193
addTransactionStub
@@ -191,20 +202,71 @@ describe('CreateSecurityToken', () => {
191202
PolyTransactionTag.CreateSecurityToken
192203
);
193204
expect(addTransactionStub.callCount).toEqual(1);
194-
expect(addProcedureSpy.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
195-
expect(addProcedureSpy.callCount).toEqual(1);
205+
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
206+
expect(addProcedureStub.callCount).toEqual(1);
196207
});
197208

198209
test('should add the transaction to the queue to create the security token with a treasury wallet', async () => {
210+
const customTreasuryWallet = '0x5555555555555555555555555555555555555555';
199211
target = new CreateSecurityToken(
200212
{
201213
...params,
202-
treasuryWallet: '0x5', // Extra argument of treasuryWallet
214+
treasuryWallet: customTreasuryWallet, // Extra argument of treasuryWallet
203215
},
204216
contextMock.getMockInstance()
205217
);
218+
const approveErc20ArgsSpy = sinon.spy();
219+
const addProcedureStub = stub(target, 'addProcedure');
220+
addProcedureStub.withArgs(ApproveErc20).returns(approveErc20ArgsSpy);
221+
222+
const generateNewSecurityTokenArgsStub = sinon.stub();
223+
generateNewSecurityTokenArgsStub.returns([{}]);
224+
225+
const addTransactionStub = stub(target, 'addTransaction');
226+
securityTokenRegistryMock.mock(
227+
'generateNewSecurityToken',
228+
Promise.resolve('GenerateNewSecurityToken')
229+
);
230+
const { generateNewSecurityToken } = securityTokenRegistryMock.getMockInstance();
231+
addTransactionStub
232+
.withArgs(generateNewSecurityToken)
233+
.returns(generateNewSecurityTokenArgsStub);
234+
206235
// Real call
207236
await target.prepareTransactions();
237+
238+
// Verifications
239+
expect(approveErc20ArgsSpy.getCall(0).args[0]).toEqual({
240+
amount: costInPoly,
241+
spender: params.address,
242+
});
243+
expect(approveErc20ArgsSpy.callCount).toBe(1);
244+
245+
expect(generateNewSecurityTokenArgsStub.getCall(0).args[0]).toEqual({
246+
name: params.name,
247+
ticker: params.symbol,
248+
tokenDetails: '',
249+
divisible: params.divisible,
250+
protocolVersion: '0',
251+
treasuryWallet: customTreasuryWallet,
252+
});
253+
expect(generateNewSecurityTokenArgsStub.callCount).toEqual(1);
254+
255+
expect(
256+
addTransactionStub
257+
.getCall(0)
258+
.calledWith(securityTokenRegistryMock.getMockInstance().generateNewSecurityToken)
259+
).toEqual(true);
260+
expect(addTransactionStub.getCall(0).lastArg.fees).toEqual({
261+
usd: costInUsd,
262+
poly: costInPoly,
263+
});
264+
expect(addTransactionStub.getCall(0).lastArg.tag).toEqual(
265+
PolyTransactionTag.CreateSecurityToken
266+
);
267+
expect(addTransactionStub.callCount).toEqual(1);
268+
expect(addProcedureStub.getCall(0).calledWithExactly(ApproveErc20)).toEqual(true);
269+
expect(addProcedureStub.callCount).toEqual(1);
208270
});
209271
});
210272
});

0 commit comments

Comments
 (0)