-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add
useWalletConnectSessions
test coverage
- Loading branch information
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/services/walletconnect/useWalletConnectSessions.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
]) | ||
}) | ||
}) | ||
}) |