-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: recovery loading + trigger (#2850)
* refactor: recovery loading + trigger * refactor: remove slice + move state to context * fix: rename context + create helper hook
- Loading branch information
Showing
51 changed files
with
1,135 additions
and
1,103 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
127 changes: 127 additions & 0 deletions
127
src/components/recovery/RecoveryContext/__tests__/index.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,127 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { useContext } from 'react' | ||
|
||
import { useCurrentChain, useHasFeature } from '@/hooks/useChains' | ||
import useSafeInfo from '@/hooks/useSafeInfo' | ||
import { useWeb3ReadOnly } from '@/hooks/wallets/web3' | ||
import { getDelayModifiers } from '@/services/recovery/delay-modifier' | ||
import { getRecoveryState } from '@/services/recovery/recovery-state' | ||
import { txDispatch, TxEvent } from '@/services/tx/txEvents' | ||
import { chainBuilder } from '@/tests/builders/chains' | ||
import { addressExBuilder, safeInfoBuilder } from '@/tests/builders/safe' | ||
import { act, fireEvent, render, waitFor } from '@/tests/test-utils' | ||
import { RecoveryContext, RecoveryProvider } from '..' | ||
import { getTxDetails } from '@/services/tx/txDetails' | ||
|
||
jest.mock('@/services/recovery/delay-modifier') | ||
jest.mock('@/services/recovery/recovery-state') | ||
|
||
const mockGetDelayModifiers = getDelayModifiers as jest.MockedFunction<typeof getDelayModifiers> | ||
const mockGetRecoveryState = getRecoveryState as jest.MockedFunction<typeof getRecoveryState> | ||
|
||
jest.mock('@/hooks/useSafeInfo') | ||
jest.mock('@/hooks/wallets/web3') | ||
jest.mock('@/hooks/useChains') | ||
jest.mock('@/services/tx/txDetails') | ||
|
||
const mockUseSafeInfo = useSafeInfo as jest.MockedFunction<typeof useSafeInfo> | ||
const mockUseWeb3ReadOnly = useWeb3ReadOnly as jest.MockedFunction<typeof useWeb3ReadOnly> | ||
const mockUseCurrentChain = useCurrentChain as jest.MockedFunction<typeof useCurrentChain> | ||
const mockUseHasFeature = useHasFeature as jest.MockedFunction<typeof useHasFeature> | ||
const mockGetTxDetails = getTxDetails as jest.MockedFunction<typeof getTxDetails> | ||
|
||
describe('RecoveryContext', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
|
||
// Clear memoization cache | ||
getTxDetails.cache.clear?.() | ||
}) | ||
|
||
it('should refetch manually calling it', async () => { | ||
mockUseHasFeature.mockReturnValue(true) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const chainId = '5' | ||
const safe = safeInfoBuilder() | ||
.with({ chainId, modules: [addressExBuilder().build()] }) | ||
.build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
const chain = chainBuilder().build() | ||
mockUseCurrentChain.mockReturnValue(chain) | ||
const delayModifiers = [{}] | ||
mockGetDelayModifiers.mockResolvedValue(delayModifiers as any) | ||
|
||
function Test() { | ||
const { refetch } = useContext(RecoveryContext) | ||
|
||
return <button onClick={refetch}>Refetch</button> | ||
} | ||
|
||
const { queryByText } = render( | ||
<RecoveryProvider> | ||
<Test /> | ||
</RecoveryProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(mockGetDelayModifiers).toHaveBeenCalledTimes(1) | ||
expect(mockGetRecoveryState).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
act(() => { | ||
fireEvent.click(queryByText('Refetch')!) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(mockGetRecoveryState).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
expect(mockGetDelayModifiers).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should refetch when interacting with a Delay Modifier', async () => { | ||
mockUseHasFeature.mockReturnValue(true) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const chainId = '5' | ||
const safe = safeInfoBuilder() | ||
.with({ chainId, modules: [addressExBuilder().build()] }) | ||
.build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
const chain = chainBuilder().build() | ||
mockUseCurrentChain.mockReturnValue(chain) | ||
const delayModifierAddress = faker.finance.ethereumAddress() | ||
mockGetDelayModifiers.mockResolvedValue([{ address: delayModifierAddress } as any]) | ||
mockGetTxDetails.mockResolvedValue({ txData: { to: { value: delayModifierAddress } } } as any) | ||
|
||
render( | ||
<RecoveryProvider> | ||
<></> | ||
</RecoveryProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(mockGetDelayModifiers).toHaveBeenCalledTimes(1) | ||
expect(mockGetRecoveryState).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
const txId = faker.string.alphanumeric() | ||
|
||
act(() => { | ||
txDispatch(TxEvent.PROCESSED, { | ||
txId, | ||
safeAddress: faker.finance.ethereumAddress(), | ||
}) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(mockGetTxDetails).toHaveBeenCalledTimes(1) | ||
expect(mockGetTxDetails).toHaveBeenNthCalledWith(1, txId, safe.chainId) | ||
|
||
expect(mockGetRecoveryState).toHaveBeenCalledTimes(2) | ||
}) | ||
}) | ||
}) |
136 changes: 136 additions & 0 deletions
136
src/components/recovery/RecoveryContext/__tests__/useDelayModifier.test.ts
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,136 @@ | ||
import { useHasFeature } from '@/hooks/useChains' | ||
import useSafeInfo from '@/hooks/useSafeInfo' | ||
import { useWeb3ReadOnly } from '@/hooks/wallets/web3' | ||
import { getSpendingLimitModuleAddress } from '@/services/contracts/spendingLimitContracts' | ||
import { getDelayModifiers } from '@/services/recovery/delay-modifier' | ||
import { addressExBuilder, safeInfoBuilder } from '@/tests/builders/safe' | ||
import { act, renderHook } from '@/tests/test-utils' | ||
import { useDelayModifiers } from '../useDelayModifiers' | ||
|
||
jest.mock('@/services/recovery/delay-modifier') | ||
|
||
const mockGetDelayModifiers = getDelayModifiers as jest.MockedFunction<typeof getDelayModifiers> | ||
|
||
jest.mock('@/hooks/useSafeInfo') | ||
jest.mock('@/hooks/wallets/web3') | ||
jest.mock('@/hooks/useChains') | ||
|
||
const mockUseSafeInfo = useSafeInfo as jest.MockedFunction<typeof useSafeInfo> | ||
const mockUseWeb3ReadOnly = useWeb3ReadOnly as jest.MockedFunction<typeof useWeb3ReadOnly> | ||
const mockUseHasFeature = useHasFeature as jest.MockedFunction<typeof useHasFeature> | ||
|
||
describe('useDelayModifiers', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should not fetch if the current chain does not support Delay Modifiers', async () => { | ||
jest.useFakeTimers() | ||
|
||
mockUseHasFeature.mockReturnValue(false) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const safe = safeInfoBuilder().build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
|
||
const { result } = renderHook(() => useDelayModifiers()) | ||
|
||
// Give enough time for loading to occur, if it will | ||
await act(async () => { | ||
jest.advanceTimersByTime(10) | ||
}) | ||
|
||
expect(result.current).toEqual([undefined, undefined, false]) | ||
expect(mockGetDelayModifiers).not.toHaveBeenCalledTimes(1) | ||
|
||
jest.useRealTimers() | ||
}) | ||
|
||
it('should not fetch is there is no provider', async () => { | ||
jest.useFakeTimers() | ||
|
||
mockUseHasFeature.mockReturnValue(true) | ||
mockUseWeb3ReadOnly.mockReturnValue(undefined) | ||
const safe = safeInfoBuilder().build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
|
||
const { result } = renderHook(() => useDelayModifiers()) | ||
|
||
// Give enough time for loading to occur, if it will | ||
await act(async () => { | ||
jest.advanceTimersByTime(10) | ||
}) | ||
|
||
expect(result.current).toEqual([undefined, undefined, false]) | ||
expect(mockGetDelayModifiers).not.toHaveBeenCalledTimes(1) | ||
|
||
jest.useRealTimers() | ||
}) | ||
|
||
it('should not fetch if there is no Safe modules enabled', async () => { | ||
jest.useFakeTimers() | ||
|
||
mockUseHasFeature.mockReturnValue(true) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const safe = safeInfoBuilder().with({ modules: [] }).build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
|
||
const { result } = renderHook(() => useDelayModifiers()) | ||
|
||
// Give enough time for loading to occur, if it will | ||
await act(async () => { | ||
jest.advanceTimersByTime(10) | ||
}) | ||
|
||
expect(result.current).toEqual([undefined, undefined, false]) | ||
expect(mockGetDelayModifiers).not.toHaveBeenCalledTimes(1) | ||
|
||
jest.useRealTimers() | ||
}) | ||
|
||
it('should not fetch if only the spending limit is enabled', async () => { | ||
jest.useFakeTimers() | ||
|
||
mockUseHasFeature.mockReturnValue(true) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const chainId = '5' | ||
const safe = safeInfoBuilder() | ||
.with({ chainId, modules: [{ value: getSpendingLimitModuleAddress(chainId)! }] }) | ||
.build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
|
||
const { result } = renderHook(() => useDelayModifiers()) | ||
|
||
// Give enough time for loading to occur, if it will | ||
await act(async () => { | ||
jest.advanceTimersByTime(10) | ||
}) | ||
|
||
expect(result.current).toEqual([undefined, undefined, false]) | ||
expect(mockGetDelayModifiers).not.toHaveBeenCalledTimes(1) | ||
|
||
jest.useRealTimers() | ||
}) | ||
|
||
it('should otherwise fetch', async () => { | ||
mockUseHasFeature.mockReturnValue(true) | ||
const provider = {} | ||
mockUseWeb3ReadOnly.mockReturnValue(provider as any) | ||
const chainId = '5' | ||
const safe = safeInfoBuilder() | ||
.with({ chainId, modules: [addressExBuilder().build()] }) | ||
.build() | ||
const safeInfo = { safe, safeAddress: safe.address.value } | ||
mockUseSafeInfo.mockReturnValue(safeInfo as any) | ||
|
||
renderHook(() => useDelayModifiers()) | ||
|
||
expect(mockGetDelayModifiers).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
Oops, something went wrong.