vega-frontend-monorepo/libs/wallet/src/use-vega-transaction.spec.tsx
m.ray 71ede25339
chore: upgrade to React 18 (#952)
* chore: upgrade react only

* chore: import renderHook from testing-library/react

* chore: add @babel/runtime to fix tests

* fix: fix some of the tests

* fix: fix some of the tests

* fix: fix tests failing on not being wrapped in act

* fix: fix tests in use-environment

* fix:  fix @types/react issue

* fix: fix formatting

* fix: remove unsued method

* fix: callout not accepting react node and root element null check

* fix: main.tsx stats null check

* fix: implicit any type fixes

* Update libs/environment/src/hooks/use-nodes.spec.tsx

* fix:  import act from testing-lib

* fix:  add strict mode back

* fix:  fix formatting issues

* fix: add babel deps for storybook

* Update tsconfig.json (#970)

* Update tsconfig.json

* feat: [console-lite] - add missing types in few places

Co-authored-by: maciek <maciek@vegaprotocol.io>

* chore(#952): remove any from useDataProvider hook

Co-authored-by: macqbat <kubat.maciek@gmail.com>
Co-authored-by: maciek <maciek@vegaprotocol.io>
Co-authored-by: Bartłomiej Głownia <bglownia@gmail.com>
2022-08-09 10:43:11 +01:00

95 lines
3.2 KiB
TypeScript

import { act, renderHook } from '@testing-library/react';
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 './wallet-types';
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(errorMessage);
});
it('Handles multiple errors', async () => {
const errorObj = {
error: '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.error);
});
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
);
});