diff --git a/src/components/new-safe/create/steps/StatusStep/__tests__/usePendingSafe.test.ts b/src/components/new-safe/create/steps/StatusStep/__tests__/usePendingSafe.test.ts new file mode 100644 index 0000000000..145df2ebe5 --- /dev/null +++ b/src/components/new-safe/create/steps/StatusStep/__tests__/usePendingSafe.test.ts @@ -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) + }) +}) diff --git a/src/components/new-safe/create/steps/StatusStep/usePendingSafe.ts b/src/components/new-safe/create/steps/StatusStep/usePendingSafe.ts index fbb53a0813..08c3ac543e 100644 --- a/src/components/new-safe/create/steps/StatusStep/usePendingSafe.ts +++ b/src/components/new-safe/create/steps/StatusStep/usePendingSafe.ts @@ -9,8 +9,8 @@ export const usePendingSafe = (): [PendingSafeData | undefined, (safe: PendingSa const [pendingSafes, setPendingSafes] = useLocalStorage(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) {