Skip to content

Commit

Permalink
fix: flaky e2e contract tests (#1640)
Browse files Browse the repository at this point in the history
- Contract tests now display:
  - Connection status
  - Contract call errors below each field
 - Buttons are now disabled when required data is not present
 - Replaced `timeouts` with `expect.poll` using new visual indicators
  • Loading branch information
arthurgeron authored Oct 29, 2024
1 parent 7d1d08d commit d6a9426
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ test.describe('Deposit Half ETH', () => {

const depositHalfButton = getButtonByText(page, 'Deposit Half ETH', true);

await page.waitForTimeout(3000);
await expect
.poll(() => depositHalfButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await depositHalfButton.click();

const walletNotificationPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ test.describe('Forward and Mint Multicall', () => {
page,
'Deposit And Mint Multicall'
);
await page.waitForTimeout(2500);
await expect
.poll(() => forwardHalfAndMintButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await forwardHalfAndMintButton.click();

const walletNotificationPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ test.describe('Forward Eth', () => {
await forwardEthInput.fill(forwardEthAmount);

const forwardEthButton = getButtonByText(page, 'Forward ETH');
await page.waitForTimeout(2500);
await expect
.poll(() => forwardEthButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await forwardEthButton.click();

const walletNotificationPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ test.describe('Forward Half ETH and Mint External Custom Asset', () => {
page,
'Forward Half And External Mint'
);
await page.waitForTimeout(2500);
await expect
.poll(() => forwardHalfAndMintButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await forwardHalfAndMintButton.click();

const walletNotificationPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ test.describe('Forward Half ETH and Mint Custom Asset', () => {
page,
'Forward Half And Mint'
);
await page.waitForTimeout(2500);
await expect
.poll(() => forwardHalfAndMintButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await forwardHalfAndMintButton.click();

const walletNotificationPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ test.describe('Forward Half Custom Asset', () => {
page,
'Forward Half Custom Asset'
);
await page.waitForTimeout(2500);
await expect
.poll(() => forwardHalfCustomAssetButton.isEnabled().catch(() => false), {
timeout: 15000,
})
.toBeTruthy();
await forwardHalfCustomAssetButton.click();

const walletNotificationPage =
Expand Down
8 changes: 6 additions & 2 deletions packages/e2e-contract-tests/playwright/e2e/MintAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ test.describe('Mint Assets', () => {
await mintInput.fill(mintAmount);

const mintButton = getButtonByText(page, 'Mint', true);
await page.waitForTimeout(3000);
await expect
.poll(() => mintButton.isEnabled().catch(() => false), { timeout: 15000 })
.toBeTruthy();
await mintButton.click();

// test asset is correct
Expand Down Expand Up @@ -126,7 +128,9 @@ test.describe('Mint Assets', () => {
await decimalsInput.fill(decimals);

const mintButton = getButtonByText(page, 'Mint Asset configuration');
await page.waitForTimeout(3000);
await expect
.poll(() => mintButton.isEnabled().catch(() => false), { timeout: 15000 })
.toBeTruthy();
await mintButton.click();

// test asset is correct
Expand Down
16 changes: 15 additions & 1 deletion packages/e2e-contract-tests/playwright/e2e/utils/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { FuelWalletTestHelper } from '@fuels/playwright-utils';
import { getButtonByText, getByAriaLabel } from '@fuels/playwright-utils';
import {
expect,
getButtonByText,
getByAriaLabel,
hasText,
} from '@fuels/playwright-utils';
import type { Page } from '@playwright/test';

export const connect = async (
Expand All @@ -12,5 +17,14 @@ export const connect = async (
await getByAriaLabel(page, `Connect to ${walletName}`, true).click();
await fuelWalletTestHelper.walletConnect();

await expect
.poll(
() =>
hasText(page, 'Status: Connected')
.then(() => true)
.catch(() => false),
{ timeout: 15000 }
)
.toBeTruthy();
await page.waitForTimeout(3000);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { bn } from 'fuels';
import { useState } from 'react';

import { mint } from '../contract_interactions';
import { useAction } from '../hooks/useAction';

export const AssetConfigurationCard = () => {
const [assetData, setAssetData] = useState({
Expand All @@ -11,7 +12,28 @@ export const AssetConfigurationCard = () => {
decimals: '',
});
const { account } = useAccount();
const wallet = useWallet(account);
const wallet = useWallet({ account });
const { disabled, execute, error } = useAction({
isValid:
!!wallet &&
!!assetData.amount &&
!!assetData.subId &&
!!assetData.decimals,
action: async () => {
if (
wallet.wallet &&
assetData.amount &&
assetData.subId &&
assetData.decimals
) {
await mint({
wallet: wallet.wallet!,
amount: bn.parseUnits(assetData.amount, Number(assetData.decimals)),
subId: assetData.subId,
});
}
},
});

return (
<div>
Expand Down Expand Up @@ -47,23 +69,10 @@ export const AssetConfigurationCard = () => {
}
/>
<br />
<button
type="button"
onClick={async () => {
if (wallet.wallet) {
await mint({
wallet: wallet.wallet,
amount: bn.parseUnits(
assetData.amount,
Number(assetData.decimals)
),
subId: assetData.subId,
});
}
}}
>
<button type="button" disabled={disabled} onClick={execute}>
Mint Asset configuration
</button>
{error && <p style={{ color: 'red' }}>{error}</p>}
<hr />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@ import { bn } from 'fuels';
import { useState } from 'react';

import { depositAndMintMultiCall } from '../contract_interactions';
import { useAction } from '../hooks/useAction';
import { useBaseAssetId } from '../hooks/useBaseAssetId';

export const DepositAndMintMultiCalls = () => {
const [forwardAmount, setForwardAmount] = useState<string>('');
const [mintAmount, setMintAmount] = useState<string>('');
const { account } = useAccount();
const wallet = useWallet(account);
const wallet = useWallet({ account });
const baseAssetId = useBaseAssetId();
const { disabled, execute, error } = useAction({
isValid: !!baseAssetId && !!wallet && !!forwardAmount && !!mintAmount,
action: async () => {
if (baseAssetId && wallet.wallet && mintAmount && forwardAmount) {
await depositAndMintMultiCall({
wallet: wallet.wallet!,
forwardAmount: bn.parseUnits(forwardAmount),
mintAmount: bn.parseUnits(mintAmount, 1).div(10),
assetId: baseAssetId,
baseAssetId,
});
}
},
});

return (
<div>
Expand All @@ -36,23 +51,10 @@ export const DepositAndMintMultiCalls = () => {
value={mintAmount}
/>
<br />
<button
type="button"
disabled={!baseAssetId}
onClick={async () => {
if (baseAssetId && wallet.wallet && mintAmount && forwardAmount) {
await depositAndMintMultiCall({
wallet: wallet.wallet,
forwardAmount: bn.parseUnits(forwardAmount),
mintAmount: bn.parseUnits(mintAmount, 1).div(10),
assetId: baseAssetId,
baseAssetId,
});
}
}}
>
<button type="button" disabled={disabled} onClick={execute}>
Deposit And Mint Multicall
</button>
{error && <p style={{ color: 'red' }}>{error}</p>}
<hr />
</div>
</div>
Expand Down
30 changes: 16 additions & 14 deletions packages/e2e-contract-tests/src/components/DepositHalfEthCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import { bn } from 'fuels';
import { useState } from 'react';

import { depositHalf } from '../contract_interactions';
import { useAction } from '../hooks/useAction';
import { useBaseAssetId } from '../hooks/useBaseAssetId';

export const DepositHalfEthCard = () => {
const [amount, setAmount] = useState<string>('');
const { account } = useAccount();
const wallet = useWallet(account);
const wallet = useWallet({ account });
const baseAssetId = useBaseAssetId();
const { disabled, execute, error } = useAction({
isValid: !!baseAssetId && !!wallet && !!amount,
action: async () => {
if (baseAssetId && wallet.wallet && amount) {
await depositHalf({
wallet: wallet.wallet!,
amount: bn.parseUnits(amount),
assetId: baseAssetId,
});
}
},
});

return (
<div>
Expand All @@ -24,21 +37,10 @@ export const DepositHalfEthCard = () => {
value={amount}
/>
<br />
<button
type="button"
disabled={!baseAssetId}
onClick={async () => {
if (baseAssetId && wallet.wallet && amount) {
await depositHalf({
wallet: wallet.wallet,
amount: bn.parseUnits(amount),
assetId: baseAssetId,
});
}
}}
>
<button type="button" disabled={disabled} onClick={execute}>
Deposit Half ETH
</button>
{error && <p style={{ color: 'red' }}>{error}</p>}
<hr />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@ import { useState } from 'react';

import { MAIN_CONTRACT_ID } from '../config';
import { deposit } from '../contract_interactions';
import { useAction } from '../hooks/useAction';
import { useBaseAssetId } from '../hooks/useBaseAssetId';
import { calculateAssetId } from '../utils';

export const ForwardCustomAssetCard = () => {
const [amount, setAmount] = useState<string>('');
const { account } = useAccount();
const wallet = useWallet(account);
const wallet = useWallet({ account });

const baseAssetId = useBaseAssetId();
const assetId =
!!baseAssetId && calculateAssetId(MAIN_CONTRACT_ID, baseAssetId);
const { disabled, execute, error } = useAction({
isValid: !!baseAssetId && !!wallet && !!amount,
action: async () => {
if (assetId && wallet.wallet && amount) {
await deposit({
wallet: wallet.wallet!,
amount: bn.parseUnits(amount, 1).div(10),
assetId,
});
}
},
});

return (
<div>
Expand All @@ -27,21 +40,10 @@ export const ForwardCustomAssetCard = () => {
value={amount}
/>
<br />
<button
type="button"
disabled={!assetId}
onClick={async () => {
if (assetId && wallet.wallet && amount) {
await deposit({
wallet: wallet.wallet,
amount: bn.parseUnits(amount, 1).div(10),
assetId,
});
}
}}
>
<button type="button" disabled={disabled} onClick={execute}>
Forward Custom Asset
</button>
{error && <p style={{ color: 'red' }}>{error}</p>}
<hr />
</div>
</div>
Expand Down
Loading

0 comments on commit d6a9426

Please sign in to comment.