From e49eba9589a1a0990678938edd1956f599fb0f7e Mon Sep 17 00:00:00 2001 From: Philipp Giese Date: Mon, 21 Oct 2024 16:39:29 +0200 Subject: [PATCH] refactor mocking --- extension/e2e/accountHandling.spec.ts | 72 +++++++++------------------ extension/e2e/mockWeb3.ts | 7 ++- 2 files changed, 28 insertions(+), 51 deletions(-) diff --git a/extension/e2e/accountHandling.spec.ts b/extension/e2e/accountHandling.spec.ts index 0ef91cc4..d73e6341 100644 --- a/extension/e2e/accountHandling.spec.ts +++ b/extension/e2e/accountHandling.spec.ts @@ -1,31 +1,29 @@ -import { mock } from '@depay/web3-mock' +import { Page } from '@playwright/test' import { expect, test } from './fixture' import { loadExtension } from './loadExtension' import { mockWeb3 } from './mockWeb3' test.describe('Locked account', () => { + const account = '0x1000000000000000000000000000000000000000' + + const openConfiguration = async (page: Page) => { + await page.getByRole('link', { name: 'Configure routes' }).click() + await page.getByRole('button', { name: 'Add Route' }).click() + await page.getByRole('button', { name: 'Connect with MetaMask' }).click() + await expect(page.getByText(account)).toBeInViewport() + } + test('handles wallet disconnect gracefully', async ({ page, extensionId, }) => { - const { lockWallet } = mockWeb3(page, () => - mock({ - blockchain: 'ethereum', - accounts: { return: ['0x1000000000000000000000000000000000000000'] }, - }) - ) + const { lockWallet } = await mockWeb3(page, { + accounts: [account], + }) const extension = await loadExtension(page, extensionId) - await extension.getByRole('link', { name: 'Configure routes' }).click() - await extension.getByRole('button', { name: 'Add Route' }).click() - await extension - .getByRole('button', { name: 'Connect with MetaMask' }) - .click() - await expect( - extension.getByText('0x1000000000000000000000000000000000000000') - ).toBeInViewport() - + await openConfiguration(extension) await lockWallet() await expect( @@ -37,55 +35,31 @@ test.describe('Locked account', () => { page, extensionId, }) => { - const { lockWallet } = mockWeb3(page, () => - mock({ - blockchain: 'ethereum', - accounts: { return: ['0x1000000000000000000000000000000000000000'] }, - }) - ) + const { lockWallet } = await mockWeb3(page, { + accounts: [account], + }) const extension = await loadExtension(page, extensionId) - await extension.getByRole('link', { name: 'Configure routes' }).click() - await extension.getByRole('button', { name: 'Add Route' }).click() - await extension - .getByRole('button', { name: 'Connect with MetaMask' }) - .click() - await expect( - extension.getByText('0x1000000000000000000000000000000000000000') - ).toBeInViewport() - + await openConfiguration(extension) await lockWallet() await extension.getByRole('button', { name: 'Reconnect' }).click() - await expect( - extension.getByText('0x1000000000000000000000000000000000000000') - ).toBeInViewport() + await expect(extension.getByText(account)).toBeInViewport() }) test('it is possible to disconnect a locked account', async ({ page, extensionId, }) => { - const { lockWallet } = mockWeb3(page, () => - mock({ - blockchain: 'ethereum', - accounts: { return: ['0x1000000000000000000000000000000000000000'] }, - }) - ) + const { lockWallet } = await mockWeb3(page, { + accounts: [account], + }) const extension = await loadExtension(page, extensionId) - await extension.getByRole('link', { name: 'Configure routes' }).click() - await extension.getByRole('button', { name: 'Add Route' }).click() - await extension - .getByRole('button', { name: 'Connect with MetaMask' }) - .click() - await expect( - extension.getByText('0x1000000000000000000000000000000000000000') - ).toBeInViewport() - + await openConfiguration(extension) await lockWallet() await extension.getByRole('button', { name: 'Disconnect' }).click() diff --git a/extension/e2e/mockWeb3.ts b/extension/e2e/mockWeb3.ts index e29473ab..036249cd 100644 --- a/extension/e2e/mockWeb3.ts +++ b/extension/e2e/mockWeb3.ts @@ -9,14 +9,17 @@ const web3Content: MutableRefObject = { current: null } declare global { type mock = { trigger: (event: string, data: unknown) => void + mock: (options: { chain: string; accounts: { return: string[] } }) => void } const Web3Mock: mock } -export const mockWeb3 = (page: Page, fn: () => unknown) => { +type MockOptions = { accounts: string[] } + +export const mockWeb3 = async (page: Page, { accounts }: MockOptions) => { page.addInitScript({ - content: `${getLibraryCode()}\n(${fn.toString().replaceAll('mock', 'Web3Mock.mock')})()`, + content: `${getLibraryCode()}\n(() => { Web3Mock.mock(${JSON.stringify({ blockchain: 'ethereum', accounts: { return: accounts } })})})()`, }) return {