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

✨ feat: Add renameAccount method #1084

Closed
9 changes: 9 additions & 0 deletions wallets/metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export class MetaMask {
await this.homePage.addNewAccount(accountName)
}

/**
* Renames the currently selected account.
*
* @param newAccountName - The new name for the account.
*/
async renameAccount(newAccountName: string) {
await this.homePage.renameAccount(newAccountName)
}

/**
* Imports a wallet using the given private key.
*
Expand Down
1 change: 1 addition & 0 deletions wallets/metamask/src/pages/HomePage/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export * from './addNetwork'
export * from './toggleShowTestNetworks'
export * from './addNewAccount'
export * from './transactionDetails'
export * from './renameAccount'
export { default as getAccountAddress } from './getAccountAddress'
24 changes: 24 additions & 0 deletions wallets/metamask/src/pages/HomePage/actions/renameAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Page } from '@playwright/test'
import { z } from 'zod'
import Selectors from '../selectors'

// @todo, look into validation schema that's language agnostic for Metamask reserved words (""Account 1", "Account 2", etc.)
const Account = z.object({
name: z.string().refine((value) => {
return value.trim().length > 0
}, 'Invalid account name')
})

export type Account = z.infer<typeof Account>

export async function renameAccount(page: Page, newAccountName: Account['name']) {
Account.parse({ name: newAccountName }) // Validate newAccountName against Account schema

await page.locator(Selectors.accountMenu.accountButton).click()

await page.locator(Selectors.accountMenu.renameAccountMenu.listItemButton).click()
await page.locator(Selectors.accountMenu.renameAccountMenu.listItemDetailButton).click()
await page.locator(Selectors.accountMenu.renameAccountMenu.renameButton).click()
await page.locator(Selectors.accountMenu.renameAccountMenu.renameInput).fill(newAccountName)
await page.locator(Selectors.accountMenu.renameAccountMenu.confirmRenameButton).click()
}
5 changes: 5 additions & 0 deletions wallets/metamask/src/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getAccountAddress,
importWalletFromPrivateKey,
lock,
renameAccount,
settings,
switchAccount,
switchNetwork,
Expand Down Expand Up @@ -37,6 +38,10 @@ export class HomePage {
await addNewAccount(this.page, accountName)
}

async renameAccount(newAccountName: string) {
await renameAccount(this.page, newAccountName)
}

async getAccountAddress() {
return await getAccountAddress(this.page)
}
Expand Down
11 changes: 10 additions & 1 deletion wallets/metamask/src/pages/HomePage/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const addNewAccountMenu = {
createButton: `${accountMenuContainer} button.mm-button-primary`
}

const renameAccountMenu = {
listItemButton: `${accountMenuContainer} ${createDataTestSelector('account-list-item-menu-button')}`,
listItemDetailButton: `${createDataTestSelector('account-list-menu-details')}`,
renameButton: `${createDataTestSelector('editable-label-button')}`,
confirmRenameButton: 'div.editable-label button.mm-button-icon',
renameInput: 'input'
}

const importAccountMenu = {
privateKeyInput: `${accountMenuContainer} input#private-key-box`,
importButton: `${accountMenuContainer} ${createDataTestSelector('import-account-confirm-button')}`,
Expand All @@ -29,7 +37,8 @@ const addAccountMenu = {
const accountMenu = {
accountButton: createDataTestSelector('account-menu-icon'),
accountNames: `${accountMenuContainer} .multichain-account-menu-popover__list .multichain-account-list-item__account-name__button`,
addAccountMenu
addAccountMenu,
renameAccountMenu
}

const threeDotsMenu = {
Expand Down
23 changes: 23 additions & 0 deletions wallets/metamask/test/e2e/metamask/renameAccount.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { testWithSynpress } from '@synthetixio/synpress-fixtures'
import { MetaMask, unlockForFixture } from '../../../src'

import basicSetup from '../wallet-setup/basic.setup'

const test = testWithSynpress(basicSetup, unlockForFixture)

const { expect } = test

test('should rename current account with specified name', async ({ context, metamaskPage }) => {
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword)

const accountName = 'Test Account'
await metamask.renameAccount(accountName)

await expect(metamaskPage.locator(metamask.homePage.selectors.accountMenu.accountButton)).toHaveText(accountName)
})

test('should throw Zod error if an empty account name is passed', async ({ context, metamaskPage }) => {
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword)

await expect(metamask.renameAccount('')).rejects.toThrowError('Invalid account name')
})