|
1 |
| -import { render, screen, fireEvent } from "@testing-library/react"; |
| 1 | +import { render, screen, fireEvent, within } from "@testing-library/react"; |
| 2 | +import { useState } from "react"; |
2 | 3 | import { describe, it, expect, jest } from "@jest/globals";
|
3 | 4 | import HistoryAndNotifications from "../HistoryAndNotifications";
|
4 | 5 | import { ServerNotification } from "@modelcontextprotocol/sdk/types.js";
|
@@ -223,4 +224,66 @@ describe("HistoryAndNotifications", () => {
|
223 | 224 | expect(screen.getByText("No history yet")).toBeTruthy();
|
224 | 225 | expect(screen.getByText("No notifications yet")).toBeTruthy();
|
225 | 226 | });
|
| 227 | + |
| 228 | + it("clears request history when Clear is clicked", () => { |
| 229 | + const Wrapper = () => { |
| 230 | + const [history, setHistory] = useState(mockRequestHistory); |
| 231 | + return ( |
| 232 | + <HistoryAndNotifications |
| 233 | + requestHistory={history} |
| 234 | + serverNotifications={[]} |
| 235 | + onClearHistory={() => setHistory([])} |
| 236 | + /> |
| 237 | + ); |
| 238 | + }; |
| 239 | + |
| 240 | + render(<Wrapper />); |
| 241 | + |
| 242 | + // Verify items are present initially |
| 243 | + expect(screen.getByText("2. test/method2")).toBeTruthy(); |
| 244 | + expect(screen.getByText("1. test/method1")).toBeTruthy(); |
| 245 | + |
| 246 | + // Click Clear in History header (scoped by the History heading's container) |
| 247 | + const historyHeader = screen.getByText("History"); |
| 248 | + const historyHeaderContainer = historyHeader.parentElement as HTMLElement; |
| 249 | + const historyClearButton = within(historyHeaderContainer).getByRole( |
| 250 | + "button", |
| 251 | + { name: "Clear" }, |
| 252 | + ); |
| 253 | + fireEvent.click(historyClearButton); |
| 254 | + |
| 255 | + // History should now be empty |
| 256 | + expect(screen.getByText("No history yet")).toBeTruthy(); |
| 257 | + }); |
| 258 | + |
| 259 | + it("clears server notifications when Clear is clicked", () => { |
| 260 | + const Wrapper = () => { |
| 261 | + const [notifications, setNotifications] = |
| 262 | + useState<ServerNotification[]>(mockNotifications); |
| 263 | + return ( |
| 264 | + <HistoryAndNotifications |
| 265 | + requestHistory={[]} |
| 266 | + serverNotifications={notifications} |
| 267 | + onClearNotifications={() => setNotifications([])} |
| 268 | + /> |
| 269 | + ); |
| 270 | + }; |
| 271 | + |
| 272 | + render(<Wrapper />); |
| 273 | + |
| 274 | + // Verify items are present initially |
| 275 | + expect(screen.getByText("2. notifications/progress")).toBeTruthy(); |
| 276 | + expect(screen.getByText("1. notifications/message")).toBeTruthy(); |
| 277 | + |
| 278 | + // Click Clear in Server Notifications header (scoped by its heading's container) |
| 279 | + const notifHeader = screen.getByText("Server Notifications"); |
| 280 | + const notifHeaderContainer = notifHeader.parentElement as HTMLElement; |
| 281 | + const notifClearButton = within(notifHeaderContainer).getByRole("button", { |
| 282 | + name: "Clear", |
| 283 | + }); |
| 284 | + fireEvent.click(notifClearButton); |
| 285 | + |
| 286 | + // Notifications should now be empty |
| 287 | + expect(screen.getByText("No notifications yet")).toBeTruthy(); |
| 288 | + }); |
226 | 289 | });
|
0 commit comments