Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed Apr 18, 2024
1 parent af123ce commit 4f67187
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 251 deletions.
47 changes: 19 additions & 28 deletions examples/new-dawn/test/e2e/00_mock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
import {
EthereumWalletMock,
testWithEthereumWalletMock,
} from "@synthetixio/synpress";
import { EthereumWalletMock, testWithEthereumWalletMock } from '@synthetixio/synpress'

const test = testWithEthereumWalletMock;
const test = testWithEthereumWalletMock

const { expect } = test;
const { expect } = test

test("should mock MetaMask in the Test Dapp", async ({ page, walletMock }) => {
expect(await walletMock.getAllAccounts()).toHaveLength(1);
test('should mock MetaMask in the Test Dapp', async ({ page, walletMock }) => {
expect(await walletMock.getAllAccounts()).toHaveLength(1)

await page.locator("#connectButton").click();
await page.locator('#connectButton').click()

await expect(page.locator("#accounts")).toHaveText(
"0xd73b04b0e696b0945283defa3eee453814758f1a"
);
await expect(page.locator('#accounts')).toHaveText('0xd73b04b0e696b0945283defa3eee453814758f1a')

await page.locator("#getAccounts").click();
await expect(page.locator("#getAccountsResult")).toHaveText(
"0xd73b04b0e696b0945283defa3eee453814758f1a"
);
});
await page.locator('#getAccounts').click()
await expect(page.locator('#getAccountsResult')).toHaveText('0xd73b04b0e696b0945283defa3eee453814758f1a')
})

test("should add new account using MetaMask mock", async ({ page }) => {
const walletMock = new EthereumWalletMock(page);
test('should add new account using MetaMask mock', async ({ page }) => {
const walletMock = new EthereumWalletMock(page)

await walletMock.importWallet(
"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"
);
await walletMock.addNewAccount();
await walletMock.importWallet('candy maple cake sugar pudding cream honey rich smooth crumble sweet treat')
await walletMock.addNewAccount()

await page.locator("#connectButton").click();
await page.locator('#connectButton').click()

await expect(page.locator("#accounts")).toHaveText(
"0x6503D95e3F20389EE9496b277ABfFDb8eCCD2cc5,0xd73b04b0e696b0945283defa3eee453814758f1a"
);
});
await expect(page.locator('#accounts')).toHaveText(
'0x6503D95e3F20389EE9496b277ABfFDb8eCCD2cc5,0xd73b04b0e696b0945283defa3eee453814758f1a'
)
})
22 changes: 6 additions & 16 deletions release/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { defineWalletSetup } from "@synthetixio/synpress-core";
import {
getExtensionId,
testWithSynpress,
} from "@synthetixio/synpress-fixtures";
import {
MetaMask,
homePageSelectors,
unlockForFixture,
} from "@synthetixio/synpress-metamask";
import {
EthereumWalletMock,
testWithEthereumWalletMock,
} from "@synthetixio/ethereum-wallet-mock";
import { EthereumWalletMock, testWithEthereumWalletMock } from '@synthetixio/ethereum-wallet-mock'
import { defineWalletSetup } from '@synthetixio/synpress-core'
import { getExtensionId, testWithSynpress } from '@synthetixio/synpress-fixtures'
import { MetaMask, homePageSelectors, unlockForFixture } from '@synthetixio/synpress-metamask'

export {
// Framework fixtures
Expand All @@ -24,5 +14,5 @@ export {
defineWalletSetup,
getExtensionId,
unlockForFixture,
homePageSelectors,
};
homePageSelectors
}
129 changes: 58 additions & 71 deletions wallets/ethereum-wallet-mock/src/ethereum-wallet-mock.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import type { Page } from "@playwright/test";
import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts";
import type { Network } from "./network/Network";
import { ACCOUNT_MOCK, BLOCKCHAIN, BSC_NETWORK_ID } from "./utils";

export type WalletMock =
| "metamask"
| "coinbase"
| "phantom"
| "walletconnect"
| "walletlink";

/**
* This class is the heart of Synpress's MetaMask API.
*/
import type { Page } from '@playwright/test'
import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts'
import type { Network } from './network/Network'
import { ACCOUNT_MOCK, BLOCKCHAIN, BSC_NETWORK_ID } from './utils'

export type WalletMock = 'metamask' | 'coinbase' | 'phantom' | 'walletconnect' | 'walletlink'

