Skip to content

Commit 1b0035f

Browse files
committed
refactor: move IcpAgent stubs to beforeEach for better test isolation
TICKET: WIN-5302
1 parent 3724553 commit 1b0035f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

modules/sdk-coin-icp/test/unit/transactionBuilder/transactionRecover.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ describe('ICP transaction recovery', async () => {
4040
broadcastResponse = Buffer.from(testData.PublicNodeApiBroadcastResponse, 'hex');
4141
});
4242

43+
beforeEach(function () {
44+
// Common stubs for IcpAgent
45+
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
46+
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
47+
});
48+
4349
afterEach(function () {
4450
recoveryParams = {
4551
userKey: testData.WRWRecovery.userKey,
@@ -66,8 +72,6 @@ describe('ICP transaction recovery', async () => {
6672
});
6773

6874
const body = testData.RecoverySignedTransactionWithDefaultMemo;
69-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
70-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
7175
nock(nodeUrl).post(broadcastEndpoint, body).reply(200, broadcastResponse);
7276
const recoverTxn = await icp.recover(recoveryParams);
7377
recoverTxn.id.should.be.a.String();
@@ -88,8 +92,6 @@ describe('ICP transaction recovery', async () => {
8892
});
8993

9094
const body = testData.RecoverySignedTransactionWithMemo;
91-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
92-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
9395
nock(nodeUrl).post(broadcastEndpoint, body).reply(200, broadcastResponse);
9496
recoveryParams.memo = testData.MetaDataWithMemo.memo;
9597
const recoverTxn = await icp.recover(recoveryParams);
@@ -109,8 +111,6 @@ describe('ICP transaction recovery', async () => {
109111
ingressEndTime: testData.MetaDataWithMemo.ingress_end,
110112
});
111113

112-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
113-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
114114
const unsignedSweepRecoveryParams = {
115115
bitgoKey:
116116
'0310768736a005ea5364e1b5b5288cf553224dd28b2df8ced63b72a8020478967f05ec5bce1f26cd7eb009a4bea445bb55c2f54a30f2706c1a3747e8df2d288829',
@@ -133,8 +133,6 @@ describe('ICP transaction recovery', async () => {
133133
ingressEndTime: testData.MetaDataWithMemo.ingress_end,
134134
});
135135

136-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
137-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
138136
const unsignedSweepRecoveryParams = {
139137
bitgoKey: 'testKey',
140138
recoveryDestination: testData.Accounts.account2.address,
@@ -156,17 +154,13 @@ describe('ICP transaction recovery', async () => {
156154
ingressEndTime: testData.MetaDataWithMemo.ingress_end,
157155
});
158156

159-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
160-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
161157
const unsignedSweepRecoveryParams = {
162158
recoveryDestination: testData.Accounts.account2.address,
163159
};
164160
await icp.recover(unsignedSweepRecoveryParams).should.rejectedWith('Error during ICP recovery: missing bitgoKey');
165161
});
166162

167163
it('should fail to recover if broadcast API fails', async () => {
168-
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(1000000000));
169-
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
170164
nock(nodeUrl).post(broadcastEndpoint).reply(500, 'Internal Server Error');
171165
recoveryParams.memo = 0;
172166
await icp
@@ -177,6 +171,8 @@ describe('ICP transaction recovery', async () => {
177171
});
178172

179173
it('should fail to recover txn if balance is low', async () => {
174+
// Override the default balance stub for this specific test
175+
sinon.restore();
180176
sinon.stub(IcpAgent.prototype, 'getBalance').resolves(BigNumber(10));
181177
sinon.stub(IcpAgent.prototype, 'getFee').resolves(BigNumber(10000));
182178
nock(nodeUrl).post(broadcastEndpoint).reply(200, broadcastResponse);

0 commit comments

Comments
 (0)