vega-frontend-monorepo/libs/wallet/src/use-vega-transaction-manager.spec.tsx
Art 87e1f9998e
feat: transaction store and toasts (#2382)
* feat: add eth and vega transaction stores

feat: replace useStoredEthereumTransaction with useEthTransactionManager

feat: add event bus subsciption to vega transaction store

feat: handle order cancellation

feat: rename Deposit, Order and Withdraw status field to be unique

Revert "feat: rename Deposit, Order and Withdraw status field to be unique"

This reverts commit f0b314d53fb3ada6fbebaba4fd1e5af6f38beaed.

feat: split transaction update subscription

feat: handle order and deposit transaction

feat: handle withdrawal creation through transaction store

feat: handle withdraw approval

feat: handle panding withdrawls, add createdAt

feat: handle transaction toast/dialog dismissal

feat: add use vega transaction store tests

feat: add use vega transaction store tests

feat: add use vega transaction menager tests

feat: add use vega transaction menager tests

feat: add use vega transaction updater tests

feat: improve use vega transaction updater tests

feat: add use eth transaction store

feat: add use eth withdraw approvals store

feat: add use eth transaction updater tests

fixed tests

* feat: toasts

feat: toasts

feat: toasts

* feat: add use eth withdraw approval manager tests

* feat: add use eth transaction manager tests

* feat: add use eth transaction manager tests

* feat: add useEthWithdrawApprovalsManager tests

* feat: remove Web3Container react container from CreateWithdrawalDialog

* feat: remove Web3Container react container around TransactionsHandler

* feat: remove unnecessary async from PendingWithdrawalsTable

* feat: remove comments from WithdrawalFeedback

* fixed z-index issue

* cypress

Co-authored-by: Bartłomiej Głownia <bglownia@gmail.com>
2022-12-21 10:29:32 +01:00

111 lines
3.1 KiB
TypeScript

import { useVegaTransactionManager } from './use-vega-transaction-manager';
import { renderHook } from '@testing-library/react';
import waitForNextTick from 'flush-promises';
import type { TransactionResponse } from './connectors/vega-connector';
import type {
VegaTransactionStore,
VegaStoredTxState,
} from './use-vega-transaction-store';
import { VegaTxStatus } from './use-vega-transaction';
const mockSendTx = jest.fn<Promise<Partial<TransactionResponse> | null>, []>();
const pubKey = 'pubKey';
jest.mock('./use-vega-wallet', () => ({
useVegaWallet: () => ({
sendTx: mockSendTx,
pubKey,
}),
}));
const transactionHash = 'txHash';
const signature = 'signature';
const receivedAt = 'receivedAt';
const sentAt = 'sentAt';
const transactionResponse: TransactionResponse = {
transactionHash,
signature,
receivedAt,
sentAt,
};
const pendingTransactionUpdate = {
status: VegaTxStatus.Pending,
txHash: transactionHash,
signature,
};
const update = jest.fn();
const del = jest.fn();
const defaultState: Partial<VegaTransactionStore> = {
transactions: [
{
id: 0,
status: VegaTxStatus.Requested,
} as VegaStoredTxState,
{
id: 1,
status: VegaTxStatus.Requested,
} as VegaStoredTxState,
],
update,
delete: del,
};
const mockTransactionStoreState = jest.fn<Partial<VegaTransactionStore>, []>();
jest.mock('./use-vega-transaction-store', () => ({
useVegaTransactionStore: (
selector: (state: Partial<VegaTransactionStore>) => void
) => selector(mockTransactionStoreState()),
}));
describe('useVegaTransactionManager', () => {
beforeEach(() => {
update.mockReset();
del.mockReset();
mockSendTx.mockReset();
mockTransactionStoreState.mockReset();
});
it('sendTx of first pending transaction', async () => {
mockTransactionStoreState.mockReturnValue(defaultState);
mockSendTx.mockResolvedValue(transactionResponse);
let result = renderHook(useVegaTransactionManager);
result.rerender();
expect(update).not.toBeCalled();
await waitForNextTick();
expect(update.mock.calls[0]).toEqual([0, pendingTransactionUpdate]);
expect(update.mock.calls[1]).toEqual([1, pendingTransactionUpdate]);
update.mockReset();
result = renderHook(useVegaTransactionManager);
await waitForNextTick();
expect(update).toBeCalled();
expect(update.mock.calls[0]).toEqual([0, pendingTransactionUpdate]);
result.rerender();
await waitForNextTick();
expect(update.mock.calls[1]).toEqual([1, pendingTransactionUpdate]);
});
it('del transaction on null response', async () => {
mockTransactionStoreState.mockReturnValue(defaultState);
mockSendTx.mockResolvedValue(null);
renderHook(useVegaTransactionManager);
await waitForNextTick();
expect(update).not.toBeCalled();
expect(del).toBeCalled();
});
it('sets error on reject', async () => {
mockTransactionStoreState.mockReturnValue(defaultState);
mockSendTx.mockRejectedValue(null);
renderHook(useVegaTransactionManager);
await waitForNextTick();
expect(update).toBeCalled();
expect(update.mock.calls[0][1]?.status).toEqual(VegaTxStatus.Error);
});
});