Skip to content

Commit

Permalink
test(e2e): Improve burner wallet tests FE-859 (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelitow authored Nov 19, 2024
1 parent ccf20ef commit 70efbff
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
12 changes: 7 additions & 5 deletions e2e-tests/runner/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export const sessionTests = async (
// biome-ignore lint/suspicious/noExportsInTest: <explanation>
export const transferTests = async (
page: Page,
{ connect, approveTransfer }: ConnectorFunctions,
{ connect, approveTransfer, keepSession }: ConnectorFunctions,
) => {
await connect(page);
!keepSession && (await connect(page));

await page.click('text=Transfer 0.0001 ETH');
await approveTransfer(page);
Expand All @@ -59,16 +59,16 @@ export const transferTests = async (
await page.waitForSelector('text=Transferred successfully!'),
).toBeTruthy();

await page.click('text=Disconnect');
!keepSession && (await page.click('text=Disconnect'));
};

// biome-ignore lint/suspicious/noExportsInTest: <explanation>
export const incrementTests = async (
page: Page,
{ approveTransfer, connect }: ConnectorFunctions,
{ approveTransfer, connect, keepSession }: ConnectorFunctions,
) => {
await test.step('should connect and increment', async () => {
await connect(page);
!keepSession && (await connect(page));

const incrementButton = await page.getByRole('button', {
name: 'Increment',
Expand All @@ -81,5 +81,7 @@ export const incrementTests = async (
expect(
await page.waitForSelector('text=Counter Incremented!'),
).toBeTruthy();

!keepSession && (await page.click('text=Disconnect'));
});
};
1 change: 1 addition & 0 deletions e2e-tests/runner/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export type ApproveTransferFunction = (page: Page) => Promise<void>;
export interface ConnectorFunctions {
connect: ConnectFunction;
approveTransfer: ApproveTransferFunction;
keepSession?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { getButtonByText, getByAriaLabel, test } from '@fuels/playwright-utils';
import {
getButtonByText,
getByAriaLabel,
seedWallet,
test,
} from '@fuels/playwright-utils';
import { type Page, expect } from '@playwright/test';
import { sessionTests, skipBridgeFunds } from '../../../common/common';
import { type BN, Provider, Wallet, bn } from 'fuels';
import {
incrementTests,
sessionTests,
skipBridgeFunds,
transferTests,
} from '../../../common/common';
import type { ConnectFunction } from '../../../common/types';
import { transferMaxBalance } from '../setup';

const { VITE_FUEL_PROVIDER_URL, VITE_WALLET_SECRET } = process.env as Record<
string,
string
>;

const connect: ConnectFunction = async (page: Page) => {
await page.bringToFront();
Expand All @@ -22,5 +39,50 @@ test.describe('BurnerWalletConnector', async () => {

test('BurnerWallet Tests', async ({ page }) => {
await sessionTests(page, { connect, approveTransfer: async () => {} });
await connect(page);

const addressElement = await page.locator('css=#address');

const address = await addressElement.getAttribute('data-address');
const amount: BN = bn(100_000_000);

if (address) {
await seedWallet(
address,
amount,
VITE_FUEL_PROVIDER_URL || '',
VITE_WALLET_SECRET || '',
);
} else {
throw new Error('Address is null');
}

await incrementTests(page, {
connect,
approveTransfer: async () => {},
keepSession: true,
});

await transferTests(page, {
connect,
approveTransfer: async () => {},
keepSession: true,
});

const privateKey = await page.evaluate(() =>
localStorage.getItem('burner-wallet-private-key'),
);

if (!privateKey) {
throw new Error('Private key is null');
}

const burnerWallet = Wallet.fromPrivateKey(privateKey);
const fuelProvider = await Provider.create(VITE_FUEL_PROVIDER_URL);
burnerWallet.connect(fuelProvider);
await transferMaxBalance({
fromWallet: burnerWallet,
toWallet: Wallet.fromPrivateKey(VITE_WALLET_SECRET || ''),
});
});
});
12 changes: 3 additions & 9 deletions e2e-tests/runner/examples/connectors/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,12 @@ export const testSetup = async ({
};

export const fundWallet = async ({ publicKey }: { publicKey: string }) => {
const fuelProvider = await Provider.create(VITE_FUEL_PROVIDER_URL);
const masterWallet = Wallet.fromMnemonic(VITE_MASTER_WALLET_MNEMONIC);
masterWallet.connect(fuelProvider);

const txResponse = await masterWallet.transfer(
await seedWallet(
publicKey,
bn.parseUnits('0.1'),
VITE_FUEL_PROVIDER_URL,
VITE_WALLET_SECRET,
);

await txResponse.waitForResult();

return true;
};

export const transferMaxBalance = async ({
Expand Down

0 comments on commit 70efbff

Please sign in to comment.