forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationItem.test.tsx
More file actions
62 lines (54 loc) · 2.16 KB
/
Copy pathNotificationItem.test.tsx
File metadata and controls
62 lines (54 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { ReactElement } from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import { MemoryRouter } from "react-router";
import { describe, it, expect, beforeEach } from "vitest";
import { NotificationItem } from "./NotificationItem";
import { useNotificationsStore } from "../../store/notifications";
import { resetNotificationsForTesting } from "../../test-utils/notifications";
import type { Notification } from "../../types/notifications";
const wrap = (ui: ReactElement) =>
render(<MemoryRouter>{ui}</MemoryRouter>);
const mockNotification: Notification = {
id: "test-1",
type: "payment_received",
title: "Payment received",
message: "You received $10.",
read: false,
createdAt: new Date().toISOString(),
};
describe("NotificationItem", () => {
beforeEach(() => {
resetNotificationsForTesting();
});
it("renders title and message", () => {
wrap(<NotificationItem notification={mockNotification} />);
expect(screen.getByText("Payment received")).toBeInTheDocument();
expect(screen.getByText("You received $10.")).toBeInTheDocument();
});
it("shows Mark as read when unread", () => {
wrap(<NotificationItem notification={mockNotification} />);
expect(screen.getByRole("button", { name: /mark as read/i })).toBeInTheDocument();
});
it("shows Mark as unread when read", () => {
wrap(
<NotificationItem
notification={{ ...mockNotification, read: true }}
/>
);
expect(screen.getByRole("button", { name: /mark as unread/i })).toBeInTheDocument();
});
it("mark as read updates store", () => {
useNotificationsStore.setState({
notifications: [mockNotification],
});
wrap(<NotificationItem notification={mockNotification} />);
const btn = screen.getByRole("button", { name: /mark as read/i });
fireEvent.click(btn);
const updated = useNotificationsStore.getState().notifications.find((n) => n.id === "test-1");
expect(updated?.read).toBe(true);
});
it("has data-testid for notification id", () => {
wrap(<NotificationItem notification={mockNotification} />);
expect(screen.getByTestId("notification-test-1")).toBeInTheDocument();
});
});