Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed Apr 8, 2024
1 parent 276fbf2 commit 1b41be1
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 206 deletions.
24 changes: 10 additions & 14 deletions examples/new-dawn/test/e2e/00_mock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { testWithMetaMaskMock, MetaMaskMock } from "@synthetixio/synpress";
import { MetaMaskMock, testWithMetaMaskMock } from '@synthetixio/synpress'

const test = testWithMetaMaskMock;
const test = testWithMetaMaskMock

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

test("should work to the MetaMask Test Dapp", async ({ page }) => {
const metamask = new MetaMaskMock(page);
test('should work to the MetaMask Test Dapp', async ({ page }) => {
const metamask = new MetaMaskMock(page)

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable metamask.

await page.goto("/");
await page.goto('/')

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')
})
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 {
testWithMetaMaskMock,
MetaMaskMock,
} from "@synthetixio/synpress-metamask-mock";
import { defineWalletSetup } from '@synthetixio/synpress-core'
import { getExtensionId, testWithSynpress } from '@synthetixio/synpress-fixtures'
import { MetaMask, homePageSelectors, unlockForFixture } from '@synthetixio/synpress-metamask'
import { MetaMaskMock, testWithMetaMaskMock } from '@synthetixio/synpress-metamask-mock'

export {
// Framework fixtures
Expand All @@ -24,5 +14,5 @@ export {
defineWalletSetup,
getExtensionId,
unlockForFixture,
homePageSelectors,
};
homePageSelectors
}
11 changes: 6 additions & 5 deletions wallets/metamask-mock/environment.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
CI: boolean;
HEADLESS: boolean;
CI: boolean
HEADLESS: boolean
}
}
}

declare global {
interface Window {
ethereum: any;
ethereum: import('ethers').Eip1193Provider
}

const Web3Mock: any;
// biome-ignore lint/suspicious/noExplicitAny: Web3Mock is a mock object
const Web3Mock: any
}

export {};
export {}
6 changes: 3 additions & 3 deletions wallets/metamask-mock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./metamask-mock";
export * from "./utils";
export * from "./testWithMetaMaskMock";
export * from './metamask-mock'
export * from './utils'
export * from './testWithMetaMaskMock'
96 changes: 47 additions & 49 deletions wallets/metamask-mock/src/metamask-mock.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts";
// @ts-ignore
import { connect } from "@depay/web3-mock";
import type { Page } from "@playwright/test";
import type { Network } from "./network/Network";
import { connect } from '@depay/web3-mock'
import type { Page } from '@playwright/test'
import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts'
import type { Network } from './network/Network'

export const blockchain = "ethereum";
export const wallet = "metamask";
export const blockchain = 'ethereum'
export const wallet = 'metamask'

const networkConfig = {
blockchain,
wallet,
};
wallet
}

const BSC_NETWORK_ID = "0x38";
const BSC_NETWORK_ID = '0x38'

export const SEED_PHRASE =
"test test test test test test test test test test test junk";
export const SEED_PHRASE = 'test test test test test test test test test test test junk'

export const PRIVATE_KEY =
"ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a";
export const PRIVATE_KEY = 'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'

/**
* This class is the heart of Synpress's MetaMask API.
*/
export class MetaMaskMock {
seedPhrase = "";
seedPhrase = ''

constructor(
/**
* The MetaMask tab page.
*/
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;
this.page = page
}

/**
Expand All @@ -41,39 +39,39 @@ export class MetaMaskMock {
* @param seedPhrase - The seed phrase to import.
*/
importWallet(seedPhrase: string) {
this.seedPhrase = seedPhrase;
this.seedPhrase = seedPhrase
}

/**
* 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(
([networkConfig, accounts]) => {
return Web3Mock.mock({
...networkConfig,
accounts: {
return: accounts,
},
});
return: accounts
}
})
},
[networkConfig, [newAccount.address, ...accounts]]
);
)
}

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

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

/**
Expand All @@ -109,12 +107,12 @@ export class MetaMaskMock {
// @ts-ignore
...networkConfig,
accounts: {
return: [accountAddress],
},
});
return: [accountAddress]
}
})
},
[networkConfig, accountAddress]
);
)
}

/**
Expand All @@ -133,27 +131,27 @@ export class MetaMaskMock {
chainName: network.name,
nativeCurrency: network.nativeCurrency,
rpcUrls: [network.rpcUrl],
blockExplorerUrls: [network.blockExplorerUrl],
};
blockExplorerUrls: [network.blockExplorerUrl]
}

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

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

/**
Expand All @@ -168,24 +166,24 @@ export class MetaMaskMock {
// @ts-ignore
...networkConfig,
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 }]
})
},
[networkConfig, networkName, BSC_NETWORK_ID]
);
)
}

/**
* Connects to the dapp using the currently selected account.
*/
async connectToDapp(network: string) {
connect(network);
connect(network)
}
}
18 changes: 9 additions & 9 deletions wallets/metamask-mock/src/network/Network.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type Network = {
name: string;
rpcUrl: string;
chainId: number;
blockExplorerUrl?: string;
name: string
rpcUrl: string
chainId: number
blockExplorerUrl?: string
nativeCurrency?: {
name: string;
symbol: string;
decimals: number;
};
};
name: string
symbol: string
decimals: number
}
}
Loading

0 comments on commit 1b41be1

Please sign in to comment.