Skip to content

Commit

Permalink
iron out some test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed May 29, 2024
1 parent 81dcbe5 commit dffcb98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
31 changes: 3 additions & 28 deletions src/components/tx/SignOrExecuteForm/PermissionsCheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import { useCurrentChain } from '@/hooks/useChains'
import { dispatchModuleTxExecution } from '@/services/tx/tx-sender'
import useOnboard from '@/hooks/wallets/useOnboard'
import { assertOnboard, assertWallet } from '@/utils/helpers'
import { trimTrailingSlash } from '@/utils/url'
import { TxModalContext } from '@/components/tx-flow'
import { getModuleTransactionId } from '@/services/transactions'

const Role = ({ children }: { children: string }) => {
let humanReadableRoleKey = children
Expand Down Expand Up @@ -454,13 +454,13 @@ const useGasLimit = (
return { gasLimit, gasLimitError, gasLimitLoading }
}

const pollModuleTransactionId = async (props: {
export const pollModuleTransactionId = async (props: {
transactionService: string
safeAddress: string
txHash: string
}): Promise<string> => {
// exponential delay between attempts for around 4 min
return backOff(() => fetchModuleTransactionId(props), {
return backOff(() => getModuleTransactionId(props), {
startingDelay: 750,
maxDelay: 20000,
numOfAttempts: 19,
Expand All @@ -470,28 +470,3 @@ const pollModuleTransactionId = async (props: {
},
})
}

const fetchModuleTransactionId = async ({
transactionService,
safeAddress,
txHash,
}: {
transactionService: string
safeAddress: string
txHash: string
}) => {
const url = `${trimTrailingSlash(
transactionService,
)}/api/v1/safes/${safeAddress}/module-transactions/?transaction_hash=${txHash}`
const { results } = await fetch(url).then((res) => {
if (res.ok && res.status === 200) {
return res.json() as Promise<any>
} else {
throw new Error('Error fetching Safe module transactions')
}
})

if (results.length === 0) throw new Error('module transaction not found')

return results[0].moduleTransactionId as string
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { createMockSafeTransaction } from '@/tests/transactions'
import { OperationType } from '@safe-global/safe-core-sdk-types'
import { type ReactElement } from 'react'
import * as zodiacRoles from 'zodiac-roles-deployments'
import { fireEvent, render, waitFor, mockWeb3Provider } from '@/tests/test-utils'

import { type ConnectedWallet } from '@/hooks/wallets/useOnboard'
import * as useSafeInfoHook from '@/hooks/useSafeInfo'
import * as wallet from '@/hooks/wallets/useWallet'
import * as onboardHooks from '@/hooks/wallets/useOnboard'
import * as txSender from '@/services/tx/tx-sender/dispatch'
import { mockWeb3Provider, render } from '@/tests/test-utils'
import { fireEvent, waitFor } from '@testing-library/react'
import { extendedSafeInfoBuilder } from '@/tests/builders/safe'
import { type OnboardAPI } from '@web3-onboard/core'
import { AbiCoder, encodeBytes32String } from 'ethers'
import PermissionsCheck from '../PermissionsCheck'
import PermissionsCheck, * as permissionsCheckModule from '../PermissionsCheck'

// We assume that CheckWallet always returns true
jest.mock('@/components/common/CheckWallet', () => ({
Expand All @@ -30,10 +29,16 @@ jest.mock('@/hooks/useChains', () => ({
chainId: '1',
chainName: 'Ethereum',
features: [],
transactionService: 'https://tx.service.mock',
})),
useHasFeature: jest.fn(() => true), // used to check for EIP1559 support
}))

// mock getModuleTransactionId
jest.mock('@/services/transactions', () => ({
getModuleTransactionId: jest.fn(() => 'i1234567890'),
}))

describe('PermissionsCheck', () => {
let executeSpy: jest.SpyInstance

Expand Down Expand Up @@ -87,6 +92,8 @@ describe('PermissionsCheck', () => {
// Mock return value of useWeb3ReadOnly
// It's only used for eth_estimateGas requests
mockWeb3Provider([])

jest.spyOn(permissionsCheckModule, 'pollModuleTransactionId').mockReturnValue(Promise.resolve('i1234567890'))
})

it('execute the tx when the submit button is clicked', async () => {
Expand All @@ -97,7 +104,9 @@ describe('PermissionsCheck', () => {
operation: OperationType.Call,
})

const { findByText } = render(<PermissionsCheck safeTx={safeTx} />)
const onSubmit = jest.fn()

const { findByText } = render(<PermissionsCheck safeTx={safeTx} onSubmit={onSubmit} />)

fireEvent.click(await findByText('Execute through role', { selector: 'button' }))

Expand All @@ -114,6 +123,11 @@ describe('PermissionsCheck', () => {
expect.anything(),
)
})

// calls provided onSubmit callback
await waitFor(() => {
expect(onSubmit).toHaveBeenCalled()
})
})
})

Expand Down
29 changes: 29 additions & 0 deletions src/services/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import memoize from 'lodash/memoize'
import { getTransactionDetails, getTransactionHistory } from '@safe-global/safe-gateway-typescript-sdk'
import { trimTrailingSlash } from '@/utils/url'

export const getTimezoneOffset = () => new Date().getTimezoneOffset() * 60 * -1000

Expand Down Expand Up @@ -28,3 +29,31 @@ export const getTxHistory = (chainId: string, safeAddress: string, trusted = fal
pageUrl,
)
}

/**
* Fetch the module transaction id from the transaction service providing the transaction hash
*/
export const getModuleTransactionId = async ({
transactionService,
safeAddress,
txHash,
}: {
transactionService: string
safeAddress: string
txHash: string
}) => {
const url = `${trimTrailingSlash(
transactionService,
)}/api/v1/safes/${safeAddress}/module-transactions/?transaction_hash=${txHash}`
const { results } = await fetch(url).then((res) => {
if (res.ok && res.status === 200) {
return res.json() as Promise<any>
} else {
throw new Error('Error fetching Safe module transactions')
}
})

if (results.length === 0) throw new Error('module transaction not found')

return results[0].moduleTransactionId as string
}

0 comments on commit dffcb98

Please sign in to comment.