Skip to content

Commit

Permalink
refactor mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
frontendphil committed Oct 21, 2024
1 parent 28dad0b commit e49eba9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 51 deletions.
72 changes: 23 additions & 49 deletions extension/e2e/accountHandling.spec.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions extension/e2e/mockWeb3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ const web3Content: MutableRefObject<string | null> = { 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 {
Expand Down

0 comments on commit e49eba9

Please sign in to comment.