Skip to content

Commit

Permalink
fix: add useWalletConnectSessions test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Oct 2, 2023
1 parent 242cf91 commit 7e14fbf
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions src/services/walletconnect/useWalletConnectSessions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { renderHook, waitFor } from '@/tests/test-utils'
import { WalletConnectContext } from '@/services/walletconnect/WalletConnectContext'
import useWalletConnectSessions from './useWalletConnectSessions'
import type WalletConnectWallet from './WalletConnectWallet'

describe('useWalletConnectSessions', () => {
it('should return an array of active sessions', () => {
const sessions = [
{
topic: 'topic1',
chainId: 1,
key: 'key1',
metadata: {
name: 'session1',
},
},
{
topic: 'topic2',
chainId: 1,
key: 'key2',
metadata: {
name: 'session2',
},
},
]

const mockWalletConnect = {
getActiveSessions: jest.fn().mockReturnValue(sessions),
onSessionAdd: jest.fn(),
onSessionDelete: jest.fn(),
} as unknown as WalletConnectWallet

const wrapper = ({ children }: any) => (
<WalletConnectContext.Provider value={{ walletConnect: mockWalletConnect, error: null }}>
{children}
</WalletConnectContext.Provider>
)

const { result } = renderHook(() => useWalletConnectSessions(), { wrapper })

expect(result.current).toEqual(sessions)
})

it('should update sessions when a session is added', async () => {
const mockWalletConnect = {
getActiveSessions: jest.fn().mockReturnValue([]),
onSessionAdd: jest.fn(),
onSessionDelete: jest.fn(),
} as unknown as WalletConnectWallet

const wrapper = ({ children }: any) => (
<WalletConnectContext.Provider value={{ walletConnect: mockWalletConnect, error: null }}>
{children}
</WalletConnectContext.Provider>
)

const { result } = renderHook(() => useWalletConnectSessions(), { wrapper })

expect(result.current).toEqual([])

const updateSessions = (mockWalletConnect.onSessionAdd as jest.Mock).mock.calls[0][0]

;(mockWalletConnect.getActiveSessions as jest.Mock).mockReturnValue([
{
topic: 'topic1',
chainId: 1,
key: 'key1',
metadata: {
name: 'session1',
},
},
])

updateSessions()

expect(mockWalletConnect.getActiveSessions).toHaveBeenCalled()

await waitFor(() => {
expect(result.current).toEqual([
{
topic: 'topic1',
chainId: 1,
key: 'key1',
metadata: {
name: 'session1',
},
},
])
})
})

it('should update sessions when a session is deleted', async () => {
const mockWalletConnect = {
getActiveSessions: jest.fn().mockReturnValue([]),
onSessionAdd: jest.fn(),
onSessionDelete: jest.fn(),
} as unknown as WalletConnectWallet

const wrapper = ({ children }: any) => (
<WalletConnectContext.Provider value={{ walletConnect: mockWalletConnect, error: null }}>
{children}
</WalletConnectContext.Provider>
)

const { result } = renderHook(() => useWalletConnectSessions(), { wrapper })

expect(result.current).toEqual([])

const updateSessions = (mockWalletConnect.onSessionDelete as jest.Mock).mock.calls[0][0]

;(mockWalletConnect.getActiveSessions as jest.Mock).mockReturnValue([
{
topic: 'topic1',
chainId: 1,
key: 'key1',
metadata: {
name: 'session1',
},
},
])

updateSessions()

expect(mockWalletConnect.getActiveSessions).toHaveBeenCalled()

await waitFor(() => {
expect(result.current).toEqual([
{
topic: 'topic1',
chainId: 1,
key: 'key1',
metadata: {
name: 'session1',
},
},
])
})
})
})

0 comments on commit 7e14fbf

Please sign in to comment.