Skip to content

Commit

Permalink
chore: adding the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macci001 committed Dec 27, 2024
1 parent 26f05d7 commit b21d91a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
66 changes: 65 additions & 1 deletion packages/components/toast/__tests__/toast.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from "react";
import {render} from "@testing-library/react";
import {render, screen} from "@testing-library/react";
import userEvent, {UserEvent} from "@testing-library/user-event";

import {addToast, ToastProvider} from "../src";

const title = "Testing Title";
const description = "Testing Description";

describe("Toast", () => {
let user: UserEvent;

Expand Down Expand Up @@ -57,4 +60,65 @@ describe("Toast", () => {
await user.click(button);
expect(ref.current).not.toBeNull();
});

it("should display title and description when component is rendered", async () => {
const wrapper = render(
<>
<ToastProvider />
<button
data-testid="button"
onClick={() => {
addToast({
title: title,
description: description,
});
}}
>
Show Toast
</button>
</>,
);

const button = wrapper.getByTestId("button");

await user.click(button);

const region = screen.getByRole("region");

expect(region).toContainHTML(title);
expect(region).toContainHTML(description);
});

it("should close", async () => {
const wrapper = render(
<>
<ToastProvider />
<button
data-testid="button"
onClick={() => {
addToast({
title: title,
description: description,
});
}}
>
Show Toast
</button>
</>,
);

const button = wrapper.getByTestId("button");

await user.click(button);

const initialCloseButtons = wrapper.getAllByRole("button");
const initialButtonLength = initialCloseButtons.length;

await user.click(initialCloseButtons[initialButtonLength - 1]);

const finalCloseButtons = wrapper.getAllByRole("button");
const finalButtonLength = finalCloseButtons.length;

expect(initialButtonLength).toEqual(finalButtonLength + 1);
});
});
2 changes: 1 addition & 1 deletion packages/components/toast/src/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export function useToast<T extends ToastProps>(originalProps: UseToastProps<T>)
(props = {}) => ({
className: slots.closeButton({class: classNames?.closeButton}),
"aria-label": "close-button",
...mergeProps(props, otherProps, closeButtonProps, {onPress: props.onClose}),
...mergeProps(props, closeButtonProps, {onPress: originalProps.onClose}),
}),
[closeButtonProps],
);
Expand Down
2 changes: 1 addition & 1 deletion packages/components/toast/stories/toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,6 @@ export const WithEndContent = {
},
};

export const CustomTemplate = {
export const CustomStyles = {
render: CustomToastTemplate,
};

0 comments on commit b21d91a

Please sign in to comment.