Skip to content

An opinionated toast component for React.

Bugfix: Update now works for first toastId (#156)

Eirik Mo175576e38b32
diff --git a/src/state.ts b/src/state.ts
index 15b58d7..d29fcd1 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -8,7 +8,7 @@ import {
ToastTypes,
} from './types';
-let toastsCounter = 0;
+let toastsCounter = 1;
class Observer {
subscribers: Array<(toast: ExternalToast | ToastToDismiss) => void>;
diff --git a/test/src/app/page.tsx b/test/src/app/page.tsx
index 9e9826b..73181ea 100644
--- a/test/src/app/page.tsx
+++ b/test/src/app/page.tsx
@@ -126,6 +126,21 @@ export default function Home({ searchParams }: any) {
>
Non-dismissible Toast
</button>
+ <button
+ data-testid="update-toast"
+ className="button"
+ onClick={() => {
+ const toastId = toast('My Unupdated Toast', {
+ duration: 10000,
+ });
+ toast('My Updated Toast', {
+ id: toastId,
+ duration: 10000,
+ });
+ }}
+ >
+ Updated Toast
+ </button>
{showAutoClose ? <div data-testid="auto-close-el" /> : null}
{showDismiss ? <div data-testid="dismiss-el" /> : null}
<Toaster position={searchParams.position || 'bottom-right'} theme={theme} dir={searchParams.dir || 'auto'} />
diff --git a/test/tests/basic.spec.ts b/test/tests/basic.spec.ts
index 688689a..de74409 100644
--- a/test/tests/basic.spec.ts
+++ b/test/tests/basic.spec.ts
@@ -1,4 +1,4 @@
-import { test, expect } from '@playwright/test';
+import { expect, test } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('/');
@@ -159,4 +159,10 @@ test.describe('Basic functionality', () => {
await page.getByTestId('default-button').click();
await expect(page.locator('[data-sonner-toaster]')).toHaveAttribute('dir', 'ltr');
});
+
+ test('show correct toast content when updating', async ({ page }) => {
+ await page.getByTestId('update-toast').click();
+ await expect(page.getByText('My Unupdated Toast')).toHaveCount(0);
+ await expect(page.getByText('My Updated Toast')).toHaveCount(1);
+ });
});