export class EthereumWalletMock {
seedPhrase = "";
wallet: WalletMock = "metamask";

constructor(
/**
* The MetaMask tab page.
*/
readonly page: Page
) {
this.page = page;
seedPhrase = ''
wallet: WalletMock = 'metamask'

constructor(readonly page: Page) {

Check warning

Code scanning / CodeQL

Useless assignment to property Warning

This write to property 'page' is useless, since
another property write
always overrides it.
this.page = page
}

/**
Expand All @@ -32,53 +19,53 @@ export class EthereumWalletMock {
* @param seedPhrase - The seed phrase to import.
*/
importWallet(seedPhrase: string) {
this.seedPhrase = seedPhrase;
this.seedPhrase = seedPhrase

return this.page.evaluate(
([blockchain, wallet, accounts]) => {
return Web3Mock.mock({
blockchain,
wallet,
accounts: {
return: accounts,
},
});
return: accounts
}
})
},
[BLOCKCHAIN, this.wallet, [ACCOUNT_MOCK]]
);
)
}

/**
* Retrieves the current account address.
*/
async getAllAccounts(): Promise<string[]> {
return this.page.evaluate(() => {
return window.ethereum.request({ method: "eth_requestAccounts" });
});
return window.ethereum.request({ method: 'eth_requestAccounts' })
})
}

/**
* Adds a new account. This account is based on the initially imported seed phrase.
*/
async addNewAccount() {
const accounts = await this.getAllAccounts();
const accounts = await this.getAllAccounts()

const newAccount = mnemonicToAccount(this.seedPhrase, {
accountIndex: accounts.length,
});
accountIndex: accounts.length
})

return this.page.evaluate(
([blockchain, wallet, accounts]) => {
return Web3Mock.mock({
blockchain,
wallet,
accounts: {
return: accounts,
},
});
return: accounts
}
})
},
[BLOCKCHAIN, this.wallet, [newAccount.address, ...accounts]]
);
)
}

/**
Expand All @@ -87,20 +74,20 @@ export class EthereumWalletMock {
* @param privateKey - The private key to import.
*/
async importWalletFromPrivateKey(privateKey: `0x${string}`) {
const newAccount = privateKeyToAccount(privateKey);
const newAccount = privateKeyToAccount(privateKey)

return this.page.evaluate(
([blockchain, wallet, account]) => {
return Web3Mock.mock({
blockchain,
wallet,
accounts: {
return: account,
},
});
return: account
}
})
},
[BLOCKCHAIN, this.wallet, [newAccount.address]]
);
)
}

/**
Expand All @@ -115,12 +102,12 @@ export class EthereumWalletMock {
blockchain,
wallet,
accounts: {
return: [accountAddress],
},
});
return: [accountAddress]
}
})
},
[BLOCKCHAIN, this.wallet, accountAddress]
);
)
}

/**
Expand All @@ -139,28 +126,28 @@ export class EthereumWalletMock {
chainName: network.name,
nativeCurrency: network.nativeCurrency,
rpcUrls: [network.rpcUrl],
blockExplorerUrls: [network.blockExplorerUrl],
};
blockExplorerUrls: [network.blockExplorerUrl]
}

return this.page.evaluate(
([blockchain, wallet, networkInfo]) => {
return Web3Mock.mock({
blockchain,
wallet,
network: {
add: networkInfo,
},
});
add: networkInfo
}
})
},
[BLOCKCHAIN, this.wallet, networkInfo]
);
)
}

/**
* Retrieves the current account address.
*/
async getAccountAddress() {
return (await this.getAllAccounts())[0];
return (await this.getAllAccounts())[0]
}

/**
Expand All @@ -175,46 +162,46 @@ export class EthereumWalletMock {
blockchain,
wallet,
network: {
switchTo: networkName,
},
});
switchTo: networkName
}
})

window.ethereum.request({
method: "wallet_switchEthereumChain",
method: 'wallet_switchEthereumChain',
// Mock do not support custom network IDs
params: [{ chainId }],
});
params: [{ chainId }]
})
},
[BLOCKCHAIN, this.wallet, networkName, BSC_NETWORK_ID]
);
)
}

/**
* Connects wallet to the dapp.
*/
async connectToDapp(wallet: WalletMock = "metamask") {
this.wallet = wallet;
async connectToDapp(wallet: WalletMock = 'metamask') {
this.wallet = wallet
return this.page.evaluate(
([blockchain, accounts, wallet]) => {
// Cannot pass custom class as an argument to `page.evaluate`
class WalletConnectStub {}

let connector: WalletConnectStub | undefined;
let connector: WalletConnectStub | undefined

if (wallet === "walletconnect") {
connector = WalletConnectStub;
if (wallet === 'walletconnect') {
connector = WalletConnectStub
}

return Web3Mock.mock({
blockchain,
wallet,
accounts: {
return: accounts,
return: accounts
},
connector,
});
connector
})
},
[BLOCKCHAIN, [ACCOUNT_MOCK], wallet]
);
)
}
}
6 changes: 3 additions & 3 deletions wallets/ethereum-wallet-mock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./ethereum-wallet-mock";
export * from "./utils";
export * from "./testWithEthereumWalletMock";
export * from './ethereum-wallet-mock'
export * from './utils'
export * from './testWithEthereumWalletMock'
Loading

0 comments on commit 4f67187

Please sign in to comment.