-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:safe-global/safe-wallet-web into dev
- Loading branch information
Showing
23 changed files
with
779 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { render, waitFor } from '@/tests/test-utils' | ||
import CooldownButton from './index' | ||
|
||
describe('CooldownButton', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers() | ||
}) | ||
|
||
afterAll(() => { | ||
jest.useRealTimers() | ||
}) | ||
it('should be disabled initially if startDisabled is set and become enabled after <cooldown> seconds', async () => { | ||
const onClickEvent = jest.fn() | ||
const result = render( | ||
<CooldownButton cooldown={30} onClick={onClickEvent} startDisabled> | ||
Try again | ||
</CooldownButton>, | ||
) | ||
|
||
expect(result.getByRole('button')).toBeDisabled() | ||
expect(result.getByText('Try again in 30s')).toBeVisible() | ||
|
||
jest.advanceTimersByTime(10_000) | ||
|
||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeDisabled() | ||
expect(result.getByText('Try again in 20s')).toBeVisible() | ||
}) | ||
|
||
jest.advanceTimersByTime(5_000) | ||
|
||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeDisabled() | ||
expect(result.getByText('Try again in 15s')).toBeVisible() | ||
}) | ||
|
||
jest.advanceTimersByTime(15_000) | ||
|
||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeEnabled() | ||
}) | ||
result.getByRole('button').click() | ||
|
||
expect(onClickEvent).toHaveBeenCalledTimes(1) | ||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeDisabled() | ||
}) | ||
}) | ||
|
||
it('should be enabled initially if startDisabled is not set and become disabled after click', async () => { | ||
const onClickEvent = jest.fn() | ||
const result = render( | ||
<CooldownButton cooldown={30} onClick={onClickEvent}> | ||
Try again | ||
</CooldownButton>, | ||
) | ||
|
||
expect(result.getByRole('button')).toBeEnabled() | ||
result.getByRole('button').click() | ||
|
||
expect(onClickEvent).toHaveBeenCalledTimes(1) | ||
|
||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeDisabled() | ||
expect(result.getByText('Try again in 30s')).toBeVisible() | ||
}) | ||
|
||
jest.advanceTimersByTime(30_000) | ||
|
||
await waitFor(() => { | ||
expect(result.getByRole('button')).toBeEnabled() | ||
}) | ||
result.getByRole('button').click() | ||
|
||
expect(onClickEvent).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |
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,48 @@ | ||
import { Button } from '@mui/material' | ||
import { useState, useCallback, useEffect, type ReactNode } from 'react' | ||
|
||
// TODO: Extract into a hook so it can be reused for links and not just buttons | ||
const CooldownButton = ({ | ||
onClick, | ||
cooldown, | ||
startDisabled = false, | ||
children, | ||
}: { | ||
onClick: () => void | ||
startDisabled?: boolean | ||
cooldown: number // Cooldown in seconds | ||
children: ReactNode | ||
}) => { | ||
const [remainingSeconds, setRemainingSeconds] = useState(startDisabled ? cooldown : 0) | ||
const [lastSendTime, setLastSendTime] = useState(startDisabled ? Date.now() : 0) | ||
|
||
const adjustSeconds = useCallback(() => { | ||
const remainingCoolDownSeconds = Math.max(0, cooldown * 1000 - (Date.now() - lastSendTime)) / 1000 | ||
setRemainingSeconds(remainingCoolDownSeconds) | ||
}, [cooldown, lastSendTime]) | ||
|
||
useEffect(() => { | ||
// Counter for progress | ||
const interval = setInterval(adjustSeconds, 1000) | ||
return () => clearInterval(interval) | ||
}, [adjustSeconds]) | ||
|
||
const handleClick = () => { | ||
setLastSendTime(Date.now()) | ||
setRemainingSeconds(cooldown) | ||
onClick() | ||
} | ||
|
||
const isDisabled = remainingSeconds > 0 | ||
|
||
return ( | ||
<Button onClick={handleClick} variant="contained" size="small" disabled={isDisabled}> | ||
<span> | ||
{children} | ||
{remainingSeconds > 0 && ` in ${Math.floor(remainingSeconds)}s`} | ||
</span> | ||
</Button> | ||
) | ||
} | ||
|
||
export default CooldownButton |
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
Oops, something went wrong.