Skip to content

Commit

Permalink
fix: swaps test, ref #4725
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Dec 19, 2023
1 parent 13aa68f commit 7db0784
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
28 changes: 14 additions & 14 deletions tests/page-object-models/swap.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ import { createTestSelector } from '@tests/utils';

export class SwapPage {
readonly page: Page;
readonly chooseAssetList: Locator;
readonly chooseAssetListItem: Locator;
readonly selectAssetBtn: Locator;
readonly swapAmountInput: Locator;
readonly swapBtn: Locator;
readonly swapDetailsAmount: Locator;
readonly swapDetailsProtocol: Locator;
readonly swapDetailsSymbol: Locator;
readonly swapReviewBtn: Locator;
readonly chooseAssetList = createTestSelector(SwapSelectors.ChooseAssetList);
readonly chooseAssetListItem = createTestSelector(SwapSelectors.ChooseAssetListItem);
readonly selectAssetBtn = createTestSelector(SwapSelectors.SelectAssetTriggerBtn);
readonly swapAmountInput = createTestSelector(SwapSelectors.SwapAmountInput);
readonly swapReviewBtn = createTestSelector(SwapSelectors.SwapReviewBtn);

constructor(page: Page) {
this.page = page;
this.chooseAssetList = page.getByTestId(SwapSelectors.ChooseAssetList);
this.chooseAssetListItem = page.getByTestId(SwapSelectors.ChooseAssetListItem);
this.selectAssetBtn = page.getByTestId(SwapSelectors.SelectAssetTriggerBtn);
this.swapAmountInput = page.getByTestId(SwapSelectors.SwapAmountInput);
this.swapBtn = page.getByTestId(SwapSelectors.SwapBtn);
this.swapDetailsAmount = page.getByTestId(SwapSelectors.SwapDetailsAmount);
this.swapDetailsProtocol = page.getByTestId(SwapSelectors.SwapDetailsProtocol);
this.swapDetailsSymbol = page.getByTestId(SwapSelectors.SwapDetailsSymbol);
this.swapReviewBtn = page.getByTestId(SwapSelectors.SwapReviewBtn);
}

async waitForSwapPageReady() {
Expand All @@ -34,11 +29,16 @@ export class SwapPage {
}

async selectAssetToReceive() {
const swapAssetSelectors = await this.selectAssetBtn.all();
const swapAssetSelectors = await this.page.locator(this.selectAssetBtn).all();
await swapAssetSelectors[1].click();
await this.chooseAssetList.waitFor();
const swapAssets = await this.chooseAssetListItem.all();
await this.page.locator(this.chooseAssetList).waitFor();
const swapAssets = await this.page.locator(this.chooseAssetListItem).all();
await swapAssets[0].click();
await this.swapReviewBtn.click();
await this.page.locator(this.swapReviewBtn).click();
}

async inputSwapAmountFrom() {
const swapAmountInputs = await this.page.locator(this.swapAmountInput).all();
await swapAmountInputs[0].fill('1');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test.describe('Transaction signing', () => {
const multiSignatureTxHex = await generateMultisigUnsignedStxTransfer(
TEST_ACCOUNT_2_STX_ADDRESS,
amount,
100,
'mainnet',
[TEST_ACCOUNT_3_PUBKEY, TEST_ACCOUNT_1_PUBKEY],
2,
Expand Down
29 changes: 18 additions & 11 deletions tests/specs/swap/swap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { test } from '../../fixtures/fixtures';

const alexSdkPostRoute = 'https://*.alexlab.co/v1/graphql';
const hiroApiPostRoute = '*/**/v2/transactions';

test.describe('Swaps', () => {
test.beforeEach(async ({ extensionId, globalPage, homePage, onboardingPage, swapPage }) => {
await globalPage.setupAndUseApiCalls(extensionId);
await onboardingPage.signInWithTestAccount(extensionId);
await homePage.enableTestMode();
await homePage.swapButton.click();
await swapPage.waitForSwapPageReady();
});
Expand All @@ -16,8 +16,7 @@ test.describe('Swaps', () => {
});

test('that it shows correct swap review details', async ({ swapPage }) => {
const swapAmountInputs = await swapPage.swapAmountInput.all();
await swapAmountInputs[0].fill('1');
await swapPage.inputSwapAmountFrom();
await swapPage.selectAssetToReceive();

const swapProtocol = await swapPage.swapDetailsProtocol.innerText();
Expand All @@ -37,14 +36,22 @@ test.describe('Swaps', () => {
});

test('that the swap is broadcast', async ({ swapPage }) => {
const requestPromise = swapPage.page.waitForRequest(alexSdkPostRoute);

await swapPage.page.route(alexSdkPostRoute, async route => {
await route.abort();
});

const swapAmountInputs = await swapPage.swapAmountInput.all();
await swapAmountInputs[0].fill('1');
let requestPromise;
const isSponsoredSwap = swapPage.page.getByText('Sponsored');

if (isSponsoredSwap) {
requestPromise = swapPage.page.waitForRequest(alexSdkPostRoute);
await swapPage.page.route(alexSdkPostRoute, async route => {
await route.abort();
});
} else {
requestPromise = swapPage.page.waitForRequest(hiroApiPostRoute);
await swapPage.page.route(alexSdkPostRoute, async route => {
await route.abort();
});
}

await swapPage.inputSwapAmountFrom();
await swapPage.selectAssetToReceive();

await swapPage.swapBtn.click();
Expand Down
23 changes: 16 additions & 7 deletions tests/specs/transactions/transactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ test.describe('Transaction signing', () => {

testAppPage = await TestAppPage.openDemoPage(context);
await testAppPage.signIn();
const accountsPage = await context.waitForEvent('page');
await accountsPage.locator('text="Account 1"').click();
await testAppPage.page.bringToFront();
await testAppPage.page.click('text=Debugger', {
timeout: 30000,
});
await accountsPage.close();
});

// These tests often break if ran in parallel
Expand All @@ -31,6 +24,14 @@ test.describe('Transaction signing', () => {
test('that it validates against insufficient funds when performing a contract call', async ({
context,
}) => {
const accountsPage = await context.waitForEvent('page');
await accountsPage.locator('text="Account 2"').click();
await testAppPage.page.bringToFront();
await testAppPage.page.click('text=Debugger', {
timeout: 30000,
});
await accountsPage.close();

await testAppPage.clickContractCallButton();
const transactionRequestPage = new TransactionRequestPage(await context.waitForEvent('page'));
const error =
Expand All @@ -42,6 +43,14 @@ test.describe('Transaction signing', () => {

test.describe('App initiated STX transfer', () => {
test('that it broadcasts correctly with given fee and amount', async ({ context }) => {
const accountsPage = await context.waitForEvent('page');
await accountsPage.locator('text="Account 1"').click();
await testAppPage.page.bringToFront();
await testAppPage.page.click('text=Debugger', {
timeout: 30000,
});
await accountsPage.close();

await testAppPage.clickStxTransferButton();
const transactionRequestPage = new TransactionRequestPage(await context.waitForEvent('page'));

Expand Down
2 changes: 2 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export async function generateUnsignedStxTransfer(
export async function generateMultisigUnsignedStxTransfer(
recipient: string,
amount: number,
fee: number,
network: any,
publicKeys: string[],
threshold: number,
Expand All @@ -53,6 +54,7 @@ export async function generateMultisigUnsignedStxTransfer(
memo?: string
) {
const options = {
fee,
recipient,
memo,
publicKeys,
Expand Down

0 comments on commit 7db0784

Please sign in to comment.