Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite farm stake dialog tests #507

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,151 +1,163 @@
// import { getBalance, getTokenBalance } from '@/test/e2e/utils'
// import { testIds } from '@/ui/utils/testIds'
// import { Locator, Page, expect } from '@playwright/test'
// import { Address } from 'viem'
// import { DialogPageObject, TxOverviewWithRoute } from '../../../common/Dialog.PageObject'

// export class SavingsDialogPageObject extends DialogPageObject {
// private readonly type: 'deposit' | 'withdraw' | 'send'

// constructor({ page, type }: { page: Page; type: 'deposit' | 'withdraw' | 'send' }) {
// super(
// page,
// new RegExp(`${type === 'deposit' ? 'Deposit to' : type === 'send' ? 'Send from' : 'Withdraw from'} Savings`),
// )
// this.type = type
// }

// // # region locators
// locateUpgradeSwitch(): Locator {
// return this.page.getByTestId(testIds.dialog.savings.upgradeSwitch)
// }
// // #endregion locators

// // #region actions
// async clickBackToSavingsButton(): Promise<void> {
// await this.page.getByRole('button', { name: 'Back to Savings' }).click()
// await this.region.waitFor({
// state: 'detached',
// })
// }

// async fillReceiverAction(receiver: string): Promise<void> {
// await this.region.getByTestId(testIds.component.AddressInput.input).fill(receiver)
// }

// // #endregion actions

// // #region assertions
// async expectDiscrepancyWarning(discrepancy: string): Promise<void> {
// const explanation =
// this.type === 'deposit'
// ? 'The final amount received may be less than the deposit amount by up to'
// : 'You may be charged more than the withdraw amount by up to'
// await expect(
// this.region.getByText(`Market fluctuations can impact your transaction value. ${explanation} ${discrepancy}`),
// ).toBeVisible()
// }

// async expectTransactionOverviewToBeVisible(): Promise<void> {
// await expect(this.locatePanelByHeader('Transaction overview')).toBeVisible()
// }

// async expectAssetSelectorOptions(options: string[]): Promise<void> {
// const selectorOptions = await this.page.getByTestId(testIds.component.AssetSelector.option).all()
// expect(selectorOptions).toHaveLength(options.length)

// for (const [index, option] of selectorOptions.entries()) {
// await expect(option).toContainText(options[index]!)
// }
// }

// async expectTransactionOverview(transactionOverview: TransactionOverview): Promise<void> {
// const panel = this.locatePanelByHeader('Transaction overview')
// await expect(panel).toBeVisible()

// for (const [index, [label, value]] of transactionOverview.entries()) {
// const row = panel.getByTestId(testIds.dialog.depositSavings.transactionDetailsRow(index))
// await expect(row).toBeVisible()
// await expect(row).toContainText(label)
// await expect(row).toContainText(value)
// }
// }

// async expectNativeRouteTransactionOverview(transactionOverview: NativeRouteTransactionOverview): Promise<void> {
// const panel = this.locatePanelByHeader('Transaction overview')
// await expect(panel).toBeVisible()
// const savingsTxOverviewTestIds = testIds.dialog.savings.transactionOverview

// if (transactionOverview.apy) {
// const apyValue = panel.getByTestId(savingsTxOverviewTestIds.apy.value)
// const apyDescription = panel.getByTestId(savingsTxOverviewTestIds.apy.description)
// await expect(apyValue).toContainText(transactionOverview.apy.value)
// await expect(apyDescription).toContainText(transactionOverview.apy.description)
// }

// await this.expectTransactionOverviewRoute(transactionOverview.routeItems)
// if (transactionOverview.badgeTokens) {
// await this.expectSkyBadgeForTokens(transactionOverview.badgeTokens)
// }
// await this.expectOutcomeText(transactionOverview.outcome)
// }

// async expectSuccessPage(): Promise<void> {
// // for now we only check if the success message is visible
// await expect(this.page.getByText('Congrats, all done!')).toBeVisible()
// }

// async expectAddressInputError(error: string): Promise<void> {
// await expect(this.page.getByTestId(testIds.component.AddressInput.error)).toHaveText(error)
// }

// async expectReceiverIsSmartContractWarning(): Promise<void> {
// await expect(this.page.getByTestId(testIds.dialog.savings.send.addressIsSmartContractWarning)).toBeVisible()
// }

// async expectReceiverBalance({
// forkUrl,
// receiver,
// expectedBalance,
// }: {
// forkUrl: string
// receiver: Address
// expectedBalance: number
// }): Promise<void> {
// const currentBalance = await getBalance({ forkUrl, address: receiver })
// expect(currentBalance.isEqualTo(expectedBalance)).toBe(true)
// }

// async expectReceiverTokenBalance({
// forkUrl,
// receiver,
// token,
// expectedBalance,
// }: {
// forkUrl: string
// receiver: Address
// token: { address: Address; decimals: number }
// expectedBalance: number
// }): Promise<void> {
// const currentTokenBalance = await getTokenBalance({ forkUrl, address: receiver, token })
// expect(currentTokenBalance.isEqualTo(expectedBalance)).toBe(true)
// }

// expectUpgradeSwitchToBeHidden(): Promise<void> {
// return expect(this.locateUpgradeSwitch()).toBeHidden()
// }

// clickUpgradeSwitch(): Promise<void> {
// return this.locateUpgradeSwitch().click()
// }
// // #endregion assertions
// }

// type TransactionOverview = [string, string][]

