1
- import { render , screen , fireEvent } from "@testing-library/react" ;
1
+ import { render , screen , fireEvent , act } from "@testing-library/react" ;
2
2
import "@testing-library/jest-dom" ;
3
3
import { describe , it , beforeEach , jest } from "@jest/globals" ;
4
4
import Sidebar from "../Sidebar" ;
@@ -14,7 +14,7 @@ jest.mock("../../lib/hooks/useTheme", () => ({
14
14
15
15
// Mock toast hook
16
16
const mockToast = jest . fn ( ) ;
17
- jest . mock ( "@/hooks/use-toast " , ( ) => ( {
17
+ jest . mock ( "@/lib/ hooks/useToast " , ( ) => ( {
18
18
useToast : ( ) => ( {
19
19
toast : mockToast ,
20
20
} ) ,
@@ -28,6 +28,9 @@ Object.defineProperty(navigator, "clipboard", {
28
28
} ,
29
29
} ) ;
30
30
31
+ // Setup fake timers
32
+ jest . useFakeTimers ( ) ;
33
+
31
34
describe ( "Sidebar Environment Variables" , ( ) => {
32
35
const defaultProps = {
33
36
connectionStatus : "disconnected" as const ,
@@ -69,6 +72,7 @@ describe("Sidebar Environment Variables", () => {
69
72
70
73
beforeEach ( ( ) => {
71
74
jest . clearAllMocks ( ) ;
75
+ jest . clearAllTimers ( ) ;
72
76
} ) ;
73
77
74
78
describe ( "Basic Operations" , ( ) => {
@@ -642,9 +646,10 @@ describe("Sidebar Environment Variables", () => {
642
646
describe ( "Copy Configuration Features" , ( ) => {
643
647
beforeEach ( ( ) => {
644
648
jest . clearAllMocks ( ) ;
649
+ jest . clearAllTimers ( ) ;
645
650
} ) ;
646
651
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 ( ) => {
648
653
const command = "node" ;
649
654
const args = "--inspect server.js" ;
650
655
const env = { API_KEY : "test-key" , DEBUG : "true" } ;
@@ -656,10 +661,16 @@ describe("Sidebar Environment Variables", () => {
656
661
env,
657
662
} ) ;
658
663
659
- const copyServerEntryButton = screen . getByRole ( "button" , {
660
- name : / s e r v e r e n t r y / i,
664
+ // Use act to properly wrap the clipboard operations
665
+ await act ( async ( ) => {
666
+ const copyServerEntryButton = screen . getByRole ( "button" , {
667
+ name : / s e r v e r e n t r y / i,
668
+ } ) ;
669
+ fireEvent . click ( copyServerEntryButton ) ;
670
+
671
+ // Fast-forward timers to handle the setTimeout
672
+ jest . runAllTimers ( ) ;
661
673
} ) ;
662
- fireEvent . click ( copyServerEntryButton ) ;
663
674
664
675
// Check clipboard API was called with the correct configuration
665
676
expect ( mockClipboardWrite ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -677,7 +688,7 @@ describe("Sidebar Environment Variables", () => {
677
688
expect ( mockClipboardWrite ) . toHaveBeenCalledWith ( expectedConfig ) ;
678
689
} ) ;
679
690
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 ( ) => {
681
692
const command = "node" ;
682
693
const args = "--inspect server.js" ;
683
694
const env = { API_KEY : "test-key" , DEBUG : "true" } ;
@@ -689,10 +700,15 @@ describe("Sidebar Environment Variables", () => {
689
700
env,
690
701
} ) ;
691
702
692
- const copyServersFileButton = screen . getByRole ( "button" , {
693
- name : / s e r v e r s f i l e / i,
703
+ await act ( async ( ) => {
704
+ const copyServersFileButton = screen . getByRole ( "button" , {
705
+ name : / s e r v e r s f i l e / i,
706
+ } ) ;
707
+ fireEvent . click ( copyServersFileButton ) ;
708
+
709
+ // Fast-forward timers to handle the setTimeout
710
+ jest . runAllTimers ( ) ;
694
711
} ) ;
695
- fireEvent . click ( copyServersFileButton ) ;
696
712
697
713
// Check clipboard API was called with the correct configuration
698
714
expect ( mockClipboardWrite ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -714,18 +730,23 @@ describe("Sidebar Environment Variables", () => {
714
730
expect ( mockClipboardWrite ) . toHaveBeenCalledWith ( expectedConfig ) ;
715
731
} ) ;
716
732
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 ( ) => {
718
734
const sseUrl = "http://localhost:3000/events" ;
719
735
720
736
renderSidebar ( {
721
737
transportType : "sse" ,
722
738
sseUrl,
723
739
} ) ;
724
740
725
- const copyServersFileButton = screen . getByRole ( "button" , {
726
- name : / s e r v e r s f i l e / i,
741
+ await act ( async ( ) => {
742
+ const copyServersFileButton = screen . getByRole ( "button" , {
743
+ name : / s e r v e r s f i l e / i,
744
+ } ) ;
745
+ fireEvent . click ( copyServersFileButton ) ;
746
+
747
+ // Fast-forward timers to handle the setTimeout
748
+ jest . runAllTimers ( ) ;
727
749
} ) ;
728
- fireEvent . click ( copyServersFileButton ) ;
729
750
730
751
// Check clipboard API was called with the correct configuration
731
752
expect ( mockClipboardWrite ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -747,7 +768,7 @@ describe("Sidebar Environment Variables", () => {
747
768
expect ( mockClipboardWrite ) . toHaveBeenCalledWith ( expectedConfig ) ;
748
769
} ) ;
749
770
750
- it ( "should handle empty args in STDIO transport" , ( ) => {
771
+ it ( "should handle empty args in STDIO transport" , async ( ) => {
751
772
const command = "python" ;
752
773
const args = "" ;
753
774
@@ -757,10 +778,15 @@ describe("Sidebar Environment Variables", () => {
757
778
args,
758
779
} ) ;
759
780
760
- const copyServerEntryButton = screen . getByRole ( "button" , {
761
- name : / s e r v e r e n t r y / i,
781
+ await act ( async ( ) => {
782
+ const copyServerEntryButton = screen . getByRole ( "button" , {
783
+ name : / s e r v e r e n t r y / i,
784
+ } ) ;
785
+ fireEvent . click ( copyServerEntryButton ) ;
786
+
787
+ // Fast-forward timers to handle the setTimeout
788
+ jest . runAllTimers ( ) ;
762
789
} ) ;
763
- fireEvent . click ( copyServerEntryButton ) ;
764
790
765
791
// Check clipboard API was called with empty args array
766
792
expect ( mockClipboardWrite ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments