Skip to content

Commit

Permalink
fix(toast): filter dismissed toasts in region (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-rb authored Oct 7, 2024
1 parent 7789aa5 commit b1b6246
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/toast/toast-region.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ export function ToastRegion<T extends ValidComponent = "div">(
const toasts = createMemo(() =>
toastStore
.toasts()
.filter((toast) => toast.region === local.regionId)
.filter(
(toast) => toast.region === local.regionId && toast.dismiss === false,
)
.slice(0, local.limit!),
);

Expand Down
48 changes: 47 additions & 1 deletion packages/core/src/toast/toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("Toast", () => {
rootProps: Partial<Toast.ToastRootProps> = {},
options?: ShowToastOptions,
) => {
toaster.show(
return toaster.show(
(props) => (
<Toast.Root {...rootProps} toastId={props.toastId}>
<Toast.Title data-testid="title">Title</Toast.Title>
Expand Down Expand Up @@ -507,6 +507,52 @@ describe("Toast", () => {
expect(toasts.length).toBe(limit);
});

it("should not use dismissed toasts for list", async () => {
const limit = 1;

let closeId: number;

const { getAllByRole, getByTestId } = render(() => (
<>
<button
type="button"
data-testid="dismiss-toast"
onClick={() => {
toaster.dismiss(closeId);
}}
>
Close a toast
</button>
<button
type="button"
data-testid="trigger"
onClick={() => {
closeId = showToast();
}}
>
Show Toast
</button>
<Toast.Region limit={limit}>
<Toast.List data-testid="custom-region" />
</Toast.Region>
</>
));

fireEvent.click(getByTestId("trigger"));

fireEvent.click(getByTestId("dismiss-toast"));

let toasts =
getByTestId("custom-region").querySelectorAll('[role="status"]');
expect(toasts.length).toBe(0);

fireEvent.click(getByTestId("trigger"));

toasts = getByTestId("custom-region").querySelectorAll('[role="status"]');

expect(toasts.length).toBe(1);
});

it("should render multiple regions simultaneously", async () => {
const { getByTestId } = render(() => (
<>
Expand Down

0 comments on commit b1b6246

Please sign in to comment.