Skip to content

Commit ef447ab

Browse files
committed
test: update types in the unit tests
1 parent 58b2730 commit ef447ab

10 files changed

+73
-45
lines changed

app/src/__tests__/components/loop/LoopPage.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { runInAction } from 'mobx';
33
import { SwapStatus } from 'types/generated/loop_pb';
44
import { grpc } from '@improbable-eng/grpc-web';
55
import { fireEvent, waitFor } from '@testing-library/react';
6+
import Big from 'big.js';
67
import { saveAs } from 'file-saver';
78
import { formatSats } from 'util/formatters';
89
import { renderWithProviders } from 'util/tests';
@@ -70,9 +71,9 @@ describe('LoopPage component', () => {
7071
const { findByText } = render();
7172
// convert from numeric timestamp to string (1586390353623905000 -> '4/15/2020')
7273
const formatDate = (s: SwapStatus.AsObject) =>
73-
new Date(s.initiationTime / 1000 / 1000).toLocaleDateString();
74+
new Date(+Big(s.initiationTime).div(1000).div(1000)).toLocaleDateString();
7475
const [swap1, swap2] = loopListSwaps.swapsList.sort(
75-
(a, b) => b.initiationTime - a.initiationTime,
76+
(a, b) => +Big(b.initiationTime).sub(a.initiationTime),
7677
);
7778

7879
expect(await findByText(formatDate(swap1))).toBeInTheDocument();

app/src/__tests__/components/loop/ProcessingSwaps.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { runInAction } from 'mobx';
33
import * as LOOP from 'types/generated/loop_pb';
44
import { fireEvent } from '@testing-library/react';
5+
import Big from 'big.js';
56
import { renderWithProviders } from 'util/tests';
67
import { loopListSwaps } from 'util/tests/sampleData';
78
import { createStore, Store } from 'store';
@@ -27,7 +28,7 @@ describe('ProcessingSwaps component', () => {
2728
swap.id = `${id || ''}${swap.id}`;
2829
swap.type = type;
2930
swap.state = state;
30-
swap.lastUpdateTime = Date.now() * 1000 * 1000;
31+
swap.lastUpdateTime = Big(Date.now() * 1000 * 1000);
3132
runInAction(() => {
3233
store.swapStore.swaps.set(swap.id, swap);
3334
});

app/src/__tests__/components/pool/AccountSection.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('AccountSection', () => {
117117
});
118118

119119
expect(store.accountStore.activeTraderKey).toBe(hex(poolInitAccount.traderKey));
120-
expect(store.fundNewAccountView.amount).toBe(0);
120+
expect(+store.fundNewAccountView.amount).toBe(0);
121121
expect(store.fundNewAccountView.confTarget).toBe(DEFAULT_CONF_TARGET);
122122
expect(store.fundNewAccountView.expireBlocks).toBe(DEFAULT_EXPIRE_BLOCKS);
123123
});
@@ -206,7 +206,7 @@ describe('AccountSection', () => {
206206
expect(getByText('Account')).toBeInTheDocument();
207207
});
208208

209-
expect(+store.accountStore.activeAccount.totalBalance).toBe(
209+
expect(store.accountStore.activeAccount.totalBalance.toString()).toBe(
210210
poolDepositAccount.account.value,
211211
);
212212
expect(store.fundAccountView.amount).toBe(0);
@@ -322,7 +322,7 @@ describe('AccountSection', () => {
322322
});
323323

324324
expect(req!.traderKey).toBe(b64(store.accountStore.activeAccount.traderKey));
325-
expect(req!.outputWithFee?.feeRateSatPerKw).toBe(2500);
325+
expect(req!.outputWithFee?.feeRateSatPerKw).toBe('2500');
326326
expect(req!.outputWithFee?.address).toBe('abc123');
327327
});
328328

@@ -375,7 +375,7 @@ describe('AccountSection', () => {
375375
});
376376

377377
expect(req!.accountKey).toBe(b64(store.accountStore.activeAccount.traderKey));
378-
expect(req!.feeRateSatPerKw).toBe(31250);
378+
expect(req!.feeRateSatPerKw).toBe('31250');
379379
expect(req!.relativeExpiry).toBe(2016);
380380
});
381381
});

