diff --git a/libs/accounts/src/lib/margin-heath-chart.spec.tsx b/libs/accounts/src/lib/margin-heath-chart.spec.tsx deleted file mode 100644 index 18e232933..000000000 --- a/libs/accounts/src/lib/margin-heath-chart.spec.tsx +++ /dev/null @@ -1,158 +0,0 @@ -import { - MarginHealthChart, - MarginHealthChartTooltip, -} from './margin-health-chart'; -import { act, render, screen } from '@testing-library/react'; -import type { MarginFieldsFragment } from './__generated__/Margins'; -import type { AssetFieldsFragment } from '@vegaprotocol/assets'; -import { MarginMode } from '@vegaprotocol/types'; - -const asset: AssetFieldsFragment = { - id: 'assetId', - decimals: 2, -} as AssetFieldsFragment; -const margins: MarginFieldsFragment = { - asset: { - id: 'assetId', - }, - collateralReleaseLevel: '1000', - initialLevel: '800', - searchLevel: '600', - maintenanceLevel: '400', - marginFactor: '', - marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, - orderMarginLevel: '', - market: { - id: 'marketId', - }, -}; - -const mockGetMargins = jest.fn(() => margins); -const mockGetBalance = jest.fn(() => '0'); - -jest.mock('./margin-data-provider', () => ({})); - -jest.mock('@vegaprotocol/assets', () => ({ - useAssetsMapProvider: () => { - return { - data: { - assetId: asset, - }, - }; - }, -})); - -jest.mock('@vegaprotocol/wallet', () => ({ - useVegaWallet: () => { - return { - pubKey: 'partyId', - }; - }, -})); - -jest.mock('@vegaprotocol/data-provider', () => ({ - useDataProvider: () => { - return { - data: mockGetMargins(), - }; - }, -})); - -jest.mock('./use-account-balance', () => ({ - useAccountBalance: () => { - return { - accountBalance: mockGetBalance(), - }; - }, -})); - -jest.mock('./use-market-account-balance', () => ({ - useMarketAccountBalance: () => { - return { - accountBalance: '700', - }; - }, -})); - -describe('MarginHealthChart', () => { - it('should render correct values', async () => { - render(); - const chart = screen.getByTestId('margin-health-chart'); - expect(chart).toHaveTextContent('3.00 above maintenance level'); - const red = screen.getByTestId('margin-health-chart-red'); - const orange = screen.getByTestId('margin-health-chart-orange'); - const yellow = screen.getByTestId('margin-health-chart-yellow'); - const green = screen.getByTestId('margin-health-chart-green'); - const balance = screen.getByTestId('margin-health-chart-balance'); - expect(parseInt(red.style.width)).toBe(40); - expect(parseInt(orange.style.width)).toBe(20); - expect(parseInt(yellow.style.width)).toBe(10); - expect(parseInt(green.style.width)).toBe(30); - expect(parseInt(balance.style.left)).toBe(70); - }); - - it('should use correct scale', async () => { - mockGetBalance.mockReturnValueOnce('1300'); - await act(async () => { - render(); - }); - await screen.findByTestId('margin-health-chart'); - const red = screen.getByTestId('margin-health-chart-red'); - expect(parseInt(red.style.width)).toBe(20); - }); -}); - -describe('MarginHealthChartTooltip', () => { - it('renders correct values and labels', async () => { - await act(async () => { - render( - - ); - }); - const labels = await screen.findAllByTestId('margin-health-tooltip-label'); - const expectedLabels = [ - 'maintenance level', - 'balance', - 'search level', - 'initial level', - 'release level', - ]; - labels.forEach((value, i) => { - expect(value).toHaveTextContent(expectedLabels[i]); - }); - const values = await screen.findAllByTestId('margin-health-tooltip-value'); - const expectedValues = ['4.00', '5.00', '6.00', '8.00', '10.00']; - values.forEach((value, i) => { - expect(value).toHaveTextContent(expectedValues[i]); - }); - }); - - it('renders balance in correct place', async () => { - const { rerender } = render( - - ); - - let values = await screen.findAllByTestId('margin-health-tooltip-value'); - expect(values[2]).toHaveTextContent('7.00'); - - rerender( - - ); - - values = await screen.findAllByTestId('margin-health-tooltip-value'); - expect(values.length).toBe(5); - expect(values[3]).toHaveTextContent('9.00'); - }); -}); diff --git a/libs/positions/src/lib/estimate-position.mock.ts b/libs/positions/src/lib/estimate-position.mock.ts index d5536737b..b1af23637 100644 --- a/libs/positions/src/lib/estimate-position.mock.ts +++ b/libs/positions/src/lib/estimate-position.mock.ts @@ -1,6 +1,7 @@ import type { PartialDeep } from 'type-fest'; import merge from 'lodash/merge'; import type { EstimatePositionQuery } from './__generated__/Positions'; +import { MarginMode } from '@vegaprotocol/types'; export const estimatePositionQuery = ( override?: PartialDeep @@ -14,14 +15,24 @@ export const estimatePositionQuery = ( initialLevel: '500000', maintenanceLevel: '200000', searchLevel: '300000', + marginFactor: '1', + orderMarginLevel: '0', + marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, }, worstCase: { collateralReleaseLevel: '1100000', initialLevel: '600000', maintenanceLevel: '300000', searchLevel: '400000', + marginFactor: '1', + orderMarginLevel: '0', + marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, }, }, + collateralIncreaseEstimate: { + bestCase: '0', + worstCase: '0', + }, liquidation: { bestCase: { including_buy_orders: '1', diff --git a/libs/positions/src/lib/liquidation-price.spec.tsx b/libs/positions/src/lib/liquidation-price.spec.tsx index f438e09c7..d57bfac4c 100644 --- a/libs/positions/src/lib/liquidation-price.spec.tsx +++ b/libs/positions/src/lib/liquidation-price.spec.tsx @@ -9,32 +9,23 @@ import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { MarginMode } from '@vegaprotocol/types'; describe('LiquidationPrice', () => { - const props = { + const variables = { marketId: 'market-id', openVolume: '100', - decimalPlaces: 2, - averageEntryPrice: '100', - generalAccountBalance: '100', - marginAccountBalance: '100', - orderMarginAccountBalance: '100', + averageEntryPrice: '10', + marginAccountBalance: '500', + generalAccountBalance: '500', + orderMarginAccountBalance: '0', marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, marginFactor: '1', }; + const props = { ...variables, decimalPlaces: 2 }; const worstCaseOpenVolume = '200'; const bestCaseOpenVolume = '100'; const mock: MockedResponse = { request: { query: EstimatePositionDocument, - variables: { - marketId: props.marketId, - openVolume: props.openVolume, - averageEntryPrice: props.averageEntryPrice, - generalAccountBalance: props.generalAccountBalance, - marginAccountBalance: props.marginAccountBalance, - orderMarginAccountBalance: props.orderMarginAccountBalance, - marginMode: props.marginMode, - marginFactor: props.marginFactor, - }, + variables, }, result: { data: { @@ -45,14 +36,24 @@ describe('LiquidationPrice', () => { searchLevel: '100', initialLevel: '100', collateralReleaseLevel: '100', + orderMarginLevel: '0', + marginFactor: '0', + marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, }, bestCase: { maintenanceLevel: '100', searchLevel: '100', initialLevel: '100', collateralReleaseLevel: '100', + orderMarginLevel: '0', + marginFactor: '0', + marginMode: MarginMode.MARGIN_MODE_CROSS_MARGIN, }, }, + collateralIncreaseEstimate: { + bestCase: '0', + worstCase: '0', + }, liquidation: { worstCase: { open_volume_only: worstCaseOpenVolume, diff --git a/libs/positions/src/lib/positions.mock.ts b/libs/positions/src/lib/positions.mock.ts index 7e15feefd..8d7a9ff9c 100644 --- a/libs/positions/src/lib/positions.mock.ts +++ b/libs/positions/src/lib/positions.mock.ts @@ -181,6 +181,7 @@ const marginsFields: MarginFieldsFragment[] = [ ]; export const singleRow: Position = { + marginFactor: '1', generalAccountBalance: '12345600', maintenanceLevel: '12300000', marginMode: Schema.MarginMode.MARGIN_MODE_CROSS_MARGIN,