1
- import { render , screen , fireEvent , act } from "@testing-library/react" ;
1
+ import {
2
+ render ,
3
+ screen ,
4
+ fireEvent ,
5
+ act ,
6
+ waitFor ,
7
+ } from "@testing-library/react" ;
2
8
import "@testing-library/jest-dom" ;
3
9
import { describe , it , beforeEach , jest } from "@jest/globals" ;
4
10
import Sidebar from "../Sidebar" ;
@@ -623,125 +629,187 @@ describe("Sidebar", () => {
623
629
fireEvent . click ( button ) ;
624
630
} ;
625
631
626
- it ( "should update bearer token" , ( ) => {
627
- const setBearerToken = jest . fn ( ) ;
632
+ it ( "should update bearer token via custom headers" , async ( ) => {
633
+ const setCustomHeaders = jest . fn ( ) ;
628
634
renderSidebar ( {
629
- bearerToken : "" ,
630
- setBearerToken ,
635
+ customHeaders : [ ] ,
636
+ setCustomHeaders ,
631
637
transportType : "sse" , // Set transport type to SSE
632
638
} ) ;
633
639
634
640
openAuthSection ( ) ;
635
641
636
- const tokenInput = screen . getByTestId ( "bearer-token-input" ) ;
637
- fireEvent . change ( tokenInput , { target : { value : "new_token" } } ) ;
642
+ // Add a new header
643
+ const addButton = screen . getByTestId ( "add-header-button" ) ;
644
+ fireEvent . click ( addButton ) ;
638
645
639
- expect ( setBearerToken ) . toHaveBeenCalledWith ( "new_token" ) ;
646
+ // Verify that setCustomHeaders was called to add an empty header
647
+ expect ( setCustomHeaders ) . toHaveBeenCalledWith ( [
648
+ {
649
+ name : "" ,
650
+ value : "" ,
651
+ enabled : true ,
652
+ } ,
653
+ ] ) ;
640
654
} ) ;
641
655
642
- it ( "should update header name" , ( ) => {
643
- const setHeaderName = jest . fn ( ) ;
656
+ it ( "should update header name via custom headers " , ( ) => {
657
+ const setCustomHeaders = jest . fn ( ) ;
644
658
renderSidebar ( {
645
- headerName : "Authorization" ,
646
- setHeaderName,
659
+ customHeaders : [
660
+ {
661
+ name : "Authorization" ,
662
+ value : "Bearer token123" ,
663
+ enabled : true ,
664
+ } ,
665
+ ] ,
666
+ setCustomHeaders,
647
667
transportType : "sse" ,
648
668
} ) ;
649
669
650
670
openAuthSection ( ) ;
651
671
652
- const headerInput = screen . getByTestId ( "header-input" ) ;
653
- fireEvent . change ( headerInput , { target : { value : "X-Custom-Auth" } } ) ;
672
+ const headerNameInput = screen . getByTestId ( "header-name- input-0 " ) ;
673
+ fireEvent . change ( headerNameInput , { target : { value : "X-Custom-Auth" } } ) ;
654
674
655
- expect ( setHeaderName ) . toHaveBeenCalledWith ( "X-Custom-Auth" ) ;
675
+ expect ( setCustomHeaders ) . toHaveBeenCalledWith ( [
676
+ {
677
+ name : "X-Custom-Auth" ,
678
+ value : "Bearer token123" ,
679
+ enabled : true ,
680
+ } ,
681
+ ] ) ;
656
682
} ) ;
657
683
658
- it ( "should clear bearer token" , ( ) => {
659
- const setBearerToken = jest . fn ( ) ;
684
+ it ( "should clear bearer token via custom headers " , ( ) => {
685
+ const setCustomHeaders = jest . fn ( ) ;
660
686
renderSidebar ( {
661
- bearerToken : "existing_token" ,
662
- setBearerToken,
687
+ customHeaders : [
688
+ {
689
+ name : "Authorization" ,
690
+ value : "Bearer existing_token" ,
691
+ enabled : true ,
692
+ } ,
693
+ ] ,
694
+ setCustomHeaders,
663
695
transportType : "sse" , // Set transport type to SSE
664
696
} ) ;
665
697
666
698
openAuthSection ( ) ;
667
699
668
- const tokenInput = screen . getByTestId ( "bearer-token -input" ) ;
669
- fireEvent . change ( tokenInput , { target : { value : "" } } ) ;
700
+ const headerValueInput = screen . getByTestId ( "header-value -input-0 " ) ;
701
+ fireEvent . change ( headerValueInput , { target : { value : "" } } ) ;
670
702
671
- expect ( setBearerToken ) . toHaveBeenCalledWith ( "" ) ;
703
+ expect ( setCustomHeaders ) . toHaveBeenCalledWith ( [
704
+ {
705
+ name : "Authorization" ,
706
+ value : "" ,
707
+ enabled : true ,
708
+ } ,
709
+ ] ) ;
672
710
} ) ;
673
711
674
- it ( "should properly render bearer token input" , ( ) => {
712
+ it ( "should properly render header value input as password field " , ( ) => {
675
713
const { rerender } = renderSidebar ( {
676
- bearerToken : "existing_token" ,
714
+ customHeaders : [
715
+ {
716
+ name : "Authorization" ,
717
+ value : "Bearer existing_token" ,
718
+ enabled : true ,
719
+ } ,
720
+ ] ,
677
721
transportType : "sse" , // Set transport type to SSE
678
722
} ) ;
679
723
680
724
openAuthSection ( ) ;
681
725
682
726
// Token input should be a password field
683
- const tokenInput = screen . getByTestId ( "bearer-token -input" ) ;
727
+ const tokenInput = screen . getByTestId ( "header-value -input-0 " ) ;
684
728
expect ( tokenInput ) . toHaveProperty ( "type" , "password" ) ;
685
729
686
730
// Update the token
687
- fireEvent . change ( tokenInput , { target : { value : "new_token" } } ) ;
731
+ fireEvent . change ( tokenInput , { target : { value : "Bearer new_token" } } ) ;
688
732
689
733
// Rerender with updated token
690
734
rerender (
691
735
< TooltipProvider >
692
736
< Sidebar
693
737
{ ...defaultProps }
694
- bearerToken = "new_token"
738
+ customHeaders = { [
739
+ {
740
+ name : "Authorization" ,
741
+ value : "Bearer new_token" ,
742
+ enabled : true ,
743
+ } ,
744
+ ] }
695
745
transportType = "sse"
696
746
/>
697
747
</ TooltipProvider > ,
698
748
) ;
699
749
700
750
// Token input should still exist after update
701
- expect ( screen . getByTestId ( "bearer-token -input" ) ) . toBeInTheDocument ( ) ;
751
+ expect ( screen . getByTestId ( "header-value -input-0 " ) ) . toBeInTheDocument ( ) ;
702
752
} ) ;
703
753
704
754
it ( "should maintain token visibility state after update" , ( ) => {
705
755
const { rerender } = renderSidebar ( {
706
- bearerToken : "existing_token" ,
756
+ customHeaders : [
757
+ {
758
+ name : "Authorization" ,
759
+ value : "Bearer existing_token" ,
760
+ enabled : true ,
761
+ } ,
762
+ ] ,
707
763
transportType : "sse" , // Set transport type to SSE
708
764
} ) ;
709
765
710
766
openAuthSection ( ) ;
711
767
712
768
// Token input should be a password field
713
- const tokenInput = screen . getByTestId ( "bearer-token -input" ) ;
769
+ const tokenInput = screen . getByTestId ( "header-value -input-0 " ) ;
714
770
expect ( tokenInput ) . toHaveProperty ( "type" , "password" ) ;
715
771
716
772
// Update the token
717
- fireEvent . change ( tokenInput , { target : { value : "new_token" } } ) ;
773
+ fireEvent . change ( tokenInput , { target : { value : "Bearer new_token" } } ) ;
718
774
719
775
// Rerender with updated token
720
776
rerender (
721
777
< TooltipProvider >
722
778
< Sidebar
723
779
{ ...defaultProps }
724
- bearerToken = "new_token"
780
+ customHeaders = { [
781
+ {
782
+ name : "Authorization" ,
783
+ value : "Bearer new_token" ,
784
+ enabled : true ,
785
+ } ,
786
+ ] }
725
787
transportType = "sse"
726
788
/>
727
789
</ TooltipProvider > ,
728
790
) ;
729
791
730
792
// Token input should still exist after update
731
- expect ( screen . getByTestId ( "bearer-token -input" ) ) . toBeInTheDocument ( ) ;
793
+ expect ( screen . getByTestId ( "header-value -input-0 " ) ) . toBeInTheDocument ( ) ;
732
794
} ) ;
733
795
734
796
it ( "should maintain header name when toggling auth section" , ( ) => {
735
797
renderSidebar ( {
736
- headerName : "X-API-Key" ,
798
+ customHeaders : [
799
+ {
800
+ name : "X-API-Key" ,
801
+ value : "api-key-123" ,
802
+ enabled : true ,
803
+ } ,
804
+ ] ,
737
805
transportType : "sse" ,
738
806
} ) ;
739
807
740
808
// Open auth section
741
809
openAuthSection ( ) ;
742
810
743
811
// Verify header name is displayed
744
- const headerInput = screen . getByTestId ( "header-input" ) ;
812
+ const headerInput = screen . getByTestId ( "header-name- input-0 " ) ;
745
813
expect ( headerInput ) . toHaveValue ( "X-API-Key" ) ;
746
814
747
815
// Close auth section
@@ -752,19 +820,66 @@ describe("Sidebar", () => {
752
820
fireEvent . click ( authButton ) ;
753
821
754
822
// Verify header name is still preserved
755
- expect ( screen . getByTestId ( "header-input" ) ) . toHaveValue ( "X-API-Key" ) ;
823
+ expect ( screen . getByTestId ( "header-name-input-0" ) ) . toHaveValue (
824
+ "X-API-Key" ,
825
+ ) ;
756
826
} ) ;
757
827
758
- it ( "should display default header name when not specified " , ( ) => {
828
+ it ( "should display placeholder for header name when no headers exist " , ( ) => {
759
829
renderSidebar ( {
760
- headerName : undefined ,
830
+ customHeaders : [ ] ,
761
831
transportType : "sse" ,
762
832
} ) ;
763
833
764
834
openAuthSection ( ) ;
765
835
766
- const headerInput = screen . getByTestId ( "header-input" ) ;
767
- expect ( headerInput ) . toHaveAttribute ( "placeholder" , "Authorization" ) ;
836
+ // Verify that the "No custom headers configured" message is shown
837
+ expect (
838
+ screen . getByText ( "No custom headers configured" ) ,
839
+ ) . toBeInTheDocument ( ) ;
840
+ expect (
841
+ screen . getByText ( 'Click "Add" to get started' ) ,
842
+ ) . toBeInTheDocument ( ) ;
843
+
844
+ // Verify the Add button is present
845
+ const addButton = screen . getByTestId ( "add-header-button" ) ;
846
+ expect ( addButton ) . toBeInTheDocument ( ) ;
847
+ } ) ;
848
+
849
+ it ( "should allow editing existing headers" , ( ) => {
850
+ const setCustomHeaders = jest . fn ( ) ;
851
+ renderSidebar ( {
852
+ customHeaders : [
853
+ {
854
+ name : "Authorization" ,
855
+ value : "Bearer token123" ,
856
+ enabled : true ,
857
+ } ,
858
+ ] ,
859
+ setCustomHeaders,
860
+ transportType : "sse" ,
861
+ } ) ;
862
+
863
+ openAuthSection ( ) ;
864
+
865
+ // Verify header inputs are rendered
866
+ const headerNameInput = screen . getByTestId ( "header-name-input-0" ) ;
867
+ const headerValueInput = screen . getByTestId ( "header-value-input-0" ) ;
868
+
869
+ expect ( headerNameInput ) . toHaveValue ( "Authorization" ) ;
870
+ expect ( headerValueInput ) . toHaveValue ( "Bearer token123" ) ;
871
+ expect ( headerNameInput ) . toHaveAttribute ( "placeholder" , "Header Name" ) ;
872
+ expect ( headerValueInput ) . toHaveAttribute ( "placeholder" , "Header Value" ) ;
873
+
874
+ // Update header name
875
+ fireEvent . change ( headerNameInput , { target : { value : "X-API-Key" } } ) ;
876
+ expect ( setCustomHeaders ) . toHaveBeenCalledWith ( [
877
+ {
878
+ name : "X-API-Key" ,
879
+ value : "Bearer token123" ,
880
+ enabled : true ,
881
+ } ,
882
+ ] ) ;
768
883
} ) ;
769
884
} ) ;
770
885
0 commit comments