|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
import { NotificationItemType } from "@/providers/Notification" |
|
5
|
|
|
6
|
// mock data |
|
7
|
const mockNotifications: NotificationItemType[] = [ |
|
8
|
{ |
|
9
|
id: "1", |
|
10
|
title: "Top Notification", |
|
11
|
placement: "top", |
|
12
|
}, |
|
13
|
{ |
|
14
|
id: "2", |
|
15
|
title: "Bottom Notification", |
|
16
|
placement: "bottom", |
|
17
|
}, |
|
18
|
{ |
|
19
|
id: "3", |
|
20
|
title: "Another Top", |
|
21
|
placement: "top", |
|
22
|
onClose: vi.fn(), |
|
23
|
}, |
|
24
|
] |
|
25
|
|
|
26
|
// mock functions |
|
27
|
const mockRemoveNotification = vi.fn() |
|
28
|
|
|
29
|
// mock components |
|
30
|
vi.mock("react-transition-group", () => ({ |
|
31
|
TransitionGroup: ({ |
|
32
|
children, |
|
33
|
className, |
|
34
|
}: { |
|
35
|
children: React.ReactNode |
|
36
|
className?: string |
|
37
|
}) => ( |
|
38
|
<div data-testid="transition-group" className={className}> |
|
39
|
{children} |
|
40
|
</div> |
|
41
|
), |
|
42
|
})) |
|
43
|
|
|
44
|
vi.mock("@/components/Notification/Item", () => ({ |
|
45
|
NotificationItem: ({ |
|
46
|
id, |
|
47
|
title, |
|
48
|
className, |
|
49
|
}: { |
|
50
|
id?: string |
|
51
|
title?: string |
|
52
|
className?: string |
|
53
|
}) => ( |
|
54
|
<div data-testid="notification-item" data-id={id} className={className}> |
|
55
|
{title} |
|
56
|
</div> |
|
57
|
), |
|
58
|
})) |
|
59
|
|
|
60
|
vi.mock("@/providers/Notification", () => ({ |
|
61
|
useNotifications: () => ({ |
|
62
|
notifications: mockNotifications, |
|
63
|
removeNotification: mockRemoveNotification, |
|
64
|
}), |
|
65
|
})) |
|
66
|
|
|
67
|
import { NotificationContainer } from "../../Notification" |
|
68
|
|
|
69
|
beforeEach(() => { |
|
70
|
vi.clearAllMocks() |
|
71
|
}) |
|
72
|
|
|
73
|
describe("rendering", () => { |
|
74
|
test("renders notification container", () => { |
|
75
|
const { container } = render(<NotificationContainer />) |
|
76
|
const transitionGroups = container.querySelectorAll( |
|
77
|
"[data-testid='transition-group']" |
|
78
|
) |
|
79
|
expect(transitionGroups).toHaveLength(2) |
|
80
|
}) |
|
81
|
|
|
82
|
test("renders top notifications in top transition group", () => { |
|
83
|
const { container } = render(<NotificationContainer />) |
|
84
|
const transitionGroups = container.querySelectorAll( |
|
85
|
"[data-testid='transition-group']" |
|
86
|
) |
|
87
|
const topGroup = transitionGroups[0] |
|
88
|
expect(topGroup).toHaveClass("top-0") |
|
89
|
const topNotifications = topGroup.querySelectorAll( |
|
90
|
"[data-testid='notification-item']" |
|
91
|
) |
|
92
|
expect(topNotifications).toHaveLength(2) |
|
93
|
expect(topNotifications[0]).toHaveAttribute("data-id", "1") |
|
94
|
expect(topNotifications[1]).toHaveAttribute("data-id", "3") |
|
95
|
}) |
|
96
|
|
|
97
|
test("renders bottom notifications in bottom transition group", () => { |
|
98
|
const { container } = render(<NotificationContainer />) |
|
99
|
const transitionGroups = container.querySelectorAll( |
|
100
|
"[data-testid='transition-group']" |
|
101
|
) |
|
102
|
const bottomGroup = transitionGroups[1] |
|
103
|
expect(bottomGroup).toHaveClass("bottom-0") |
|
104
|
const bottomNotifications = bottomGroup.querySelectorAll( |
|
105
|
"[data-testid='notification-item']" |
|
106
|
) |
|
107
|
expect(bottomNotifications).toHaveLength(1) |
|
108
|
expect(bottomNotifications[0]).toHaveAttribute("data-id", "2") |
|
109
|
}) |
|
110
|
}) |
|
111
|
|
|
112
|
describe("interactions", () => { |
|
113
|
test("renders notifications with onClose callback", () => { |
|
114
|
const mockOnClose = vi.fn() |
|
115
|
const notificationWithOnClose: NotificationItemType = { |
|
116
|
id: "4", |
|
117
|
title: "Test", |
|
118
|
onClose: mockOnClose, |
|
119
|
} |
|
120
|
mockNotifications.push(notificationWithOnClose) |
|
121
|
|
|
122
|
render(<NotificationContainer />) |
|
123
|
// Verify notification is rendered |
|
124
|
// The onClose would be called when user clicks close button |
|
125
|
expect(mockNotifications).toContainEqual(notificationWithOnClose) |
|
126
|
}) |
|
127
|
}) |