2022-08-09 09:43:11 +00:00
|
|
|
import { act, renderHook } from '@testing-library/react';
|
2022-03-30 09:49:48 +00:00
|
|
|
import type { VegaWalletContextShape } from './context';
|
|
|
|
import { VegaWalletContext } from './context';
|
|
|
|
import type { ReactNode } from 'react';
|
2022-10-03 18:12:34 +00:00
|
|
|
import {
|
|
|
|
initialState,
|
|
|
|
useVegaTransaction,
|
|
|
|
VegaTxStatus,
|
|
|
|
} from './use-vega-transaction';
|
|
|
|
import type { Transaction } from './connectors';
|
|
|
|
import { WalletError } from './connectors';
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const mockPubKey = '0x123';
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
const defaultWalletContext = {
|
2022-10-03 18:12:34 +00:00
|
|
|
pubKey: null,
|
|
|
|
pubKeys: [],
|
2023-01-19 11:16:21 +00:00
|
|
|
isReadOnly: false,
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
sendTx: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
disconnect: jest.fn(),
|
2022-10-03 18:12:34 +00:00
|
|
|
selectPubKey: jest.fn(),
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
connector: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
function setup(context?: Partial<VegaWalletContextShape>) {
|
|
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
|
|
<VegaWalletContext.Provider value={{ ...defaultWalletContext, ...context }}>
|
|
|
|
{children}
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
);
|
|
|
|
return renderHook(() => useVegaTransaction(), { wrapper });
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
describe('useVegaTransaction', () => {
|
|
|
|
it('has the correct default state', () => {
|
|
|
|
const { result } = setup();
|
|
|
|
expect(result.current).toEqual({
|
|
|
|
transaction: initialState,
|
|
|
|
send: expect.any(Function),
|
|
|
|
reset: expect.any(Function),
|
|
|
|
setComplete: expect.any(Function),
|
|
|
|
setTransaction: expect.any(Function),
|
|
|
|
Dialog: expect.any(Function),
|
|
|
|
});
|
|
|
|
});
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
it('resets state if sendTx returns null (user rejects)', async () => {
|
|
|
|
const mockSendTx = jest.fn().mockReturnValue(Promise.resolve(null));
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
await act(async () => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toEqual(VegaTxStatus.Default);
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
});
|
|
|
|
|
2023-01-19 11:16:21 +00:00
|
|
|
it('handles a single wallet error', () => {
|
2022-10-03 18:12:34 +00:00
|
|
|
const error = new WalletError('test error', 1, 'test data');
|
|
|
|
const mockSendTx = jest.fn(() => {
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
act(() => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toEqual(VegaTxStatus.Error);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty(
|
|
|
|
'message',
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty('code', 1);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty('data', error.data);
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
});
|
|
|
|
|
2023-01-19 11:16:21 +00:00
|
|
|
it('handles a single error', () => {
|
|
|
|
const error = new Error('test error');
|
|
|
|
const mockSendTx = jest.fn(() => {
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
act(() => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toEqual(VegaTxStatus.Error);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty(
|
|
|
|
'message',
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
it('handles an unkwown error', () => {
|
|
|
|
const unknownThrow = { foo: 'bar' };
|
|
|
|
const mockSendTx = jest.fn(() => {
|
|
|
|
throw unknownThrow;
|
|
|
|
});
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
act(() => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toEqual(VegaTxStatus.Error);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty(
|
|
|
|
'message',
|
|
|
|
'Something went wrong'
|
|
|
|
);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty('code', 105);
|
|
|
|
expect(result.current.transaction.error).toHaveProperty(
|
|
|
|
'data',
|
|
|
|
'Unknown error occurred'
|
|
|
|
);
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
});
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
it('sets txHash and signature to state if successful', async () => {
|
|
|
|
const successObj = {
|
|
|
|
transactionHash: '0x123',
|
|
|
|
signature: 'signature',
|
|
|
|
};
|
|
|
|
const mockSendTx = jest.fn().mockReturnValue(Promise.resolve(successObj));
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
await act(async () => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toEqual(VegaTxStatus.Pending);
|
|
|
|
expect(result.current.transaction.txHash).toEqual(
|
|
|
|
successObj.transactionHash
|
|
|
|
);
|
|
|
|
expect(result.current.transaction.signature).toEqual(successObj.signature);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setComplete sets VegaTxStatus', () => {
|
|
|
|
const { result } = setup();
|
|
|
|
act(() => {
|
|
|
|
result.current.setComplete();
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toBe(VegaTxStatus.Complete);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reset resets transaction status', async () => {
|
|
|
|
const mockSendTx = jest.fn().mockReturnValue(
|
|
|
|
Promise.resolve({
|
|
|
|
transactionHash: '0x123',
|
|
|
|
signature: 'signature',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const { result } = setup({ sendTx: mockSendTx });
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
result.current.send(mockPubKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(result.current.transaction.status).toBe(VegaTxStatus.Pending);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
result.current.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.current.transaction).toEqual(initialState);
|
Feat/63 Deal ticket (#82)
* scaffold dealticket package, remove trading views from react-helpers
* add deal ticket component, add intent utils, expand dialog and form group styles
* add splash component, show market not found message if market doesnt exist
* tidy up error handling
* add handleError method for vega tx hook
* add better testname for provider test, flesh out tests a bit more for deal ticket
* Add unit tests for useVegaTransaction and useOrderSubmit hooks
* add wrapper component for order dialog styles
* add vega styled loader to ui toolkit and use in order dialog
* add title prop to order dialog
* split limit and market tickets into own files
* add button radio component
* revert dialog styles
* move splash component to ui-toolkit, add story
* convert intent to enum
* Make button always type=button unless type prop is passed
* inline filter logic for tif selector
* add date-fns, add datetime to helpers
* add order types to wallet package, make price undefined if order type is market
* use enums in deal ticket logic
* tidy up order state by moving submit and transaction hooks out of deal ticket
* add comment for dialog styles
* remove decimal from price input
* add types package, delete old generated types from trading project
* rename types package to graphql
* update generate command to point to correct locations
* fix use order submit test
* use intent shadow helper
* remove date-fns and format manually, update submit button error to use input-error
* remove stray console.log
2022-03-17 19:35:46 +00:00
|
|
|
});
|
|
|
|
});
|