Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent double custom toast counter increase when initial id is undefined #511

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class Observer {

custom = (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => {
const id = data?.id || toastsCounter++;
this.create({ jsx: jsx(id), id, ...data });
this.create({ jsx: jsx(id), ...data, id });
return id;
};
}
Expand Down
22 changes: 22 additions & 0 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ export default function Home({ searchParams }: any) {
>
Render Custom Toast
</button>
<button
data-testid="update-custom-toast"
className="button"
onClick={() => {
const toastId = toast.custom((t) => (
<div>
My custom toast - unupdated
</div>
), {
id: undefined
})
toast.custom((t) => (
<div>
My custom toast - updated
</div>
), {
id: toastId
})
}}
>
Update Custom Toast
</button>
<button
data-testid="custom-cancel-button-toast"
className="button"
Expand Down
6 changes: 6 additions & 0 deletions test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ test.describe('Basic functionality', () => {
await expect(page.getByText('My Updated Toast')).toHaveCount(1);
});

test('show correct toast content when updating custom toast', async ({ page }) => {
await page.getByTestId('update-custom-toast').click();
await expect(page.getByText('My custom toast - unupdated')).toHaveCount(0);
await expect(page.getByText('My custom toast - updated')).toHaveCount(1);
});

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