vega-frontend-monorepo/libs/wallet/src/use-vega-transaction.spec.tsx
Matthew Russell a5f9ed90e8
Fix/Order dialog state (#850)
* feat: remove dialog state handling from dialog and split out edit dialog

* feat: add complete state to use-vega-transaction, fix cancel dialog

* feat: add custom dialog content for order submission

* feat: handle custom title and custom intent

* feat: separate components, make order dialog wrapper more generic

* feat: remove dialog wrapper and add icon to dialog

* chore: remove other dialog wrappers and use icon and title props on main dialog

* chore: adjust default color of dialog text

* fix: tests for tx dialog and vega tx hook

* fix: order edit and cancel hook tests

* chore: add edit dialog to stories

* fix: e2e test for deal ticket

* feat: return dialog from hook

* refactor: add use-order-event hook to dedupe bus event logic

* refactor: add custom title and intent to order submit dialog

* chore: remove console logs

* fix: type error due to component being named idalog

* chore: add helper function for converting nanoseconds

* chore: remove capitalization text transform to dialog titles

* chore: remove unused import

* feat: handle titles and intents for cancel and edit

* chore: remove unused var
2022-07-26 14:35:30 +01:00

97 lines
3.3 KiB
TypeScript

import { act, renderHook } from '@testing-library/react-hooks';
import type { VegaWalletContextShape } from './context';
import { VegaWalletContext } from './context';
import type { ReactNode } from 'react';
import { useVegaTransaction, VegaTxStatus } from './use-vega-transaction';
import type { OrderSubmissionBody } from '@vegaprotocol/vegawallet-service-api-client';
const defaultWalletContext = {
keypair: null,
keypairs: [],
sendTx: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
selectPublicKey: jest.fn(),
connector: null,
};
function setup(context?: Partial<VegaWalletContextShape>) {
const wrapper = ({ children }: { children: ReactNode }) => (
<VegaWalletContext.Provider value={{ ...defaultWalletContext, ...context }}>
{children}
</VegaWalletContext.Provider>
);
return renderHook(() => useVegaTransaction(), { wrapper });
}
it('Has the correct default state', () => {
const { result } = setup();
expect(result.current.transaction.status).toEqual(VegaTxStatus.Default);
expect(result.current.transaction.txHash).toEqual(null);
expect(result.current.transaction.signature).toEqual(null);
expect(result.current.transaction.error).toEqual(null);
expect(typeof result.current.reset).toEqual('function');
expect(typeof result.current.send).toEqual('function');
});
it('If provider returns null status should be default', async () => {
const mockSendTx = jest.fn().mockReturnValue(Promise.resolve(null));
const { result } = setup({ sendTx: mockSendTx });
await act(async () => {
result.current.send({} as OrderSubmissionBody);
});
expect(result.current.transaction.status).toEqual(VegaTxStatus.Default);
});
it('Handles a single error', async () => {
const errorMessage = 'Oops error!';
const mockSendTx = jest
.fn()
.mockReturnValue(Promise.resolve({ error: errorMessage }));
const { result } = setup({ sendTx: mockSendTx });
await act(async () => {
result.current.send({} as OrderSubmissionBody);
});
expect(result.current.transaction.status).toEqual(VegaTxStatus.Error);
expect(result.current.transaction.error).toEqual({ error: errorMessage });
});
it('Handles multiple errors', async () => {
const errorObj = {
errors: {
something: 'Went wrong!',
},
};
const mockSendTx = jest.fn().mockReturnValue(Promise.resolve(errorObj));
const { result } = setup({ sendTx: mockSendTx });
await act(async () => {
result.current.send({} as OrderSubmissionBody);
});
expect(result.current.transaction.status).toEqual(VegaTxStatus.Error);
expect(result.current.transaction.error).toEqual(errorObj);
});
it('Returns the signature if successful', async () => {
const successObj = {
tx: {
inputData: 'input-data',
signature: {
algo: 'algo',
version: 1,
value: 'signature',
},
},
txHash: '0x123',
};
const mockSendTx = jest.fn().mockReturnValue(Promise.resolve(successObj));
const { result } = setup({ sendTx: mockSendTx });
await act(async () => {
result.current.send({} as OrderSubmissionBody);
});
expect(result.current.transaction.status).toEqual(VegaTxStatus.Pending);
expect(result.current.transaction.txHash).toEqual(successObj.txHash);
expect(result.current.transaction.signature).toEqual(
successObj.tx.signature.value
);
});