Skip to content

Commit

Permalink
Fix/transaction toast success (#1647)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-defi authored Nov 21, 2024
1 parent 81eb905 commit be6a22e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
40 changes: 40 additions & 0 deletions src/internal/components/Toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,46 @@ describe('Toast component', () => {
vi.useRealTimers();
});

it('should reset the timer when re-triggered while already visible', async () => {
vi.useFakeTimers();
const handleClose = vi.fn();
const durationMs = 5000;

const { rerender } = render(
<Toast
isVisible={true}
position="bottom-right"
onClose={handleClose}
durationMs={durationMs}
startTimeout={false}
>
<div>Test Child</div>
</Toast>,
);
vi.advanceTimersByTime(2000);
expect(handleClose).not.toHaveBeenCalled();

rerender(
<Toast
isVisible={true}
position="bottom-right"
onClose={handleClose}
durationMs={durationMs}
startTimeout={true}
>
<div>Updated Child</div>
</Toast>,
);

vi.advanceTimersByTime(4000);
expect(handleClose).not.toHaveBeenCalled();

vi.advanceTimersByTime(2000);
expect(handleClose).toHaveBeenCalled();

vi.useRealTimers();
});

it('should not fire timer after manual close', () => {
vi.useFakeTimers();
const handleClose = vi.fn();
Expand Down
28 changes: 16 additions & 12 deletions src/internal/components/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getToastPosition } from '../utils/getToastPosition';
type ToastProps = {
className?: string;
durationMs?: number;
startTimeout?: boolean;
position: 'top-center' | 'top-right' | 'bottom-center' | 'bottom-right';
animation?: 'animate-enterRight' | 'animate-enterUp' | 'animate-enterDown';
isVisible: boolean;
Expand All @@ -22,7 +23,8 @@ const defaultAnimationByPosition = {

export function Toast({
className,
durationMs = 3000,
durationMs = 5000,
startTimeout = true,
position = 'bottom-center',
animation,
isVisible,
Expand All @@ -33,18 +35,20 @@ export function Toast({
const animationClass = animation ?? defaultAnimationByPosition[position];

useEffect(() => {
const timer = setTimeout(() => {
if (isVisible) {
onClose();
}
}, durationMs);
if (startTimeout) {
const timer = setTimeout(() => {
if (isVisible) {
onClose();
}
}, durationMs);

return () => {
if (timer) {
clearTimeout(timer);
}
};
}, [durationMs, isVisible, onClose]);
return () => {
if (timer) {
clearTimeout(timer);
}
};
}
}, [durationMs, isVisible, onClose, startTimeout]);

if (!isVisible) {
return null;
Expand Down
41 changes: 41 additions & 0 deletions src/transaction/components/TransactionToast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('TransactionToast', () => {
(useTransactionContext as Mock).mockReturnValue({
isLoading: true,
isToastVisible: true,
lifecycleStatus: {
statusName: 'test-status',
},
});

render(
Expand All @@ -37,6 +40,9 @@ describe('TransactionToast', () => {
setIsToastVisible: vi.fn(),
transactionHash: '',
transactionId: '',
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>Test Message</TransactionToast>);
Expand All @@ -53,6 +59,9 @@ describe('TransactionToast', () => {
setIsToastVisible,
transactionHash: '0x123',
transactionId: '',
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>Test Message</TransactionToast>);
Expand All @@ -67,6 +76,9 @@ describe('TransactionToast', () => {
isToastVisible: true,
transactionHash: '',
errorMessage: '',
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>Transaction in progress</TransactionToast>);
Expand All @@ -81,6 +93,9 @@ describe('TransactionToast', () => {
isToastVisible: true,
transactionHash: mockTransactionHash,
errorMessage: '',
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>Transaction completed</TransactionToast>);
Expand All @@ -89,12 +104,17 @@ describe('TransactionToast', () => {
});

it('displays error message when present', () => {
const setIsToastVisible = vi.fn();
const mockErrorMessage = 'Transaction failed';
(useTransactionContext as Mock).mockReturnValue({
isLoading: false,
isToastVisible: true,
transactionHash: '',
errorMessage: mockErrorMessage,
setIsToastVisible,
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>Error occurred</TransactionToast>);
Expand All @@ -110,6 +130,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: null,
transactionId: '',
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast>In Progress</TransactionToast>);
Expand All @@ -125,6 +148,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: null,
transactionId: 'test-id',
lifecycleStatus: {
statusName: 'test-status',
},
});

const { container } = render(
Expand All @@ -143,6 +169,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: null,
transactionId: 'test-id',
lifecycleStatus: {
statusName: 'test-status',
},
});

const { container } = render(
Expand All @@ -161,6 +190,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: null,
transactionId: 'test-id',
lifecycleStatus: {
statusName: 'test-status',
},
});

const { container } = render(
Expand All @@ -179,6 +211,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: null,
transactionId: 'test-id',
lifecycleStatus: {
statusName: 'test-status',
},
});

const { container } = render(<TransactionToast>Test</TransactionToast>);
Expand All @@ -197,6 +232,9 @@ describe('TransactionToast', () => {
errorMessage: '',
receipt: {},
setIsToastVisible,
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast durationMs={2000}>Test</TransactionToast>);
Expand All @@ -216,6 +254,9 @@ describe('TransactionToast', () => {
errorMessage: 'Error',
receipt: null,
setIsToastVisible,
lifecycleStatus: {
statusName: 'test-status',
},
});

render(<TransactionToast durationMs={2000}>Test</TransactionToast>);
Expand Down
1 change: 1 addition & 0 deletions src/transaction/components/TransactionToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function TransactionToast({
durationMs={durationMs}
isVisible={isToastVisible}
onClose={closeToast}
startTimeout={!!receipt || !!errorMessage}
>
{children}
</Toast>
Expand Down

0 comments on commit be6a22e

Please sign in to comment.