fix: #656 fix use order validation tests

This commit is contained in:
madalinaraicu 2022-07-14 14:19:48 +03:00
parent f87735a45c
commit ca1fb12ceb
3 changed files with 154 additions and 106 deletions

View File

@ -90,58 +90,84 @@ function setup(
return renderHook(() => useOrderValidation({ ...defaultOrder, ...props })); return renderHook(() => useOrderValidation({ ...defaultOrder, ...props }));
} }
it('Returns empty string when given valid data', () => { describe(`useOrderValidation`, () => {
it('Returns empty string when given valid data', () => {
const { result } = setup(); const { result } = setup();
expect(result.current).toEqual(''); expect(result.current).toStrictEqual({ isDisabled: false, message: `` });
}); });
it('Returns an error message when no keypair found', async () => { it('Returns an error message when no keypair found', async () => {
const { result } = setup(defaultOrder, { keypair: null }); const { result } = setup(defaultOrder, { keypair: null });
expect(result.current).toEqual(''); expect(result.current).toStrictEqual({ isDisabled: false, message: `` });
}); });
it('Returns an error message when the keypair is tainted', async () => { it('Returns an error message when the keypair is tainted', async () => {
const { result } = setup(defaultOrder, { const { result } = setup(defaultOrder, {
keypair: { ...defaultWalletContext.keypair, tainted: true }, keypair: { ...defaultWalletContext.keypair, tainted: true },
}); });
expect(result.current).toEqual(''); expect(result.current).toStrictEqual({ isDisabled: false, message: `` });
}); });
it.each` it.each`
state | errorMessage state
${MarketState.Cancelled} | ${ERROR.MARKET_INACTIVE} ${MarketState.Settled}
${MarketState.Closed} | ${ERROR.MARKET_INACTIVE} ${MarketState.Rejected}
${MarketState.Rejected} | ${ERROR.MARKET_INACTIVE} ${MarketState.TradingTerminated}
${MarketState.Settled} | ${ERROR.MARKET_INACTIVE} `(
${MarketState.TradingTerminated} | ${ERROR.MARKET_INACTIVE} 'Returns an error message for market state when no longer accepting orders',
${MarketState.Suspended} | ${ERROR.MARKET_SUSPENDED} async ({ state }) => {
${MarketState.Pending} | ${ERROR.MARKET_WAITING}
${MarketState.Proposed} | ${ERROR.MARKET_WAITING}
`(
'Returns an error message for "$marketState" market',
async ({ state, errorMessage }) => {
const { result } = setup({ market: { ...defaultOrder.market, state } }); const { result } = setup({ market: { ...defaultOrder.market, state } });
expect(result.current).toEqual(errorMessage); expect(result.current).toStrictEqual({
isDisabled: true,
message: `This market is ${state.toLowerCase()} and no longer accepting orders`,
});
} }
); );
it.each` it.each`
state
${MarketState.Suspended}
${MarketState.Pending}
${MarketState.Cancelled}
${MarketState.Proposed}
${MarketState.Closed}
`(
'Returns an error message for market state suspended or pending',
async ({ state }) => {
const { result } = setup({
market: {
...defaultOrder.market,
state,
tradingMode: MarketTradingMode.BatchAuction,
},
});
expect(result.current).toStrictEqual({
isDisabled: false,
message: `This market is ${state.toLowerCase()} and only accepting liquidity orders`,
});
}
);
it.each`
tradingMode | errorMessage tradingMode | errorMessage
${MarketTradingMode.BatchAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT} ${MarketTradingMode.BatchAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT}
${MarketTradingMode.MonitoringAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT} ${MarketTradingMode.MonitoringAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT}
${MarketTradingMode.OpeningAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT} ${MarketTradingMode.OpeningAuction} | ${ERROR.MARKET_CONTINUOUS_LIMIT}
`( `(
'Returns an error message when trying to submit a non-limit order for a "$tradingMode" market', `Returns an error message when trying to submit a non-limit order for a "$tradingMode" market`,
async ({ tradingMode, errorMessage }) => { async ({ tradingMode, errorMessage }) => {
const { result } = setup({ const { result } = setup({
market: { ...defaultOrder.market, tradingMode }, market: { ...defaultOrder.market, tradingMode },
orderType: VegaWalletOrderType.Market, orderType: VegaWalletOrderType.Market,
}); });
expect(result.current).toEqual(errorMessage); expect(result.current).toStrictEqual({
isDisabled: true,
message: errorMessage,
});
} }
); );
it.each` it.each`
tradingMode | orderTimeInForce | errorMessage tradingMode | orderTimeInForce | errorMessage
${MarketTradingMode.BatchAuction} | ${VegaWalletOrderTimeInForce.FOK} | ${ERROR.MARKET_CONTINUOUS_TIF} ${MarketTradingMode.BatchAuction} | ${VegaWalletOrderTimeInForce.FOK} | ${ERROR.MARKET_CONTINUOUS_TIF}
${MarketTradingMode.MonitoringAuction} | ${VegaWalletOrderTimeInForce.FOK} | ${ERROR.MARKET_CONTINUOUS_TIF} ${MarketTradingMode.MonitoringAuction} | ${VegaWalletOrderTimeInForce.FOK} | ${ERROR.MARKET_CONTINUOUS_TIF}
@ -152,45 +178,58 @@ it.each`
${MarketTradingMode.BatchAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF} ${MarketTradingMode.BatchAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF}
${MarketTradingMode.MonitoringAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF} ${MarketTradingMode.MonitoringAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF}
${MarketTradingMode.OpeningAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF} ${MarketTradingMode.OpeningAuction} | ${VegaWalletOrderTimeInForce.GFN} | ${ERROR.MARKET_CONTINUOUS_TIF}
`( `(
'Returns an error message when submitting a limit order with a "$orderTimeInForce" value to a "$tradingMode" market', `Returns an error message when submitting a limit order with a "$orderTimeInForce" value to a "$tradingMode" market`,
async ({ tradingMode, orderTimeInForce, errorMessage }) => { async ({ tradingMode, orderTimeInForce, errorMessage }) => {
const { result } = setup({ const { result } = setup({
market: { ...defaultOrder.market, tradingMode }, market: { ...defaultOrder.market, tradingMode },
orderType: VegaWalletOrderType.Limit, orderType: VegaWalletOrderType.Limit,
orderTimeInForce, orderTimeInForce,
}); });
expect(result.current).toEqual(errorMessage); expect(result.current).toStrictEqual({
isDisabled: true,
message: errorMessage,
});
} }
); );
it.each` it.each`
fieldName | errorType | errorMessage fieldName | errorType | errorMessage
${'size'} | ${'required'} | ${ERROR.FIELD_SIZE_REQ} ${`size`} | ${`required`} | ${ERROR.FIELD_SIZE_REQ}
${'size'} | ${'min'} | ${ERROR.FIELD_SIZE_MIN} ${`size`} | ${`min`} | ${ERROR.FIELD_SIZE_MIN}
${'price'} | ${'required'} | ${ERROR.FIELD_PRICE_REQ} ${`price`} | ${`required`} | ${ERROR.FIELD_PRICE_REQ}
${'price'} | ${'min'} | ${ERROR.FIELD_PRICE_MIN} ${`price`} | ${`min`} | ${ERROR.FIELD_PRICE_MIN}
`( `(
'Returns an error message when the order $fieldName "$errorType" validation fails', `Returns an error message when the order $fieldName "$errorType" validation fails`,
async ({ fieldName, errorType, errorMessage }) => { async ({ fieldName, errorType, errorMessage }) => {
const { result } = setup({ const { result } = setup({
fieldErrors: { [fieldName]: { type: errorType } }, fieldErrors: { [fieldName]: { type: errorType } },
}); });
expect(result.current).toEqual(errorMessage); expect(result.current).toStrictEqual({
isDisabled: true,
message: errorMessage,
});
} }
); );
it('Returns an error message when the order size incorrectly has decimal values', async () => { it('Returns an error message when the order size incorrectly has decimal values', async () => {
const { result } = setup({ const { result } = setup({
market: { ...market, positionDecimalPlaces: 0 }, market: { ...market, positionDecimalPlaces: 0 },
fieldErrors: { size: { type: 'validate', message: ERROR_SIZE_DECIMAL } }, fieldErrors: { size: { type: `validate`, message: ERROR_SIZE_DECIMAL } },
});
expect(result.current).toStrictEqual({
isDisabled: true,
message: ERROR.FIELD_PRICE_STEP_NULL,
});
}); });
expect(result.current).toEqual(ERROR.FIELD_PRICE_STEP_NULL);
});
it('Returns an error message when the order size has more decimals then allowed', async () => { it('Returns an error message when the order size has more decimals then allowed', async () => {
const { result } = setup({ const { result } = setup({
fieldErrors: { size: { type: 'validate', message: ERROR_SIZE_DECIMAL } }, fieldErrors: { size: { type: `validate`, message: ERROR_SIZE_DECIMAL } },
});
expect(result.current).toStrictEqual({
isDisabled: true,
message: ERROR.FIELD_PRICE_STEP_DECIMAL,
});
}); });
expect(result.current).toEqual(ERROR.FIELD_PRICE_STEP_DECIMAL);
}); });

View File

@ -40,25 +40,32 @@ export const useOrderValidation = ({
}; };
} }
if ([MarketState.Cancelled, MarketState.Rejected].includes(market.state)) { if (
[
MarketState.Settled,
MarketState.Rejected,
MarketState.TradingTerminated,
].includes(market.state)
) {
return { return {
isDisabled: true, isDisabled: true,
message: t( message: t(
`This market is ${market.state.toLowerCase()} and not accepting orders` `This market is ${market.state.toLowerCase()} and no longer accepting orders`
), ),
}; };
} }
if ( if (
[ [
MarketState.Suspended,
MarketState.Pending,
MarketState.Proposed,
MarketState.Cancelled, MarketState.Cancelled,
MarketState.Closed, MarketState.Closed,
MarketState.Proposed,
MarketState.Settled,
].includes(market.state) ].includes(market.state)
) { ) {
return { return {
isDisabled: true, isDisabled: false,
message: t( message: t(
`This market is ${market.state.toLowerCase()} and only accepting liquidity orders` `This market is ${market.state.toLowerCase()} and only accepting liquidity orders`
), ),
@ -96,7 +103,7 @@ export const useOrderValidation = ({
return { return {
isDisabled: false, isDisabled: false,
message: t( message: t(
'This market is currently suspended and only accepting liquidity orders' `This market is ${market.state.toLowerCase()} and only accepting liquidity orders`
), ),
}; };
} }
@ -108,7 +115,7 @@ export const useOrderValidation = ({
return { return {
isDisabled: false, isDisabled: false,
message: t( message: t(
'This market is not active yet and can accept only liquidity orders' `This market is ${market.state.toLowerCase()} and only accepting liquidity orders`
), ),
}; };
} }

View File

@ -8,6 +8,7 @@ import { addDecimalsFormatNumber, t } from '@vegaprotocol/react-helpers';
import { useEnvironment } from '@vegaprotocol/environment'; import { useEnvironment } from '@vegaprotocol/environment';
import { OrderType } from '@vegaprotocol/types'; import { OrderType } from '@vegaprotocol/types';
import type { Order } from '../wallet-types'; import type { Order } from '../wallet-types';
import get from 'lodash/get';
export interface VegaTransactionDialogProps { export interface VegaTransactionDialogProps {
orderDialogOpen: boolean; orderDialogOpen: boolean;
@ -117,7 +118,8 @@ export const VegaDialog = ({
> >
{transaction.error && ( {transaction.error && (
<pre className="text-ui break-all whitespace-pre-wrap"> <pre className="text-ui break-all whitespace-pre-wrap">
{JSON.stringify(transaction.error, null, 2)} {get(transaction.error, 'error') ??
JSON.stringify(transaction.error, null, 2)}
</pre> </pre>
)} )}
</OrderDialogWrapper> </OrderDialogWrapper>