Skip to content
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
11 changes: 7 additions & 4 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ class Observer {
) => {
const { message, ...rest } = data;
const id = typeof data?.id === 'number' || data.id?.length > 0 ? data.id : toastsCounter++;
const alreadyExists = this.toasts.find((toast) => {
return toast.id === id;
const existingToast = this.toasts.find((toast) => {
return toast.id === id || toastHasSameText(toast, message);
});

const dismissible = data.dismissible === undefined ? true : data.dismissible;

if (this.dismissedToasts.has(id)) {
this.dismissedToasts.delete(id);
}

if (alreadyExists) {
if (existingToast) {
this.toasts = this.toasts.map((toast) => {
if (toast.id === id) {
if (toast.id === existingToast.id) {
this.publish({ ...toast, ...data, id, title: message });
return {
...toast,
Expand Down Expand Up @@ -280,6 +281,8 @@ const basicToast = toastFunction;

const getHistory = () => ToastState.toasts;
const getToasts = () => ToastState.getActiveToasts();
const toastHasSameText = (toast: ToastT | ToastToDismiss, message: titleT) =>
typeof message === 'string' && 'title' in toast && typeof toast.title === 'string' && message === toast.title;

// We use `Object.assign` to maintain the correct types as we would lose them otherwise
export const toast = Object.assign(
Expand Down
10 changes: 8 additions & 2 deletions test/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ test.describe('Basic functionality', () => {
await expect(page.locator('[data-button]')).toHaveCount(1);
});

test('does not render duplicate toasts with the same text', async ({ page }) => {
await page.getByTestId('success').click();
await page.getByTestId('success').click();
await expect(page.getByText('My Success Toast', { exact: true })).toHaveCount(1);
});

test('show correct toast content based on promise state', async ({ page }) => {
await page.getByTestId('promise').click();
await expect(page.getByText('Loading...')).toHaveCount(1);
Expand Down Expand Up @@ -265,7 +271,7 @@ test.describe('Basic functionality', () => {

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]');
const button = page.locator('[data-cancel]');

await expect(button).toHaveCSS('background-color', 'rgb(254, 226, 226)');
});
Expand All @@ -280,7 +286,7 @@ test.describe('Basic functionality', () => {

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

await expect(button).toHaveCSS('background-color', 'rgb(219, 239, 255)');
});
Expand Down