Skip to content

Commit 34a90c7

Browse files
committed
add test + cleanup
1 parent c34e0b3 commit 34a90c7

File tree

2 files changed

+85
-65
lines changed

2 files changed

+85
-65
lines changed

src/SmartTransactionsController.test.ts

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ jest.mock('@ethersproject/providers', () => ({
4040

4141
const addressFrom = '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3';
4242

43-
const createUnsignedTransaction = () => {
43+
const createUnsignedTransaction = (chainId: number) => {
4444
return {
4545
from: addressFrom,
4646
to: '0x0000000000000000000000000000000000000000',
4747
value: 0,
4848
data: '0x',
4949
nonce: 0,
5050
type: 2,
51-
chainId: 4,
51+
chainId,
5252
};
5353
};
5454

@@ -262,6 +262,34 @@ const ethereumChainIdDec = parseInt(CHAIN_IDS.ETHEREUM, 16);
262262
const trackMetaMetricsEventSpy = jest.fn();
263263
const getNetworkClientByIdSpy = jest.fn();
264264

265+
const defaultState = {
266+
smartTransactionsState: {
267+
smartTransactions: {
268+
[CHAIN_IDS.ETHEREUM]: [],
269+
},
270+
userOptIn: undefined,
271+
fees: {
272+
approvalTxFees: undefined,
273+
tradeTxFees: undefined,
274+
},
275+
feesByChainId: {
276+
[CHAIN_IDS.ETHEREUM]: {
277+
approvalTxFees: undefined,
278+
tradeTxFees: undefined,
279+
},
280+
[CHAIN_IDS.GOERLI]: {
281+
approvalTxFees: undefined,
282+
tradeTxFees: undefined,
283+
},
284+
},
285+
liveness: true,
286+
livenessByChainId: {
287+
[CHAIN_IDS.ETHEREUM]: true,
288+
[CHAIN_IDS.GOERLI]: true,
289+
},
290+
},
291+
};
292+
265293
describe('SmartTransactionsController', () => {
266294
let smartTransactionsController: SmartTransactionsController;
267295
let networkListener: (networkState: NetworkState) => void;
@@ -301,33 +329,7 @@ describe('SmartTransactionsController', () => {
301329
});
302330

303331
it('initializes with default state', () => {
304-
expect(smartTransactionsController.state).toStrictEqual({
305-
smartTransactionsState: {
306-
smartTransactions: {
307-
[CHAIN_IDS.ETHEREUM]: [],
308-
},
309-
userOptIn: undefined,
310-
fees: {
311-
approvalTxFees: undefined,
312-
tradeTxFees: undefined,
313-
},
314-
feesByChainId: {
315-
[CHAIN_IDS.ETHEREUM]: {
316-
approvalTxFees: undefined,
317-
tradeTxFees: undefined,
318-
},
319-
[CHAIN_IDS.GOERLI]: {
320-
approvalTxFees: undefined,
321-
tradeTxFees: undefined,
322-
},
323-
},
324-
liveness: true,
325-
livenessByChainId: {
326-
[CHAIN_IDS.ETHEREUM]: true,
327-
[CHAIN_IDS.GOERLI]: true,
328-
},
329-
},
330-
});
332+
expect(smartTransactionsController.state).toStrictEqual(defaultState);
331333
});
332334

333335
describe('onNetworkChange', () => {
@@ -471,8 +473,8 @@ describe('SmartTransactionsController', () => {
471473

472474
describe('getFees', () => {
473475
it('gets unsigned transactions and estimates based on an unsigned transaction', async () => {
474-
const tradeTx = createUnsignedTransaction();
475-
const approvalTx = createUnsignedTransaction();
476+
const tradeTx = createUnsignedTransaction(ethereumChainIdDec);
477+
const approvalTx = createUnsignedTransaction(ethereumChainIdDec);
476478
const getFeesApiResponse = createGetFeesApiResponse();
477479
nock(API_BASE_URL)
478480
.post(`/networks/${ethereumChainIdDec}/getFees`)
@@ -486,6 +488,54 @@ describe('SmartTransactionsController', () => {
486488
tradeTxFees: getFeesApiResponse.txs[1],
487489
});
488490
});
491+
492+
it('should add fee data to feesByChainId state using the networkClientId passed in to identify the appropriate chain', async () => {
493+
const goerliChainIdDec = parseInt(CHAIN_IDS.GOERLI, 16);
494+
getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
495+
switch (networkClientId) {
496+
case 'mainnet':
497+
return {
498+
configuration: {
499+
chainId: CHAIN_IDS.ETHEREUM,
500+
},
501+
};
502+
case 'goerli':
503+
return {
504+
configuration: {
505+
chainId: CHAIN_IDS.GOERLI,
506+
},
507+
};
508+
default:
509+
throw new Error('Invalid network client id');
510+
}
511+
});
512+
513+
const tradeTx = createUnsignedTransaction(goerliChainIdDec);
514+
const approvalTx = createUnsignedTransaction(goerliChainIdDec);
515+
const getFeesApiResponse = createGetFeesApiResponse();
516+
nock(API_BASE_URL)
517+
.post(`/networks/${goerliChainIdDec}/getFees`)
518+
.reply(200, getFeesApiResponse);
519+
520+
expect(
521+
smartTransactionsController.state.smartTransactionsState.feesByChainId,
522+
).toStrictEqual(defaultState.smartTransactionsState.feesByChainId);
523+
524+
await smartTransactionsController.getFees(tradeTx, approvalTx, 'goerli');
525+
526+
expect(
527+
smartTransactionsController.state.smartTransactionsState.feesByChainId,
528+
).toMatchObject({
529+
[CHAIN_IDS.ETHEREUM]: {
530+
approvalTxFees: undefined,
531+
tradeTxFees: undefined,
532+
},
533+
[CHAIN_IDS.GOERLI]: {
534+
approvalTxFees: getFeesApiResponse.txs[0],
535+
tradeTxFees: getFeesApiResponse.txs[1],
536+
},
537+
});
538+
});
489539
});
490540

491541
describe('submitSignedTransactions', () => {
@@ -823,36 +873,10 @@ describe('SmartTransactionsController', () => {
823873
.spyOn(smartTransactionsController, 'checkPoll')
824874
.mockImplementation(() => undefined);
825875

826-
const defaultState = {
827-
smartTransactions: {
828-
[CHAIN_IDS.ETHEREUM]: [],
829-
},
830-
userOptIn: undefined,
831-
fees: {
832-
approvalTxFees: undefined,
833-
tradeTxFees: undefined,
834-
},
835-
feesByChainId: {
836-
[CHAIN_IDS.ETHEREUM]: {
837-
approvalTxFees: undefined,
838-
tradeTxFees: undefined,
839-
},
840-
[CHAIN_IDS.GOERLI]: {
841-
approvalTxFees: undefined,
842-
tradeTxFees: undefined,
843-
},
844-
},
845-
liveness: true,
846-
livenessByChainId: {
847-
[CHAIN_IDS.ETHEREUM]: true,
848-
[CHAIN_IDS.GOERLI]: true,
849-
},
850-
};
851-
852876
// pending transactions in state are required to test polling
853877
smartTransactionsController.update({
854878
smartTransactionsState: {
855-
...defaultState,
879+
...defaultState.smartTransactionsState,
856880
smartTransactions: {
857881
'0x1': [
858882
{
@@ -964,11 +988,7 @@ describe('SmartTransactionsController', () => {
964988
);
965989

966990
// cleanup
967-
smartTransactionsController.update({
968-
smartTransactionsState: {
969-
...defaultState,
970-
},
971-
});
991+
smartTransactionsController.update(defaultState);
972992

973993
smartTransactionsController.stopAllPolling();
974994
jest.clearAllTimers();

src/SmartTransactionsController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ export default class SmartTransactionsController extends PollingControllerV1<
322322
cancelledNonceIndex > -1
323323
? currentSmartTransactions
324324
.slice(0, cancelledNonceIndex)
325-
?.concat(currentSmartTransactions.slice(cancelledNonceIndex + 1))
326-
?.concat(historifiedSmartTransaction)
325+
.concat(currentSmartTransactions.slice(cancelledNonceIndex + 1))
326+
.concat(historifiedSmartTransaction)
327327
: currentSmartTransactions?.concat(historifiedSmartTransaction);
328328
this.update({
329329
smartTransactionsState: {

0 commit comments

Comments
 (0)