app/src/__tests__/components/pool/BatchStats.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { runInAction } from 'mobx';
33
import { act, waitFor } from '@testing-library/react';
4+
import Big from 'big.js';
45
import { formatSats } from 'util/formatters';
56
import { renderWithProviders } from 'util/tests';
67
import { createStore, Store } from 'store';
@@ -25,7 +26,7 @@ describe('BatchStats', () => {
2526
jest.useFakeTimers();
2627
runInAction(() => {
2728
const nowSecs = Math.ceil(Date.now() / 1000);
28-
store.batchStore.nextBatchTimestamp = nowSecs + 90;
29+
store.batchStore.nextBatchTimestamp = Big(nowSecs + 90);
2930
});
3031
const { getByText } = render();
3132
expect(getByText('Next Batch')).toBeInTheDocument();

app/src/__tests__/components/pool/OrderFormSection.spec.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ describe('OrderFormSection', () => {
7171
});
7272

7373
fireEvent.click(getByText('Place Bid Order'));
74-
expect(bid!.details.amt).toBe(1000000);
74+
expect(bid!.details.amt).toBe('1000000');
7575
expect(bid!.details.rateFixed).toBe(4960);
7676
expect(bid!.details.minUnitsMatch).toBe(1);
7777
expect(bid!.leaseDurationBlocks).toBe(2016);
7878
expect(bid!.minNodeTier).toBe(1);
79-
expect(bid!.details.maxBatchFeeRateSatPerKw).toBe(253);
79+
expect(bid!.details.maxBatchFeeRateSatPerKw).toBe('253');
8080
});
8181

8282
it('should submit an ask order', async () => {
@@ -95,11 +95,11 @@ describe('OrderFormSection', () => {
9595
});
9696

9797
fireEvent.click(getByText('Place Ask Order'));
98-
expect(ask!.details.amt).toBe(1000000);
98+
expect(ask!.details.amt).toBe('1000000');
9999
expect(ask!.details.rateFixed).toBe(4960);
100100
expect(ask!.details.minUnitsMatch).toBe(1);
101101
expect(ask!.leaseDurationBlocks).toBe(2016);
102-
expect(ask!.details.maxBatchFeeRateSatPerKw).toBe(253);
102+
expect(ask!.details.maxBatchFeeRateSatPerKw).toBe('253');
103103
});
104104

105105
it('should submit an order with a different lease duration', async () => {
@@ -119,12 +119,12 @@ describe('OrderFormSection', () => {
119119
});
120120

121121
fireEvent.click(getByText('Place Bid Order'));
122-
expect(bid!.details.amt).toBe(1000000);
122+
expect(bid!.details.amt).toBe('1000000');
123123
expect(bid!.details.rateFixed).toBe(2480);
124124
expect(bid!.details.minUnitsMatch).toBe(1);
125125
expect(bid!.leaseDurationBlocks).toBe(4032);
126126
expect(bid!.minNodeTier).toBe(1);
127-
expect(bid!.details.maxBatchFeeRateSatPerKw).toBe(253);
127+
expect(bid!.details.maxBatchFeeRateSatPerKw).toBe('253');
128128
});
129129

