Skip to content

Commit

Permalink
feat(ui-test): cover reject signing (#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
arein authored Dec 28, 2023
1 parent 8d5737e commit 0825fcf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
10 changes: 8 additions & 2 deletions apps/laboratory/src/components/Ethers/EthersConnectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useWeb3ModalAccount, useWeb3ModalProvider } from '@web3modal/ethers/rea
import { BrowserProvider, JsonRpcSigner } from 'ethers'
import { EthersTransactionButton } from './EthersTransactionButton'
import type { TypedDataField } from 'ethers'
import { SigningFailedToastTitle, SigningSucceededToastTitle } from '../../constants'

// Example data
const types: Record<string, TypedDataField[]> = {
Expand Down Expand Up @@ -44,10 +45,15 @@ export function EthersConnectButton() {
const signer = new JsonRpcSigner(provider, address)
const signature = await signer?.signMessage('Hello Web3Modal Ethers')

toast({ title: 'Success', description: signature, status: 'success', isClosable: true })
toast({
title: SigningSucceededToastTitle,
description: signature,
status: 'success',
isClosable: true
})
} catch {
toast({
title: 'Error',
title: SigningFailedToastTitle,
description: 'Failed to sign message',
status: 'error',
isClosable: true
Expand Down
10 changes: 8 additions & 2 deletions apps/laboratory/src/components/Wagmi/WagmiConnectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button, useToast } from '@chakra-ui/react'
import { useAccount, useNetwork, useSignMessage, useSignTypedData } from 'wagmi'
import { WagmiTransactionButton } from './WagmiTransactionButton'
import { useEffect, useState } from 'react'
import { SigningFailedToastTitle, SigningSucceededToastTitle } from '../../constants'

// Example data

Expand Down Expand Up @@ -59,10 +60,15 @@ export function WagmiConnectButton() {
async function onSignMessage() {
try {
const signature = await signMessageAsync()
toast({ title: 'Success', description: signature, status: 'success', isClosable: true })
toast({
title: SigningSucceededToastTitle,
description: signature,
status: 'success',
isClosable: true
})
} catch {
toast({
title: 'Error',
title: SigningFailedToastTitle,
description: 'Failed to sign message',
status: 'error',
isClosable: true
Expand Down
2 changes: 2 additions & 0 deletions apps/laboratory/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SigningSucceededToastTitle = 'Signing Succeeded'
export const SigningFailedToastTitle = 'Signing Failed'
10 changes: 10 additions & 0 deletions apps/laboratory/tests/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ testMW('it should sign', async ({ modalPage, walletPage, modalValidator, walletV
await walletPage.handleRequest({ accept: true })
await modalValidator.expectAcceptedSign()
})

testMW(
'it should reject sign',
async ({ modalPage, walletPage, modalValidator, walletValidator }) => {
await modalPage.sign()
await walletValidator.expectReceivedSign({})
await walletPage.handleRequest({ accept: false })
await modalValidator.expectRejectedSign()
}
)
2 changes: 2 additions & 0 deletions apps/laboratory/tests/shared/pages/WalletPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ export class WalletPage {
*/
async handleSessionProposal(opts: SessionParams) {
const variant = opts.accept ? `approve` : `reject`
// `.click` doesn't work here, so we use `.focus` and `Space`
await this.page.getByTestId(`session-${variant}-button`).isEnabled()
await this.page.getByTestId(`session-${variant}-button`).focus()
await this.page.keyboard.press('Space')
}

async handleRequest({ accept }: { accept: boolean }) {
const variant = accept ? `approve` : `reject`
// `.click` doesn't work here, so we use `.focus` and `Space`
await this.page.getByTestId(`session-${variant}-button`).isEnabled()
await this.page.getByTestId(`session-${variant}-button`).focus()
await this.page.keyboard.press('Space')
Expand Down
9 changes: 8 additions & 1 deletion apps/laboratory/tests/shared/validators/ModalValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test'
import type { Page } from '@playwright/test'
import { SigningFailedToastTitle, SigningSucceededToastTitle } from '../../../src/constants'

export class ModalValidator {
constructor(public readonly page: Page) {}
Expand All @@ -13,6 +14,12 @@ export class ModalValidator {
}

async expectAcceptedSign() {
await expect(this.page.getByText('Success')).toBeVisible()
// We use Chakra Toast and it's not quite straightforward to set the `data-testid` attribute on the toast element.
await expect(this.page.getByText(SigningSucceededToastTitle)).toBeVisible()
}

async expectRejectedSign() {
// We use Chakra Toast and it's not quite straightforward to set the `data-testid` attribute on the toast element.
await expect(this.page.getByText(SigningFailedToastTitle)).toBeVisible()
}
}

3 comments on commit 0825fcf

@vercel
Copy link

@vercel vercel bot commented on 0825fcf Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 0825fcf Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 0825fcf Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.