|
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"; |
4 | 4 |
|
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"; |
7 | 10 |
|
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 () => { |
10 | 14 | render(
|
11 | 15 | <>
|
12 |
| - <Toaster position="bottom-right" /> |
13 |
| - <ToastTestComponent /> |
| 16 | + <Toaster position="bottom-center" theme="light" /> |
| 17 | + <ToastVariantComponent /> |
14 | 18 | </>,
|
15 | 19 | );
|
16 |
| - |
17 | 20 | // Click the button to show the toast:
|
18 |
| - const button = screen.getByText('Show Toast'); |
| 21 | + const button = screen.getByText("Show Toast"); |
19 | 22 | fireEvent.click(button);
|
20 |
| - |
21 | 23 | await waitFor(() => {
|
22 |
| - expect(screen.getByText('Hello Toast!')).toBeInTheDocument(); |
| 24 | + expect(screen.getByText("Hello Toast!")).toBeInTheDocument(); |
23 | 25 | });
|
24 |
| - |
25 | 26 | await waitFor(() => {
|
26 |
| - expect(screen.getByText('This is a success toast')).toBeInTheDocument(); |
| 27 | + expect(screen.getByText("This is a success toast")).toBeInTheDocument(); |
27 | 28 | });
|
28 |
| - |
29 | 29 | // Press Close Button to remove the toast:
|
30 |
| - const closeButton = screen.getByRole('button', { name: 'Close' }); |
| 30 | + const closeButton = screen.getByRole("button", { name: "Close" }); |
31 | 31 | 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 | + ); |
32 | 52 |
|
| 53 | + const button = screen.getByText("Show Toast"); |
| 54 | + fireEvent.click(button); |
| 55 | + |
| 56 | + // Verify toast content is displayed |
33 | 57 | 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(); |
35 | 60 | });
|
36 | 61 |
|
| 62 | + // Click the action button |
| 63 | + const actionButton = screen.getByText("Action"); |
| 64 | + fireEvent.click(actionButton); |
| 65 | + |
| 66 | + // Verify console.log was called |
37 | 67 | 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(); |
38 | 78 | expect(
|
39 |
| - screen.queryByText('This is a success toast'), |
| 79 | + screen.queryByText("This is a success toast"), |
40 | 80 | ).not.toBeInTheDocument();
|
41 | 81 | });
|
| 82 | + |
| 83 | + // Restore the original console.log |
| 84 | + consoleSpy.mockRestore(); |
42 | 85 | });
|
43 | 86 | });
|
0 commit comments