130130
it('should reset the form after placing an order', async () => {

app/src/__tests__/store/accountStore.spec.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ describe('AccountStore', () => {
4646
});
4747

4848
it('should return sorted accounts', async () => {
49-
const a = new Account(rootStore, { ...poolInitAccount, value: 300 });
50-
const b = new Account(rootStore, { ...poolInitAccount, value: 100 });
49+
const a = new Account(rootStore, { ...poolInitAccount, value: '300' });
50+
const b = new Account(rootStore, { ...poolInitAccount, value: '100' });
5151
const c = new Account(rootStore, {
5252
...poolInitAccount,
5353
expirationHeight: 5000,
@@ -76,8 +76,8 @@ describe('AccountStore', () => {
7676
});
7777

7878
it('should excluded closed accounts in sorted accounts', async () => {
79-
const a = new Account(rootStore, { ...poolInitAccount, value: 300 });
80-
const b = new Account(rootStore, { ...poolInitAccount, value: 100 });
79+
const a = new Account(rootStore, { ...poolInitAccount, value: '300' });
80+
const b = new Account(rootStore, { ...poolInitAccount, value: '100' });
8181
const c = new Account(rootStore, {
8282
...poolInitAccount,
8383
expirationHeight: 5000,
@@ -158,7 +158,7 @@ describe('AccountStore', () => {
158158

159159
it('should create a new Account', async () => {
160160
expect(store.accounts.size).toEqual(0);
161-
await store.createAccount(3000000, 4032);
161+
await store.createAccount(Big(3000000), 4032);
162162
expect(store.accounts.size).toEqual(1);
163163
expect(store.activeAccount).toBeDefined();
164164
});
@@ -168,7 +168,7 @@ describe('AccountStore', () => {
168168
throw new Error('test-err');
169169
});
170170
expect(rootStore.appView.alerts.size).toBe(0);
171-
await store.createAccount(3000000, 4032);
171+
await store.createAccount(Big(3000000), 4032);
172172
await waitFor(() => {
173173
expect(rootStore.appView.alerts.size).toBe(1);
174174
expect(values(rootStore.appView.alerts)[0].message).toBe('test-err');
@@ -216,7 +216,9 @@ describe('AccountStore', () => {
216216
it('should deposit funds into an account', async () => {
217217
await store.fetchAccounts();
218218
const txid = await store.deposit(1);
219-
expect(+store.activeAccount.totalBalance).toBe(poolDepositAccount.account?.value);
219+
expect(store.activeAccount.totalBalance.toString()).toBe(
220+
poolDepositAccount.account?.value,
221+
);
220222
expect(txid).toEqual(poolDepositAccount.depositTxid);
221223
});
222224

@@ -236,7 +238,9 @@ describe('AccountStore', () => {
236238
it('should withdraw funds from an account', async () => {
237239
await store.fetchAccounts();
238240
const txid = await store.withdraw(1);
239-
expect(+store.activeAccount.totalBalance).toBe(poolWithdrawAccount.account?.value);
241+
expect(store.activeAccount.totalBalance.toString()).toBe(
242+
poolWithdrawAccount.account?.value,
243+
);
240244
expect(txid).toEqual(poolWithdrawAccount.withdrawTxid);
241245
});
242246

app/src/__tests__/store/buildSwapView.spec.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ describe('BuildSwapView', () => {
111111
});
112112

113113
it('should ensure amount is greater than the min terms', async () => {
114-
store.setAmount(Big(loopInTerms.minSwapAmount - 100));
114+
store.setAmount(Big(loopInTerms.minSwapAmount).sub(100));
115115
await store.getTerms();
116-
expect(+store.amountForSelected).toBe(loopInTerms.minSwapAmount);
116+
expect(store.amountForSelected.toString()).toBe(loopInTerms.minSwapAmount);
117117
});
118118

119119
it('should ensure amount is less than the max terms', async () => {
120120
store.setAmount(Big(loopInTerms.maxSwapAmount + 100));
121121
await store.getTerms();
122-
expect(+store.amountForSelected).toBe(loopInTerms.maxSwapAmount);
122+
expect(store.amountForSelected.toString()).toBe(loopInTerms.maxSwapAmount);
123123
});
124124

125125
it('should validate the conf target', async () => {
@@ -293,7 +293,7 @@ describe('BuildSwapView', () => {
293293
store.setDirection(SwapDirection.OUT);
294294
store.setAmount(Big(600));
295295

296-
let deadline = 0;
296+
let deadline = '';
297297
// mock the grpc unary function in order to capture the supplied deadline
298298
// passed in with the API request
299299
injectIntoGrpcUnary((desc, props) => {
@@ -303,7 +303,7 @@ describe('BuildSwapView', () => {
303303
// run a loop on mainnet and verify the deadline
304304
rootStore.nodeStore.network = 'mainnet';
305305
store.requestSwap();
306-
await waitFor(() => expect(deadline).toBeGreaterThan(0));
306+
await waitFor(() => expect(+deadline).toBeGreaterThan(0));
307307

308308
// inject again for the next swap
309309
injectIntoGrpcUnary((desc, props) => {
@@ -313,7 +313,7 @@ describe('BuildSwapView', () => {
313313
// run a loop on regtest and verify the deadline
314314
rootStore.nodeStore.network = 'regtest';
315315
store.requestSwap();
316-
await waitFor(() => expect(deadline).toEqual(0));
316+
await waitFor(() => expect(+deadline).toEqual(0));
317317
});
318318

319319
it('should handle errors when performing a loop', async () => {
@@ -370,7 +370,12 @@ describe('BuildSwapView', () => {
370370
describe('min/max swap limits', () => {
371371
const addChannel = (capacity: number, localBalance: number) => {
372372
const remoteBalance = capacity - localBalance;
373-
const lndChan = { ...lndChannel, capacity, localBalance, remoteBalance };
373+
const lndChan = {
374+
...lndChannel,
375+
capacity: `${capacity}`,
376+
localBalance: `${localBalance}`,
377+
remoteBalance: `${remoteBalance}`,
378+
};
374379
const channel = Channel.create(rootStore, lndChan);
375380
channel.chanId = `${channel.chanId}${rootStore.channelStore.channels.size}`;
376381
channel.remotePubkey = `${channel.remotePubkey}${rootStore.channelStore.channels.size}`;

app/src/__tests__/store/channelStore.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ describe('ChannelStore', () => {
134134
it('should compute inbound liquidity', async () => {
135135
await store.fetchChannels();
136136
const inbound = lndListChannels.channelsList.reduce(
137-
(sum, chan) => sum + chan.remoteBalance,
138-
0,
137+
(sum, chan) => sum.plus(chan.remoteBalance),
138+
Big(0),
139139
);
140140

141-
expect(+store.totalInbound).toBe(inbound);
141+
expect(+store.totalInbound).toBe(+inbound);
142142
});
143143

144144
it('should compute outbound liquidity', async () => {
145145
await store.fetchChannels();
146146
const outbound = lndListChannels.channelsList.reduce(
147-
(sum, chan) => sum + chan.localBalance,
148-
0,
147+
(sum, chan) => sum.plus(chan.localBalance),
148+
Big(0),
149149
);
150150

151-
expect(+store.totalOutbound).toBe(outbound);
151+
expect(+store.totalOutbound).toBe(+outbound);
152152
});
153153

154154
it('should fetch aliases for channels', async () => {
@@ -189,7 +189,9 @@ describe('ChannelStore', () => {
189189
expect(channel.remoteFeeRate).toBe(0);
190190
// the alias is fetched from the API and should be updated after a few ticks
191191
await waitFor(() => {
192-
expect(channel.remoteFeeRate).toBe(lndGetChanInfo.node1Policy.feeRateMilliMsat);
192+
expect(channel.remoteFeeRate.toString()).toBe(
193+
lndGetChanInfo.node1Policy.feeRateMilliMsat,
194+
);
193195
});
194196
});
195197

app/src/__tests__/store/nodeStore.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe('NodeStore', () => {
4949
expect(+store.wallet.channelBalance).toBe(0);
5050
expect(+store.wallet.walletBalance).toBe(0);
5151
await store.fetchBalances();
52-
expect(+store.wallet.channelBalance).toEqual(lndChannelBalance.balance);
53-
expect(+store.wallet.walletBalance).toEqual(lndWalletBalance.totalBalance);
52+
expect(store.wallet.channelBalance.toString()).toEqual(lndChannelBalance.balance);
53+
expect(store.wallet.walletBalance.toString()).toEqual(lndWalletBalance.totalBalance);
5454
});
5555

5656
it('should handle errors fetching balances', async () => {
@@ -69,14 +69,14 @@ describe('NodeStore', () => {
6969
it('should handle a transaction event', () => {
7070
expect(+store.wallet.walletBalance).toBe(0);
7171
store.onTransaction(lndTransaction);
72-
expect(+store.wallet.walletBalance).toBe(lndTransaction.amount);
72+
expect(store.wallet.walletBalance.toString()).toBe(lndTransaction.amount);
7373
});
7474

7575
it('should handle duplicate transaction events', () => {
7676
expect(+store.wallet.walletBalance).toBe(0);
7777
store.onTransaction(lndTransaction);
78-
expect(+store.wallet.walletBalance).toBe(lndTransaction.amount);
78+
expect(store.wallet.walletBalance.toString()).toBe(lndTransaction.amount);
7979
store.onTransaction(lndTransaction);
80-
expect(+store.wallet.walletBalance).toBe(lndTransaction.amount);
80+
expect(store.wallet.walletBalance.toString()).toBe(lndTransaction.amount);
8181
});
8282
});

app/src/__tests__/store/orderStore.spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,27 @@ describe('OrderStore', () => {
7575

7676
it('should submit an ask order', async () => {
7777
await rootStore.accountStore.fetchAccounts();
78-
const nonce = await store.submitOrder(OrderType.Ask, 100000, 2000, 2016, 100000, 253);
78+
const nonce = await store.submitOrder(
79+
OrderType.Ask,
80+
Big(100000),
81+
2000,
82+
2016,
83+
100000,
84+
253,
85+
);
7986
expect(nonce).toBe(hex(poolSubmitOrder.acceptedOrderNonce));
8087
});
8188

8289
it('should submit a bid order', async () => {
8390
await rootStore.accountStore.fetchAccounts();
84-
const nonce = await store.submitOrder(OrderType.Bid, 100000, 2000, 2016, 100000, 253);
91+
const nonce = await store.submitOrder(
92+
OrderType.Bid,
93+
Big(100000),
94+
2000,
95+
2016,
96+
100000,
97+
253,
98+
);
8599
expect(nonce).toBe(hex(poolSubmitOrder.acceptedOrderNonce));
86100
});
87101

@@ -100,14 +114,14 @@ describe('OrderStore', () => {
100114
}
101115
return undefined as any;
102116
});
103-
await store.submitOrder(OrderType.Bid, 100000, 2000, 2016, 100000, 253);
117+
await store.submitOrder(OrderType.Bid, Big(100000), 2000, 2016, 100000, 253);
104118
expect(rootStore.appView.alerts.size).toBe(1);
105119
expect(values(rootStore.appView.alerts)[0].message).toBe(poolInvalidOrder.failString);
106120
});
107121

108122
it('should throw if the fixed rate rate is too low', async () => {
109123
await rootStore.accountStore.fetchAccounts();
110-
await store.submitOrder(OrderType.Bid, 100000, 0.9, 20000, 100000, 253);
124+
await store.submitOrder(OrderType.Bid, Big(100000), 0.9, 20000, 100000, 253);
111125
expect(rootStore.appView.alerts.size).toBe(1);
112126
expect(values(rootStore.appView.alerts)[0].message).toMatch(/The rate is too low.*/);
113127
});

0 commit comments

Comments
 (0)