chore(trading): withdraw tests to jest (#5109)

Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
This commit is contained in:
Ben 2023-10-25 08:45:58 +01:00 committed by GitHub
parent d85f413e41
commit f7a82d33ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 170 deletions

View File

@ -1,131 +0,0 @@
import { connectEthereumWallet } from '../support/ethereum-wallet';
import { selectAsset } from '../support/helpers';
const formFieldError = 'input-error-text';
const toAddressField = 'input[name="to"]';
const amountField = 'input[name="amount"]';
const useMaximumAmount = 'use-maximum';
const submitWithdrawBtn = 'submit-withdrawal';
const ethAddressValue = Cypress.env('ETHEREUM_WALLET_ADDRESS');
const ASSET_SEPOLIA_TBTC = 2;
const ASSET_EURO = 1;
describe('withdraw form validation', { tags: '@smoke' }, () => {
before(() => {
cy.mockWeb3Provider();
cy.mockTradingPage();
cy.mockSubscription();
cy.setVegaWallet();
cy.visit('/#/portfolio');
cy.getByTestId('Withdrawals').click();
cy.getByTestId('Withdraw').click(); // sidebar item
// It also requires connection Ethereum wallet
connectEthereumWallet('MetaMask');
cy.wait('@Accounts');
cy.wait('@Assets');
});
it('empty fields', () => {
cy.getByTestId(submitWithdrawBtn).click();
cy.getByTestId(formFieldError).should('contain.text', 'Required');
// only 2 despite 3 fields because the ethereum address will be auto populated
cy.getByTestId(formFieldError).should('have.length', 2);
// Test for Ethereum address
cy.get(toAddressField).should('have.value', ethAddressValue);
});
it('min amount', () => {
// 1002-WITH-010
selectAsset(ASSET_SEPOLIA_TBTC);
cy.get(amountField).clear().type('0');
cy.getByTestId(submitWithdrawBtn).click();
cy.get('[data-testid="input-error-text"]').should(
'contain.text',
'Value is below minimum'
);
});
it('max amount', () => {
// 1002-WITH-005
// 1002-WITH-008
selectAsset(ASSET_EURO); // Will be above maximum because the vega wallet doesn't have any collateral
cy.get(amountField).clear().type('1001', { delay: 100 });
cy.getByTestId(submitWithdrawBtn).click();
cy.get('[data-testid="input-error-text"]').should(
'contain.text',
'Insufficient amount in account'
);
});
it('can set amount using use maximum button', () => {
// 1002-WITH-004
selectAsset(ASSET_SEPOLIA_TBTC);
cy.getByTestId(useMaximumAmount).click();
cy.get(amountField).should('have.value', '1000.00001');
});
});
describe(
'withdraw actions',
{ tags: '@regression', testIsolation: true },
() => {
// this is extremely ugly hack, but setting it properly in contract is too much effort for such simple validation
// 1002-WITH-018
const withdrawalThreshold =
Cypress.env('VEGA_ENV') === 'CUSTOM' ? '0.00' : '100.00';
before(() => {
cy.mockWeb3Provider();
cy.mockTradingPage();
cy.mockSubscription();
cy.setVegaWallet();
cy.visit('/#/portfolio');
cy.wait('@Accounts');
cy.wait('@Assets');
cy.getByTestId('Withdrawals').click();
cy.getByTestId('Withdraw').click();
// It also requires connection Ethereum wallet
connectEthereumWallet('MetaMask');
cy.mockVegaWalletTransaction();
});
it('triggers transaction when submitted', () => {
// 1002-WITH-002
// 1002-WITH-003
selectAsset(ASSET_SEPOLIA_TBTC);
cy.getByTestId('BALANCE_AVAILABLE_label').should(
'contain.text',
'Balance available'
);
cy.getByTestId('BALANCE_AVAILABLE_value').should(
'have.text',
'1,000.00001'
);
cy.getByTestId('WITHDRAWAL_THRESHOLD_label').should(
'contain.text',
'Delayed withdrawal threshold'
);
cy.getByTestId('WITHDRAWAL_THRESHOLD_value').should(
'contain.text',
withdrawalThreshold
);
cy.getByTestId('DELAY_TIME_label').should('contain.text', 'Delay time');
cy.getByTestId('DELAY_TIME_value').should('have.text', 'None');
cy.get(amountField).clear().type('10');
cy.getByTestId(submitWithdrawBtn).click();
cy.getByTestId('toast').should('contain.text', 'Awaiting confirmation');
});
}
);

View File

@ -1,4 +1,4 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { generateAccount, generateAsset } from './test-helpers';
import type { WithdrawManagerProps } from './withdraw-manager';
@ -57,10 +57,18 @@ describe('WithdrawManager', () => {
);
it('calls submit if valid form submission', async () => {
// 1002-WITH-002
// 1002-WITH-003
const { container } = render(generateJsx(props));
await act(async () => {
await submitValid(container);
});
const select = container.querySelector('select[name="asset"]') as Element;
await userEvent.selectOptions(select, props.assets[0].id);
await userEvent.clear(screen.getByLabelText('To (Ethereum address)'));
await userEvent.type(
screen.getByLabelText('To (Ethereum address)'),
ethereumAddress
);
await userEvent.type(screen.getByLabelText('Amount'), '0.01');
await userEvent.click(screen.getByTestId('submit-withdrawal'));
expect(props.submit).toHaveBeenCalledWith({
amount: '1000',
asset: props.assets[0].id,
@ -70,58 +78,56 @@ describe('WithdrawManager', () => {
});
it('validates correctly', async () => {
render(generateJsx(props));
// 1002-WITH-010
// 1002-WITH-005
// 1002-WITH-008
// 1002-WITH-018
const { container } = render(generateJsx(props));
// Set other fields to be valid
fireEvent.change(screen.getByLabelText('Asset'), {
target: { value: props.assets[0].id },
});
fireEvent.change(screen.getByLabelText('To (Ethereum address)'), {
target: { value: ethereumAddress },
});
const select = container.querySelector('select[name="asset"]') as Element;
await userEvent.selectOptions(select, props.assets[0].id);
expect(screen.getByTestId('connect-eth-wallet-btn')).toBeInTheDocument();
await userEvent.type(
screen.getByLabelText('To (Ethereum address)'),
ethereumAddress
);
// Min amount
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '0.00000001' },
});
fireEvent.submit(screen.getByTestId('withdraw-form'));
await userEvent.clear(screen.getByLabelText('Amount'));
await userEvent.type(screen.getByLabelText('Amount'), '0.00000001');
await userEvent.click(screen.getByTestId('submit-withdrawal'));
expect(
await screen.findByText('Value is below minimum')
).toBeInTheDocument();
expect(props.submit).not.toBeCalled();
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '0.00001' },
});
await userEvent.clear(screen.getByLabelText('Amount'));
await userEvent.type(screen.getByLabelText('Amount'), '0.00001');
// Max amount (balance is 1)
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '2' },
});
fireEvent.submit(screen.getByTestId('withdraw-form'));
await userEvent.clear(screen.getByLabelText('Amount'));
await userEvent.type(screen.getByLabelText('Amount'), '2');
await userEvent.click(screen.getByTestId('submit-withdrawal'));
expect(
await screen.findByText('Insufficient amount in account')
).toBeInTheDocument();
expect(props.submit).not.toBeCalled();
});
it('can set amount using use maximum button', async () => {
// 1002-WITH-004
render(generateJsx(props));
const submitValid = async (container: HTMLElement) => {
const select = container.querySelector('select[name="asset"]') as Element;
await userEvent.selectOptions(select, props.assets[0].id);
fireEvent.change(screen.getByLabelText('To (Ethereum address)'), {
target: { value: ethereumAddress },
});
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '0.01' },
});
fireEvent.submit(screen.getByTestId('withdraw-form'));
};
await userEvent.click(screen.getByTestId('use-maximum'));
expect(screen.getByTestId('amount-input')).toHaveValue(1);
});
it('shows withdraw delay notification if amount greater than threshold', async () => {
render(generateJsx(props));
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '1001' },
});
await userEvent.type(screen.getByLabelText('Amount'), '1001');
expect(
await screen.findByTestId('amount-withdrawal-delay-notification')
).toBeInTheDocument();
@ -130,9 +136,7 @@ describe('WithdrawManager', () => {
it('shows withdraw delay notification if threshold is 0', async () => {
withdrawAsset.threshold = new BigNumber(0);
render(generateJsx(props));
fireEvent.change(screen.getByLabelText('Amount'), {
target: { value: '0.01' },
});
await userEvent.type(screen.getByLabelText('Amount'), '0.01');
expect(
await screen.findByTestId('withdrawals-delay-notification')
).toBeInTheDocument();