Skip to content

Commit 93ebaac

Browse files
add tests
1 parent 4ac55db commit 93ebaac

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

packages/transaction-controller/src/TransactionController.test.ts

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7970,6 +7970,217 @@ describe('TransactionController', () => {
79707970
});
79717971
});
79727972

7973+
describe('emulateNewTransaction', () => {
7974+
it('publishes swap event when transaction is a swap', () => {
7975+
const swapTransactionMeta = {
7976+
id: 'tx1',
7977+
chainId: CHAIN_ID_MOCK,
7978+
networkClientId: NETWORK_CLIENT_ID_MOCK,
7979+
status: TransactionStatus.approved,
7980+
type: TransactionType.swap,
7981+
txParams: {
7982+
from: ACCOUNT_MOCK,
7983+
to: ACCOUNT_2_MOCK,
7984+
},
7985+
} as TransactionMeta;
7986+
7987+
const { controller, messenger } = setupController({
7988+
options: {
7989+
state: {
7990+
transactions: [swapTransactionMeta],
7991+
},
7992+
},
7993+
});
7994+
7995+
expect(controller.state.transactions).toHaveLength(1);
7996+
expect(controller.state.transactions[0]).toMatchObject({
7997+
id: swapTransactionMeta.id,
7998+
type: TransactionType.swap,
7999+
});
8000+
8001+
const swapListener = jest.fn();
8002+
messenger.subscribe(
8003+
'TransactionController:transactionNewSwap',
8004+
swapListener,
8005+
);
8006+
8007+
controller.emulateNewTransaction(swapTransactionMeta.id);
8008+
8009+
expect(swapListener).toHaveBeenCalledTimes(1);
8010+
expect(swapListener).toHaveBeenCalledWith({
8011+
transactionMeta: expect.objectContaining({
8012+
id: swapTransactionMeta.id,
8013+
type: TransactionType.swap,
8014+
}),
8015+
});
8016+
});
8017+
8018+
it('publishes swap approval event when transaction is a swap approval', () => {
8019+
const swapApprovalTransactionMeta = {
8020+
id: 'tx2',
8021+
chainId: CHAIN_ID_MOCK,
8022+
networkClientId: NETWORK_CLIENT_ID_MOCK,
8023+
status: TransactionStatus.approved,
8024+
type: TransactionType.swapApproval,
8025+
txParams: {
8026+
from: ACCOUNT_MOCK,
8027+
to: ACCOUNT_2_MOCK,
8028+
},
8029+
} as TransactionMeta;
8030+
8031+
const { controller, messenger } = setupController({
8032+
options: {
8033+
state: {
8034+
transactions: [swapApprovalTransactionMeta],
8035+
},
8036+
},
8037+
});
8038+
8039+
expect(controller.state.transactions).toHaveLength(1);
8040+
expect(controller.state.transactions[0]).toMatchObject({
8041+
id: swapApprovalTransactionMeta.id,
8042+
type: TransactionType.swapApproval,
8043+
});
8044+
8045+
const swapApprovalListener = jest.fn();
8046+
messenger.subscribe(
8047+
'TransactionController:transactionNewSwapApproval',
8048+
swapApprovalListener,
8049+
);
8050+
8051+
controller.emulateNewTransaction(swapApprovalTransactionMeta.id);
8052+
8053+
expect(swapApprovalListener).toHaveBeenCalledTimes(1);
8054+
expect(swapApprovalListener).toHaveBeenCalledWith({
8055+
transactionMeta: expect.objectContaining({
8056+
id: swapApprovalTransactionMeta.id,
8057+
type: TransactionType.swapApproval,
8058+
}),
8059+
});
8060+
});
8061+
8062+
it('does not publish events when transaction does not exist', () => {
8063+
const { controller, messenger } = setupController();
8064+
8065+
const swapListener = jest.fn();
8066+
const swapApprovalListener = jest.fn();
8067+
messenger.subscribe(
8068+
'TransactionController:transactionNewSwap',
8069+
swapListener,
8070+
);
8071+
messenger.subscribe(
8072+
'TransactionController:transactionNewSwapApproval',
8073+
swapApprovalListener,
8074+
);
8075+
8076+
controller.emulateNewTransaction('missing-transaction-id');
8077+
8078+
expect(swapListener).not.toHaveBeenCalled();
8079+
expect(swapApprovalListener).not.toHaveBeenCalled();
8080+
});
8081+
});
8082+
8083+
describe('emulateTransactionUpdate', () => {
8084+
it('adds transaction to state and publishes update when it does not exist', () => {
8085+
const selectedAccount = {
8086+
...INTERNAL_ACCOUNT_MOCK,
8087+
address: ACCOUNT_2_MOCK,
8088+
};
8089+
const { controller, messenger, mockGetSelectedAccount } =
8090+
setupController({ selectedAccount });
8091+
8092+
const updateTransactionSpy = jest
8093+
.spyOn(controller, 'updateTransaction')
8094+
.mockImplementation(() => undefined);
8095+
8096+
const transactionMeta = {
8097+
id: 'tx3',
8098+
chainId: CHAIN_ID_MOCK,
8099+
networkClientId: NETWORK_CLIENT_ID_MOCK,
8100+
status: TransactionStatus.unapproved,
8101+
txParams: {
8102+
from: ACCOUNT_MOCK,
8103+
to: ACCOUNT_2_MOCK,
8104+
},
8105+
} as TransactionMeta;
8106+
8107+
const statusUpdatedListener = jest.fn();
8108+
messenger.subscribe(
8109+
'TransactionController:transactionStatusUpdated',
8110+
statusUpdatedListener,
8111+
);
8112+
8113+
controller.emulateTransactionUpdate(transactionMeta);
8114+
8115+
expect(mockGetSelectedAccount).toHaveBeenCalledTimes(1);
8116+
expect(updateTransactionSpy).toHaveBeenCalledTimes(1);
8117+
const updatedTransactionMeta = updateTransactionSpy.mock.calls[0][0] as TransactionMeta;
8118+
8119+
expect(updatedTransactionMeta.txParams.from).toBe(selectedAccount.address);
8120+
expect(controller.state.transactions).toHaveLength(1);
8121+
expect(controller.state.transactions[0]).toStrictEqual(updatedTransactionMeta);
8122+
expect(updateTransactionSpy).toHaveBeenCalledWith(
8123+
updatedTransactionMeta,
8124+
'Generated from user operation',
8125+
);
8126+
expect(statusUpdatedListener).toHaveBeenCalledTimes(1);
8127+
expect(statusUpdatedListener).toHaveBeenCalledWith({
8128+
transactionMeta: updatedTransactionMeta,
8129+
});
8130+
8131+
updateTransactionSpy.mockRestore();
8132+
});
8133+
8134+
it('does not add duplicate transaction when it already exists', () => {
8135+
const selectedAccount = {
8136+
...INTERNAL_ACCOUNT_MOCK,
8137+
address: ACCOUNT_2_MOCK,
8138+
};
8139+
const existingTransactionMeta = {
8140+
id: 'tx4',
8141+
chainId: CHAIN_ID_MOCK,
8142+
networkClientId: NETWORK_CLIENT_ID_MOCK,
8143+
status: TransactionStatus.approved,
8144+
txParams: {
8145+
from: ACCOUNT_MOCK,
8146+
to: ACCOUNT_2_MOCK,
8147+
},
8148+
} as TransactionMeta;
8149+
8150+
const { controller, messenger } = setupController({
8151+
options: {
8152+
state: {
8153+
transactions: [existingTransactionMeta],
8154+
},
8155+
},
8156+
selectedAccount,
8157+
});
8158+
8159+
const updateTransactionSpy = jest
8160+
.spyOn(controller, 'updateTransaction')
8161+
.mockImplementation(() => undefined);
8162+
8163+
const statusUpdatedListener = jest.fn();
8164+
messenger.subscribe(
8165+
'TransactionController:transactionStatusUpdated',
8166+
statusUpdatedListener,
8167+
);
8168+
8169+
controller.emulateTransactionUpdate(existingTransactionMeta);
8170+
8171+
expect(controller.state.transactions).toHaveLength(1);
8172+
expect(updateTransactionSpy).toHaveBeenCalledTimes(1);
8173+
const updatedTransactionMeta = updateTransactionSpy.mock.calls[0][0] as TransactionMeta;
8174+
expect(updatedTransactionMeta.txParams.from).toBe(selectedAccount.address);
8175+
expect(statusUpdatedListener).toHaveBeenCalledTimes(1);
8176+
expect(statusUpdatedListener).toHaveBeenCalledWith({
8177+
transactionMeta: updatedTransactionMeta,
8178+
});
8179+
8180+
updateTransactionSpy.mockRestore();
8181+
});
8182+
});
8183+
79738184
describe('metadata', () => {
79748185
it('includes expected state in debug snapshots', () => {
79758186
const { controller } = setupController();

0 commit comments

Comments
 (0)