-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
RecoveryEvent.SUCCESS
notifications (#2944)
- Loading branch information
Showing
8 changed files
with
214 additions
and
15 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
126 changes: 126 additions & 0 deletions
126
src/components/recovery/RecoveryContext/__tests__/useRecoverySuccessEvents.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,126 @@ | ||
import { faker } from '@faker-js/faker' | ||
|
||
import { renderHook } from '@/tests/test-utils' | ||
import { useRecoverySuccessEvents } from '../useRecoverySuccessEvents' | ||
import { recoveryDispatch, RecoveryEvent, RecoveryTxType } from '@/services/recovery/recoveryEvents' | ||
|
||
jest.mock('@/services/recovery/recoveryEvents', () => ({ | ||
...jest.requireActual('@/services/recovery/recoveryEvents'), | ||
recoveryDispatch: jest.fn(), | ||
})) | ||
|
||
const mockRecoveryDispatch = recoveryDispatch as jest.MockedFunction<typeof recoveryDispatch> | ||
|
||
describe('useRecoverySuccessEvents', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should not dispatch SUCCESS event if recoveryState is not defined', () => { | ||
const pending = { | ||
[faker.string.hexadecimal()]: {}, | ||
} | ||
|
||
const { result } = renderHook(() => useRecoverySuccessEvents(pending as any)) | ||
|
||
expect(result.current).toBeUndefined() | ||
|
||
expect(mockRecoveryDispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not dispatch SUCCESS event if recoveryState is empty', () => { | ||
const pending = { | ||
[faker.string.hexadecimal()]: {}, | ||
} | ||
const recoveryState = [] as any[] | ||
|
||
const { result } = renderHook(() => useRecoverySuccessEvents(pending as any, recoveryState)) | ||
|
||
expect(result.current).toBeUndefined() | ||
|
||
expect(mockRecoveryDispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not dispatch SUCCESS event if pending is empty', () => { | ||
const pending = {} | ||
const recoveryState = [{ queue: [] }] | ||
|
||
const { result } = renderHook(() => useRecoverySuccessEvents(pending, recoveryState as any)) | ||
|
||
expect(result.current).toBeUndefined() | ||
|
||
expect(mockRecoveryDispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not dispatch SUCCESS event if pending is not PROCESSED', () => { | ||
const pending = { | ||
[faker.string.hexadecimal()]: { | ||
status: faker.helpers.arrayElement([RecoveryEvent.PROCESSING, RecoveryEvent.FAILED]), | ||
}, | ||
} | ||
const recoveryState = [{ queue: [] }] | ||
|
||
renderHook(() => useRecoverySuccessEvents(pending as any, recoveryState as any)) | ||
|
||
expect(mockRecoveryDispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should dispatch SUCCESS event if pending is PROCESSED and txType is PROPOSAL', () => { | ||
const pending = { | ||
[faker.string.hexadecimal()]: { | ||
status: RecoveryEvent.PROCESSED, | ||
txType: RecoveryTxType.PROPOSAL, | ||
}, | ||
} | ||
const recoveryState = [{ queue: [] }] | ||
|
||
renderHook(() => useRecoverySuccessEvents(pending as any, recoveryState as any)) | ||
|
||
expect(mockRecoveryDispatch).toHaveBeenCalledWith(RecoveryEvent.SUCCESS, { | ||
recoveryTxHash: expect.any(String), | ||
txType: RecoveryTxType.PROPOSAL, | ||
}) | ||
}) | ||
|
||
it('should not dispatch SUCCESS event if pending is PROCESSED and txType is not PROPOSAL and there is a queue', () => { | ||
const recoveryTxHash = faker.string.hexadecimal() | ||
const pending = { | ||
[recoveryTxHash]: { | ||
status: RecoveryEvent.PROCESSED, | ||
txType: faker.helpers.arrayElement([RecoveryTxType.EXECUTION, RecoveryTxType.SKIP_EXPIRED]), | ||
}, | ||
} | ||
const recoveryState = [ | ||
{ | ||
queue: [ | ||
{ | ||
args: { | ||
txHash: recoveryTxHash, | ||
}, | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
renderHook(() => useRecoverySuccessEvents(pending as any, recoveryState as any)) | ||
|
||
expect(mockRecoveryDispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should dispatch SUCCESS event if pending is PROCESSED and pending transaction is not queued', () => { | ||
const pending = { | ||
[faker.string.hexadecimal()]: { | ||
status: RecoveryEvent.PROCESSED, | ||
txType: RecoveryTxType.PROPOSAL, | ||
}, | ||
} | ||
const recoveryState = [{ queue: [] }] | ||
|
||
renderHook(() => useRecoverySuccessEvents(pending as any, recoveryState as any)) | ||
|
||
expect(mockRecoveryDispatch).toHaveBeenCalledWith(RecoveryEvent.SUCCESS, { | ||
recoveryTxHash: expect.any(String), | ||
txType: RecoveryTxType.PROPOSAL, | ||
}) | ||
}) | ||
}) |
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
43 changes: 43 additions & 0 deletions
43
src/components/recovery/RecoveryContext/useRecoverySuccessEvents.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,43 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { RecoveryEvent, RecoveryTxType, recoveryDispatch } from '@/services/recovery/recoveryEvents' | ||
import type { RecoveryState } from '@/services/recovery/recovery-state' | ||
import type { useRecoveryPendingTxs } from './useRecoveryPendingTxs' | ||
|
||
export function useRecoverySuccessEvents( | ||
pending: ReturnType<typeof useRecoveryPendingTxs>, | ||
recoveryState?: RecoveryState, | ||
): void { | ||
useEffect(() => { | ||
const pendingEntries = Object.entries(pending) | ||
|
||
if (!recoveryState || recoveryState.length === 0 || pendingEntries.length === 0) { | ||
return | ||
} | ||
|
||
pendingEntries.forEach(([recoveryTxHash, { txType, status }]) => { | ||
// Transaction successfully executed, waiting for recovery state to be loaded again | ||
if (status !== RecoveryEvent.PROCESSED) { | ||
return | ||
} | ||
|
||
const isQueued = recoveryState.some(({ queue }) => queue.some(({ args }) => args.txHash === recoveryTxHash)) | ||
|
||
if (isQueued) { | ||
// Only proposals should appear in the queue | ||
if (txType === RecoveryTxType.PROPOSAL) { | ||
recoveryDispatch(RecoveryEvent.SUCCESS, { | ||
recoveryTxHash, | ||
txType, | ||
}) | ||
} | ||
} else { | ||
// Executions/cancellations are removed from the queue | ||
recoveryDispatch(RecoveryEvent.SUCCESS, { | ||
recoveryTxHash, | ||
txType, | ||
}) | ||
} | ||
}) | ||
}, [pending, recoveryState]) | ||
} |
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