Skip to content

Commit

Permalink
Revert "fix: add onRequest tests + move tests"
Browse files Browse the repository at this point in the history
This reverts commit 0184e78.
  • Loading branch information
iamacook committed Oct 4, 2023
1 parent 0184e78 commit 552ab2b
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 482 deletions.
220 changes: 220 additions & 0 deletions src/services/walletconnect/WalletConnectContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { hexZeroPad } from 'ethers/lib/utils'
import { useContext } from 'react'
import type { SafeInfo } from '@safe-global/safe-gateway-typescript-sdk'

import { fireEvent, render, waitFor } from '@/tests/test-utils'
import { WalletConnectContext, WalletConnectProvider } from './WalletConnectContext'
import WalletConnectWallet from './WalletConnectWallet'
import { safeInfoSlice } from '@/store/safeInfoSlice'
import { useAppDispatch } from '@/store'

jest.mock('./WalletConnectWallet')

const TestComponent = () => {
const { walletConnect, error } = useContext(WalletConnectContext)
return (
<>
{walletConnect && <p>WalletConnect initialized</p>}
{error && <p>{error.message}</p>}
</>
)
}

describe('WalletConnectProvider', () => {
it('sets the walletConnect state', async () => {
jest.spyOn(WalletConnectWallet, 'init').mockImplementation(() => Promise.resolve())
jest.spyOn(WalletConnectWallet, 'updateSessions').mockImplementation(() => Promise.resolve())

const { getByText } = render(
<WalletConnectProvider>
<TestComponent />
</WalletConnectProvider>,
{
initialReduxState: {
safeInfo: {
loading: false,
data: {
address: {
value: hexZeroPad('0x123', 20),
},
chainId: '5',
} as SafeInfo,
},
},
},
)

await waitFor(() => {
expect(getByText('WalletConnect initialized')).toBeInTheDocument()
})
})

it('sets the error state', async () => {
jest.spyOn(WalletConnectWallet, 'init').mockImplementation(() => Promise.reject(new Error('Test init failed')))
jest.spyOn(WalletConnectWallet, 'updateSessions').mockImplementation(() => Promise.resolve())

const { getByText } = render(
<WalletConnectProvider>
<TestComponent />
</WalletConnectProvider>,
{
initialReduxState: {
safeInfo: {
loading: false,
data: {
address: {
value: hexZeroPad('0x123', 20),
},
chainId: '5',
} as SafeInfo,
},
},
},
)

await waitFor(() => {
expect(getByText('Test init failed')).toBeInTheDocument()
})
})

describe('updateSessions', () => {
const getUpdateSafeInfoComponent = (safeInfo: SafeInfo) => {
// eslint-disable-next-line react/display-name
return () => {
const dispatch = useAppDispatch()
const updateSafeInfo = () => {
dispatch(
safeInfoSlice.actions.set({
loading: false,
data: safeInfo,
}),
)
}

return <button onClick={() => updateSafeInfo()}>update</button>
}
}

it('updates sessions when the chainId changes', async () => {
jest.spyOn(WalletConnectWallet, 'init').mockImplementation(() => Promise.resolve())
jest.spyOn(WalletConnectWallet, 'updateSessions').mockImplementation(() => Promise.resolve())

const ChainUpdater = getUpdateSafeInfoComponent({
address: { value: hexZeroPad('0x123', 20) },
chainId: '1',
} as SafeInfo)

const { getByText } = render(
<WalletConnectProvider>
<TestComponent />
<ChainUpdater />
</WalletConnectProvider>,
{
initialReduxState: {
safeInfo: {
loading: false,
data: {
address: {
value: hexZeroPad('0x123', 20),
},
chainId: '5',
} as SafeInfo,
},
},
},
)

await waitFor(() => {
expect(getByText('WalletConnect initialized')).toBeInTheDocument()
expect(WalletConnectWallet.updateSessions).toHaveBeenCalledWith('5', hexZeroPad('0x123', 20))
})

fireEvent.click(getByText('update'))

await waitFor(() => {
expect(WalletConnectWallet.updateSessions).toHaveBeenCalledWith('1', hexZeroPad('0x123', 20))
})
})

it('updates sessions when the safeAddress changes', async () => {
jest.spyOn(WalletConnectWallet, 'init').mockImplementation(() => Promise.resolve())
jest.spyOn(WalletConnectWallet, 'updateSessions').mockImplementation(() => Promise.resolve())

const AddressUpdater = getUpdateSafeInfoComponent({
address: { value: hexZeroPad('0x456', 20) },
chainId: '5',
} as SafeInfo)

const { getByText } = render(
<WalletConnectProvider>
<TestComponent />
<AddressUpdater />
</WalletConnectProvider>,
{
initialReduxState: {
safeInfo: {
loading: false,
data: {
address: {
value: hexZeroPad('0x123', 20),
},
chainId: '5',
} as SafeInfo,
},
},
},
)

await waitFor(() => {
expect(getByText('WalletConnect initialized')).toBeInTheDocument()
expect(WalletConnectWallet.updateSessions).toHaveBeenCalledWith('5', hexZeroPad('0x123', 20))
})

fireEvent.click(getByText('update'))

await waitFor(() => {
expect(WalletConnectWallet.updateSessions).toHaveBeenCalledWith('5', hexZeroPad('0x456', 20))
})
})

it('sets the error state', async () => {
jest.spyOn(WalletConnectWallet, 'init').mockImplementation(() => Promise.resolve())
jest
.spyOn(WalletConnectWallet, 'updateSessions')
.mockImplementation(() => Promise.reject(new Error('Test updateSessions failed')))

const { getByText } = render(
<WalletConnectProvider>
<TestComponent />
</WalletConnectProvider>,
{
initialReduxState: {
safeInfo: {
loading: false,
data: {
address: {
value: hexZeroPad('0x123', 20),
},
chainId: '5',
} as SafeInfo,
},
},
},
)

await waitFor(() => {
expect(getByText('Test updateSessions failed')).toBeInTheDocument()
})
})
})

describe('onRequest', () => {
it.todo('does not continue with the request if there is no matching topic')

it.todo('does not continue with the request if there is no matching chainId')

it.todo('passes the request onto the Safe Wallet Provider and sends the response to WalletConnect')

it.todo('sets the error state if there is an error requesting the response from the Safe Wallet Provider')
})
})
2 changes: 0 additions & 2 deletions src/services/walletconnect/WalletConnectContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export const WalletConnectProvider = ({ children }: { children: ReactNode }) =>
iconUrl: session.peer.metadata.icons[0],
})

console.log('============', response)

// Send response to WalletConnect
await walletConnect.sendSessionResponse(topic, response)
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hexZeroPad } from 'ethers/lib/utils'
import type { ProposalTypes, SessionTypes, SignClientTypes, Verify } from '@walletconnect/types'
import type { IWeb3Wallet, Web3WalletTypes } from '@walletconnect/web3wallet'

import type WalletConnectWallet from '../WalletConnectWallet'
import type WalletConnectWallet from './WalletConnectWallet'

jest.mock('@walletconnect/core', () => ({
Core: jest.fn(),
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('WalletConnectWallet', () => {

beforeEach(async () => {
// Reset import
wallet = (await import('../WalletConnectWallet')).default
wallet = (await import('./WalletConnectWallet')).default

await wallet.init()
})
Expand Down
Loading

0 comments on commit 552ab2b

Please sign in to comment.