Skip to content

Commit 2b963ce

Browse files
fixing test cases after main merged
1 parent af27fa2 commit 2b963ce

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

client/src/components/__tests__/Sidebar.test.tsx

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen, fireEvent } from "@testing-library/react";
1+
import { render, screen, fireEvent, act } from "@testing-library/react";
22
import "@testing-library/jest-dom";
33
import { describe, it, beforeEach, jest } from "@jest/globals";
44
import Sidebar from "../Sidebar";
@@ -14,7 +14,7 @@ jest.mock("../../lib/hooks/useTheme", () => ({
1414

1515
// Mock toast hook
1616
const mockToast = jest.fn();
17-
jest.mock("@/hooks/use-toast", () => ({
17+
jest.mock("@/lib/hooks/useToast", () => ({
1818
useToast: () => ({
1919
toast: mockToast,
2020
}),
@@ -28,6 +28,9 @@ Object.defineProperty(navigator, "clipboard", {
2828
},
2929
});
3030

31+
// Setup fake timers
32+
jest.useFakeTimers();
33+
3134
describe("Sidebar Environment Variables", () => {
3235
const defaultProps = {
3336
connectionStatus: "disconnected" as const,
@@ -69,6 +72,7 @@ describe("Sidebar Environment Variables", () => {
6972

7073
beforeEach(() => {
7174
jest.clearAllMocks();
75+
jest.clearAllTimers();
7276
});
7377

7478
describe("Basic Operations", () => {
@@ -642,9 +646,10 @@ describe("Sidebar Environment Variables", () => {
642646
describe("Copy Configuration Features", () => {
643647
beforeEach(() => {
644648
jest.clearAllMocks();
649+
jest.clearAllTimers();
645650
});
646651

647-
it("should copy server entry configuration to clipboard for STDIO transport", () => {
652+
it("should copy server entry configuration to clipboard for STDIO transport", async () => {
648653
const command = "node";
649654
const args = "--inspect server.js";
650655
const env = { API_KEY: "test-key", DEBUG: "true" };
@@ -656,10 +661,16 @@ describe("Sidebar Environment Variables", () => {
656661
env,
657662
});
658663

659-
const copyServerEntryButton = screen.getByRole("button", {
660-
name: /server entry/i,
664+
// Use act to properly wrap the clipboard operations
665+
await act(async () => {
666+
const copyServerEntryButton = screen.getByRole("button", {
667+
name: /server entry/i,
668+
});
669+
fireEvent.click(copyServerEntryButton);
670+
671+
// Fast-forward timers to handle the setTimeout
672+
jest.runAllTimers();
661673
});
662-
fireEvent.click(copyServerEntryButton);
663674

664675
// Check clipboard API was called with the correct configuration
665676
expect(mockClipboardWrite).toHaveBeenCalledTimes(1);
@@ -677,7 +688,7 @@ describe("Sidebar Environment Variables", () => {
677688
expect(mockClipboardWrite).toHaveBeenCalledWith(expectedConfig);
678689
});
679690

680-
it("should copy servers file configuration to clipboard for STDIO transport", () => {
691+
it("should copy servers file configuration to clipboard for STDIO transport", async () => {
681692
const command = "node";
682693
const args = "--inspect server.js";
683694
const env = { API_KEY: "test-key", DEBUG: "true" };
@@ -689,10 +700,15 @@ describe("Sidebar Environment Variables", () => {
689700
env,
690701
});
691702

692-
const copyServersFileButton = screen.getByRole("button", {
693-
name: /servers file/i,
703+
await act(async () => {
704+
const copyServersFileButton = screen.getByRole("button", {
705+
name: /servers file/i,
706+
});
707+
fireEvent.click(copyServersFileButton);
708+
709+
// Fast-forward timers to handle the setTimeout
710+
jest.runAllTimers();
694711
});
695-
fireEvent.click(copyServersFileButton);
696712

697713
// Check clipboard API was called with the correct configuration
698714
expect(mockClipboardWrite).toHaveBeenCalledTimes(1);
@@ -714,18 +730,23 @@ describe("Sidebar Environment Variables", () => {
714730
expect(mockClipboardWrite).toHaveBeenCalledWith(expectedConfig);
715731
});
716732

717-
it("should copy servers file configuration to clipboard for SSE transport", () => {
733+
it("should copy servers file configuration to clipboard for SSE transport", async () => {
718734
const sseUrl = "http://localhost:3000/events";
719735

720736
renderSidebar({
721737
transportType: "sse",
722738
sseUrl,
723739
});
724740

725-
const copyServersFileButton = screen.getByRole("button", {
726-
name: /servers file/i,
741+
await act(async () => {
742+
const copyServersFileButton = screen.getByRole("button", {
743+
name: /servers file/i,
744+
});
745+
fireEvent.click(copyServersFileButton);
746+
747+
// Fast-forward timers to handle the setTimeout
748+
jest.runAllTimers();
727749
});
728-
fireEvent.click(copyServersFileButton);
729750

730751
// Check clipboard API was called with the correct configuration
731752
expect(mockClipboardWrite).toHaveBeenCalledTimes(1);
@@ -747,7 +768,7 @@ describe("Sidebar Environment Variables", () => {
747768
expect(mockClipboardWrite).toHaveBeenCalledWith(expectedConfig);
748769
});
749770

750-
it("should handle empty args in STDIO transport", () => {
771+
it("should handle empty args in STDIO transport", async () => {
751772
const command = "python";
752773
const args = "";
753774

@@ -757,10 +778,15 @@ describe("Sidebar Environment Variables", () => {
757778
args,
758779
});
759780

760-
const copyServerEntryButton = screen.getByRole("button", {
761-
name: /server entry/i,
781+
await act(async () => {
782+
const copyServerEntryButton = screen.getByRole("button", {
783+
name: /server entry/i,
784+
});
785+
fireEvent.click(copyServerEntryButton);
786+
787+
// Fast-forward timers to handle the setTimeout
788+
jest.runAllTimers();
762789
});
763-
fireEvent.click(copyServerEntryButton);
764790

765791
// Check clipboard API was called with empty args array
766792
expect(mockClipboardWrite).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)