Skip to content

Commit 9ab9b9a

Browse files
authored
fix: failing tests (#572)
* Fix tests * fix tests
1 parent 64e7b83 commit 9ab9b9a

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ const Toast = (props: ToastProps) => {
341341

342342
if (Math.abs(swipeAmount) >= SWIPE_THRESHOLD || velocity > 0.11) {
343343
setOffsetBeforeRemove(offset.current);
344+
344345
toast.onDismiss?.(toast);
345346

346347
if (swipeDirection === 'x') {

test/src/app/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ export default function Home({ searchParams }: any) {
139139
className="button"
140140
onClick={() =>
141141
toast('My Toast', {
142-
onDismiss: () => setShowDismiss(true),
142+
onDismiss: () => {
143+
setShowDismiss(true);
144+
},
143145
})
144146
}
145147
>
146-
Render Toast With onAutoClose callback
148+
Dismiss toast callback
147149
</button>
148150
<button
149151
data-testid="non-dismissible-toast"
@@ -212,6 +214,7 @@ export default function Home({ searchParams }: any) {
212214
{showAutoClose ? <div data-testid="auto-close-el" /> : null}
213215
{showDismiss ? <div data-testid="dismiss-el" /> : null}
214216
<Toaster
217+
offset={32}
215218
position={searchParams.position || 'bottom-right'}
216219
toastOptions={{
217220
actionButtonStyle: { backgroundColor: 'rgb(219, 239, 255)' },

test/tests/basic.spec.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,38 @@ test.describe('Basic functionality', () => {
8181

8282
test('toast is not removed when hovered', async ({ page }) => {
8383
await page.getByTestId('default-button').click();
84+
85+
// Wait for toast to be visible first
86+
await expect(page.locator('[data-sonner-toast]')).toBeVisible();
87+
88+
// Hover the toast
8489
await page.hover('[data-sonner-toast]');
85-
const timeout = new Promise((resolve) => setTimeout(resolve, 5000));
86-
await timeout;
90+
91+
// Wait a bit to ensure hover is registered
92+
await page.waitForTimeout(100);
93+
94+
// Create a longer timeout to verify toast persists
95+
await page.waitForTimeout(5000);
96+
97+
// Verify toast is still visible
98+
await expect(page.locator('[data-sonner-toast]')).toBeVisible();
8799
await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);
88100
});
89101

90102
test('toast is not removed if duration is set to infinity', async ({ page }) => {
91103
await page.getByTestId('infinity-toast').click();
92-
await page.hover('[data-sonner-toast]');
93-
const timeout = new Promise((resolve) => setTimeout(resolve, 5000));
94-
await timeout;
95-
await expect(page.locator('[data-sonner-toast]')).toHaveCount(1);
104+
105+
await expect(page.locator('[data-sonner-toast]')).toBeVisible();
106+
107+
const toast = page.locator('[data-sonner-toast]');
108+
await toast.hover({ force: true });
109+
110+
await page.waitForTimeout(100);
111+
112+
await page.waitForTimeout(5000);
113+
114+
await expect(toast).toBeVisible();
115+
await expect(toast).toHaveCount(1);
96116
});
97117

98118
test('toast is not removed when event prevented in action', async ({ page }) => {

0 commit comments

Comments
 (0)