// interface NativeRouteTransactionOverview extends TxOverviewWithRoute {
// apy?: {
// value: string
// description: string
// }
// }
import { TestContext } from '@/test/e2e/setup'
import { getBalance, getTokenBalance } from '@/test/e2e/utils'
import { testIds } from '@/ui/utils/testIds'
import { getUrlFromClient } from '@marsfoundation/common-testnets'
import { Locator, expect } from '@playwright/test'
import { Address } from 'viem'
import { DialogPageObject, TxOverviewWithRoute } from '../../../common/Dialog.PageObject'

export interface SavingsDialogPageObjectParams {
testContext: TestContext<any>
type: 'deposit' | 'withdraw' | 'send'
}

export class SavingsDialogPageObject extends DialogPageObject {
private readonly type: 'deposit' | 'withdraw' | 'send'

constructor({ testContext, type }: SavingsDialogPageObjectParams) {
super({
testContext,
header: new RegExp(
`${type === 'deposit' ? 'Deposit to' : type === 'send' ? 'Send from' : 'Withdraw from'} Savings`,
),
})
this.type = type
}

// # region locators
locateUpgradeSwitch(): Locator {
return this.page.getByTestId(testIds.dialog.savings.upgradeSwitch)
}
// #endregion locators

// #region actions
async clickBackToSavingsButton(): Promise<void> {
await this.page.getByRole('button', { name: 'Back to Savings' }).click()
await this.region.waitFor({
state: 'detached',
})
}

async fillReceiverAction(receiver: string): Promise<void> {
await this.region.getByTestId(testIds.component.AddressInput.input).fill(receiver)
}

// #endregion actions

// #region assertions
async expectDiscrepancyWarning(discrepancy: string): Promise<void> {
const explanation =
this.type === 'deposit'
? 'The final amount received may be less than the deposit amount by up to'
: 'You may be charged more than the withdraw amount by up to'
await expect(
this.region.getByText(`Market fluctuations can impact your transaction value. ${explanation} ${discrepancy}`),
).toBeVisible()
}

async expectTransactionOverviewToBeVisible(): Promise<void> {
await expect(this.locatePanelByHeader('Transaction overview')).toBeVisible()
}

async expectAssetSelectorOptions(options: string[]): Promise<void> {
const selectorOptions = await this.page.getByTestId(testIds.component.AssetSelector.option).all()
expect(selectorOptions).toHaveLength(options.length)

for (const [index, option] of selectorOptions.entries()) {
await expect(option).toContainText(options[index]!)
}
}

async expectTransactionOverview(transactionOverview: TransactionOverview): Promise<void> {
const panel = this.locatePanelByHeader('Transaction overview')
await expect(panel).toBeVisible()

for (const [index, [label, value]] of transactionOverview.entries()) {
const row = panel.getByTestId(testIds.dialog.depositSavings.transactionDetailsRow(index))
await expect(row).toBeVisible()
await expect(row).toContainText(label)
await expect(row).toContainText(value)
}
}

async expectNativeRouteTransactionOverview(transactionOverview: NativeRouteTransactionOverview): Promise<void> {
const panel = this.locatePanelByHeader('Transaction overview')
await expect(panel).toBeVisible()
const savingsTxOverviewTestIds = testIds.dialog.savings.transactionOverview

if (transactionOverview.apy) {
const apyValue = panel.getByTestId(savingsTxOverviewTestIds.apy.value)
const apyDescription = panel.getByTestId(savingsTxOverviewTestIds.apy.description)
await expect(apyValue).toContainText(transactionOverview.apy.value)
await expect(apyDescription).toContainText(transactionOverview.apy.description)
}

await this.expectTransactionOverviewRoute(transactionOverview.routeItems)
if (transactionOverview.badgeTokens) {
await this.expectSkyBadgeForTokens(transactionOverview.badgeTokens)
}
await this.expectOutcomeText(transactionOverview.outcome)
}

async expectSuccessPage(): Promise<void> {
// for now we only check if the success message is visible
await expect(this.page.getByText('Congrats, all done!')).toBeVisible()
}

async expectAddressInputError(error: string): Promise<void> {
await expect(this.page.getByTestId(testIds.component.AddressInput.error)).toHaveText(error)
}

async expectReceiverIsSmartContractWarning(): Promise<void> {
await expect(this.page.getByTestId(testIds.dialog.savings.send.addressIsSmartContractWarning)).toBeVisible()
}

async expectReceiverBalance({
receiver,
expectedBalance,
}: {
receiver: Address
expectedBalance: number
}): Promise<void> {
const currentBalance = await getBalance({
forkUrl: getUrlFromClient(this.testContext.testnetController.client),
address: receiver,
})
expect(currentBalance.isEqualTo(expectedBalance)).toBe(true)
}

async expectReceiverTokenBalance({
receiver,
token,
expectedBalance,
}: {
receiver: Address
token: { address: Address; decimals: number }
expectedBalance: number
}): Promise<void> {
const currentTokenBalance = await getTokenBalance({
forkUrl: getUrlFromClient(this.testContext.testnetController.client),
address: receiver,
token,
})
expect(currentTokenBalance.isEqualTo(expectedBalance)).toBe(true)
}

expectUpgradeSwitchToBeHidden(): Promise<void> {
return expect(this.locateUpgradeSwitch()).toBeHidden()
}

clickUpgradeSwitch(): Promise<void> {
return this.locateUpgradeSwitch().click()
}
// #endregion assertions
}

type TransactionOverview = [string, string][]

interface NativeRouteTransactionOverview extends TxOverviewWithRoute {
apy?: {
value: string
description: string
}
}
Loading
Loading