Skip to content

Commit 5579964

Browse files
committed
fix: toast tests - variant & action
1 parent c268d82 commit 5579964

File tree

2 files changed

+88
-24
lines changed

2 files changed

+88
-24
lines changed
+26-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { toast } from '../main';
1+
import { toast } from "../main";
22

3-
const ToastTestComponent = () => {
3+
const ToastVariantComponent = () => {
44
return (
55
<button
66
onClick={() =>
77
toast.success({
8-
text: 'Hello Toast!',
9-
description: 'This is a success toast',
8+
text: "Hello Toast!",
9+
description: "This is a success toast",
1010
})
1111
}
1212
>
@@ -15,4 +15,25 @@ const ToastTestComponent = () => {
1515
);
1616
};
1717

18-
export default ToastTestComponent;
18+
const ToastActionsComponent = () => {
19+
return (
20+
<button
21+
onClick={() =>
22+
toast.success({
23+
text: "Hello Toast!",
24+
description: "This is a success toast",
25+
action: {
26+
content: "Action",
27+
onClick: () => {
28+
console.log("Action clicked");
29+
},
30+
},
31+
})
32+
}
33+
>
34+
Show Toast
35+
</button>
36+
);
37+
};
38+
39+
export { ToastVariantComponent, ToastActionsComponent };

library/src/test/toast.test.tsx

+62-19
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,86 @@
1-
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
2-
import '@testing-library/jest-dom';
3-
import { describe, test, expect } from 'vitest';
1+
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
2+
import "@testing-library/jest-dom";
3+
import { describe, test, expect, vi } from "vitest";
44

5-
import { Toaster } from '../main';
6-
import ToastTestComponent from './toast-test-component';
5+
import { Toaster } from "../main";
6+
import {
7+
ToastVariantComponent,
8+
ToastActionsComponent,
9+
} from "./toast-test-component";
710

8-
describe('Toast notifications', () => {
9-
test('should display a success toast', async () => {
11+
describe("🚀 Toast notifications", () => {
12+
/** 📦 Display Single toast with variant: */
13+
test("should display a success toast", async () => {
1014
render(
1115
<>
12-
<Toaster position="bottom-right" />
13-
<ToastTestComponent />
16+
<Toaster position="bottom-center" theme="light" />
17+
<ToastVariantComponent />
1418
</>,
1519
);
16-
1720
// Click the button to show the toast:
18-
const button = screen.getByText('Show Toast');
21+
const button = screen.getByText("Show Toast");
1922
fireEvent.click(button);
20-
2123
await waitFor(() => {
22-
expect(screen.getByText('Hello Toast!')).toBeInTheDocument();
24+
expect(screen.getByText("Hello Toast!")).toBeInTheDocument();
2325
});
24-
2526
await waitFor(() => {
26-
expect(screen.getByText('This is a success toast')).toBeInTheDocument();
27+
expect(screen.getByText("This is a success toast")).toBeInTheDocument();
2728
});
28-
2929
// Press Close Button to remove the toast:
30-
const closeButton = screen.getByRole('button', { name: 'Close' });
30+
const closeButton = screen.getByRole("button", { name: "Close" });
3131
fireEvent.click(closeButton);
32+
await waitFor(() => {
33+
expect(screen.queryByText("Hello Toast!")).not.toBeInTheDocument();
34+
});
35+
await waitFor(() => {
36+
expect(
37+
screen.queryByText("This is a success toast"),
38+
).not.toBeInTheDocument();
39+
});
40+
});
41+
42+
/** 📦 Display Toast with Actions: */
43+
test("should display toast with action and handle interactions", async () => {
44+
const consoleSpy = vi.spyOn(console, "log");
45+
46+
render(
47+
<>
48+
<Toaster position="bottom-right" theme="dark" />
49+
<ToastActionsComponent />
50+
</>,
51+
);
3252

53+
const button = screen.getByText("Show Toast");
54+
fireEvent.click(button);
55+
56+
// Verify toast content is displayed
3357
await waitFor(() => {
34-
expect(screen.queryByText('Hello Toast!')).not.toBeInTheDocument();
58+
expect(screen.getByText("Hello Toast!")).toBeInTheDocument();
59+
expect(screen.getByText("This is a success toast")).toBeInTheDocument();
3560
});
3661

62+
// Click the action button
63+
const actionButton = screen.getByText("Action");
64+
fireEvent.click(actionButton);
65+
66+
// Verify console.log was called
3767
await waitFor(() => {
68+
expect(consoleSpy).toHaveBeenCalledWith("Action clicked");
69+
});
70+
71+
// Press Close Button to remove the toast
72+
const closeButton = screen.getByRole("button", { name: "Close" });
73+
fireEvent.click(closeButton);
74+
75+
// Verify toast is removed
76+
await waitFor(() => {
77+
expect(screen.queryByText("Hello Toast!")).not.toBeInTheDocument();
3878
expect(
39-
screen.queryByText('This is a success toast'),
79+
screen.queryByText("This is a success toast"),
4080
).not.toBeInTheDocument();
4181
});
82+
83+
// Restore the original console.log
84+
consoleSpy.mockRestore();
4285
});
4386
});

0 commit comments

Comments
 (0)