Skip to content

Commit

Permalink
fix: inability to update toast duration dynamically emilkowalski#529
Browse files Browse the repository at this point in the history
Inability to update toast duration dynamically

closed emilkowalski#529
  • Loading branch information
hqw567 committed Dec 27, 2024
1 parent edc615f commit b14d69a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const Toast = (props: ToastProps) => {

offset.current = React.useMemo(() => heightIndex * gap + toastsHeightBefore, [heightIndex, toastsHeightBefore]);

React.useEffect(() => {
remainingTime.current = duration;
}, [duration]);

React.useEffect(() => {
// Trigger enter animation without using CSS animation
setMounted(true);
Expand Down
17 changes: 17 additions & 0 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ export default function Home({ searchParams }: any) {
>
Updated Toast
</button>
<button
data-testid="update-toast-duration"
className="button"
onClick={() => {
const toastId = toast('My Unupdated Toast, Updated After 3 Seconds', {
duration: 10000,
});
setTimeout(() => {
toast('My Updated Toast, Close After 1 Second', {
id: toastId,
duration: 1000,
});
}, 3000);
}}
>
Updated Toast Duration
</button>
<button
data-testid="string-description"
className="button"
Expand Down
16 changes: 16 additions & 0 deletions test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ test.describe('Basic functionality', () => {
await expect(page.getByText('My Updated Toast')).toHaveCount(1);
});

test('should update toast content and duration after 3 seconds', async ({ page }) => {
await page.getByTestId('update-toast-duration').click();

const initialToast = page.getByText('My Unupdated Toast, Updated After 3 Seconds');
await expect(initialToast).toBeVisible();

await page.waitForTimeout(3000);
const updatedToast = page.getByText('My Updated Toast, Close After 1 Second');
await expect(updatedToast).toBeVisible();

await expect(initialToast).not.toBeVisible();

await page.waitForTimeout(1200);
await expect(updatedToast).not.toBeVisible();
});

test('cancel button is rendered with custom styles', async ({ page }) => {
await page.getByTestId('custom-cancel-button-toast').click();
const button = await page.locator('[data-cancel]');
Expand Down

0 comments on commit b14d69a

Please sign in to comment.