-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
422 additions
and
206 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
75 changes: 75 additions & 0 deletions
75
src/components/recovery/RecoveryContext/__tests__/useRecoveryPendingTxs.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,75 @@ | ||
import { RecoveryEvent, recoveryDispatch } from '@/services/recovery/recoveryEvents' | ||
import { PendingStatus } from '@/store/pendingTxsSlice' | ||
import { act, renderHook } from '@/tests/test-utils' | ||
import { faker } from '@faker-js/faker' | ||
import { useRecoveryPendingTxs } from '../useRecoveryPendingTxs' | ||
|
||
describe('useRecoveryPendingTxs', () => { | ||
it('should set pending status to SUBMITTING when EXECUTING event is emitted', () => { | ||
const delayModifierAddress = faker.finance.ethereumAddress() | ||
const transactionHash = faker.string.hexadecimal() | ||
const { result } = renderHook(() => useRecoveryPendingTxs()) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
|
||
act(() => { | ||
recoveryDispatch(RecoveryEvent.EXECUTING, { moduleAddress: delayModifierAddress, transactionHash }) | ||
}) | ||
|
||
expect(result.current).toStrictEqual({ | ||
[transactionHash]: PendingStatus.SUBMITTING, | ||
}) | ||
}) | ||
|
||
it('should set pending status to PROCESSING when PROCESSING event is emitted', () => { | ||
const delayModifierAddress = faker.finance.ethereumAddress() | ||
const transactionHash = faker.string.hexadecimal() | ||
const { result } = renderHook(() => useRecoveryPendingTxs()) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
|
||
act(() => { | ||
recoveryDispatch(RecoveryEvent.PROCESSING, { moduleAddress: delayModifierAddress, transactionHash }) | ||
}) | ||
|
||
expect(result.current).toStrictEqual({ | ||
[transactionHash]: PendingStatus.PROCESSING, | ||
}) | ||
}) | ||
|
||
it('should set remove the pending status when REVERTED event is emitted', () => { | ||
const delayModifierAddress = faker.finance.ethereumAddress() | ||
const transactionHash = faker.string.hexadecimal() | ||
const { result } = renderHook(() => useRecoveryPendingTxs()) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
|
||
act(() => { | ||
recoveryDispatch(RecoveryEvent.EXECUTING, { moduleAddress: delayModifierAddress, transactionHash }) | ||
recoveryDispatch(RecoveryEvent.REVERTED, { | ||
moduleAddress: delayModifierAddress, | ||
transactionHash, | ||
error: new Error(), | ||
}) | ||
}) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
}) | ||
|
||
it('should set remove the pending status when PROCESSED event is emitted', () => { | ||
const delayModifierAddress = faker.finance.ethereumAddress() | ||
const transactionHash = faker.string.hexadecimal() | ||
const { result } = renderHook(() => useRecoveryPendingTxs()) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
|
||
act(() => { | ||
recoveryDispatch(RecoveryEvent.EXECUTING, { moduleAddress: delayModifierAddress, transactionHash }) | ||
recoveryDispatch(RecoveryEvent.PROCESSED, { moduleAddress: delayModifierAddress, transactionHash }) | ||
}) | ||
|
||
expect(result.current).toStrictEqual({}) | ||
}) | ||
|
||
// No need to test RecoveryEvent.FAILED as pending status is not set before it is dispatched | ||
}) |
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
44 changes: 44 additions & 0 deletions
44
src/components/recovery/RecoveryContext/useRecoveryPendingTxs.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,44 @@ | ||
import { RecoveryEvent, recoverySubscribe } from '@/services/recovery/recoveryEvents' | ||
import { PendingStatus } from '@/store/pendingTxsSlice' | ||
import { useEffect, useState } from 'react' | ||
|
||
type PendingRecoveryTransactions = { [recoveryTxHash: string]: PendingStatus } | ||
|
||
const pendingStatuses: { [key in RecoveryEvent]: PendingStatus | null } = { | ||
[RecoveryEvent.EXECUTING]: PendingStatus.SUBMITTING, | ||
[RecoveryEvent.PROCESSING]: PendingStatus.PROCESSING, | ||
[RecoveryEvent.REVERTED]: null, | ||
[RecoveryEvent.PROCESSED]: null, | ||
[RecoveryEvent.FAILED]: null, | ||
} | ||
|
||
export function useRecoveryPendingTxs(): PendingRecoveryTransactions { | ||
const [pending, setPending] = useState<PendingRecoveryTransactions>({}) | ||
|
||
useEffect(() => { | ||
const unsubFns = Object.entries(pendingStatuses).map(([event, status]) => | ||
recoverySubscribe(event as RecoveryEvent, (detail) => { | ||
const recoveryTxHash = 'recoveryTxHash' in detail && detail.recoveryTxHash | ||
|
||
if (!recoveryTxHash) { | ||
return | ||
} | ||
|
||
setPending((prev) => { | ||
if (status === null) { | ||
const { [recoveryTxHash]: _, ...rest } = prev | ||
return rest | ||
} | ||
|
||
return { ...prev, [recoveryTxHash]: status } | ||
}) | ||
}), | ||
) | ||
|
||
return () => { | ||
unsubFns.forEach((unsub) => unsub()) | ||
} | ||
}, []) | ||
|
||
return pending | ||
} |
Oops, something went wrong.