Skip to content

Commit

Permalink
test: unittest for usePendingSafe hook
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Aug 3, 2023
1 parent 4347d53 commit 7912876
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { renderHook } from '@/tests/test-utils'
import { usePendingSafe } from '../usePendingSafe'

import { hexZeroPad } from 'ethers/lib/utils'
import { useCurrentChain } from '@/hooks/useChains'

// mock useCurrentChain
jest.mock('@/hooks/useChains', () => ({
useCurrentChain: jest.fn(() => ({
shortName: 'gor',
chainId: '5',
chainName: 'Goerli',
features: [],
})),
}))

describe('usePendingSafe()', () => {
const mockPendingSafe1 = {
name: 'joyful-rinkeby-safe',
threshold: 1,
owners: [],
saltNonce: 123,
address: hexZeroPad('0x10', 20),
}
const mockPendingSafe2 = {
name: 'joyful-rinkeby-safe',
threshold: 1,
owners: [],
saltNonce: 123,
address: hexZeroPad('0x10', 20),
}

beforeEach(() => {
window.localStorage.clear()
})
it('Should initially be undefined', () => {
const { result } = renderHook(() => usePendingSafe())
expect(result.current[0]).toBeUndefined()
})

it('Should set the pendingSafe per ChainId', async () => {
const { result, rerender } = renderHook(() => usePendingSafe())

result.current[1](mockPendingSafe1)

rerender()

expect(result.current[0]).toEqual(mockPendingSafe1)
;(useCurrentChain as jest.Mock).mockImplementation(() => ({
shortName: 'eth',
chainId: '1',
chainName: 'Ethereum',
features: [],
}))

rerender()
expect(result.current[0]).toEqual(undefined)

result.current[1](mockPendingSafe2)
rerender()
expect(result.current[0]).toEqual(mockPendingSafe2)
;(useCurrentChain as jest.Mock).mockImplementation(() => ({
shortName: 'gor',
chainId: '5',
chainName: 'Goerli',
features: [],
}))
rerender()
expect(result.current[0]).toEqual(mockPendingSafe1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const usePendingSafe = (): [PendingSafeData | undefined, (safe: PendingSa
const [pendingSafes, setPendingSafes] = useLocalStorage<PendingSafeByChain>(SAFE_PENDING_CREATION_STORAGE_KEY)

const chainInfo = useCurrentChain()
const pendingSafe = chainInfo && pendingSafes?.[chainInfo.chainId]

const pendingSafe = chainInfo && pendingSafes?.[chainInfo.chainId]
const setPendingSafe = useCallback(
(safe: PendingSafeData | undefined) => {
if (!chainInfo?.chainId) {
Expand Down

0 comments on commit 7912876

Please sign in to comment.