@@ -15,6 +15,7 @@ import {
15
15
useDocById ,
16
16
findSidebarCategory ,
17
17
useCurrentSidebarCategory ,
18
+ useCurrentSidebarSiblings ,
18
19
useSidebarBreadcrumbs ,
19
20
isVisibleSidebarItem ,
20
21
} from '../docsUtils' ;
@@ -780,3 +781,128 @@ describe('useCurrentSidebarCategory', () => {
780
781
) ;
781
782
} ) ;
782
783
} ) ;
784
+
785
+ describe ( 'useCurrentSidebarSiblings' , ( ) => {
786
+ const createUseCurrentSidebarSiblingsMock =
787
+ ( sidebar ?: PropSidebar ) => ( location : string ) =>
788
+ renderHook ( ( ) => useCurrentSidebarSiblings ( ) , {
789
+ wrapper : ( { children} ) => (
790
+ < DocsSidebarProvider name = "sidebarName" items = { sidebar } >
791
+ < StaticRouter location = { location } > { children } </ StaticRouter >
792
+ </ DocsSidebarProvider >
793
+ ) ,
794
+ } ) . result . current ;
795
+
796
+ it ( 'works for sidebar category' , ( ) => {
797
+ const category : PropSidebarItemCategory = testCategory ( {
798
+ href : '/cat' ,
799
+ items : [ testLink ( ) , testLink ( ) ] ,
800
+ } ) ;
801
+ const sidebar : PropSidebar = [
802
+ testLink ( ) ,
803
+ testLink ( ) ,
804
+ category ,
805
+ testCategory ( ) ,
806
+ ] ;
807
+
808
+ const mockUseCurrentSidebarCategory =
809
+ createUseCurrentSidebarSiblingsMock ( sidebar ) ;
810
+
811
+ expect ( mockUseCurrentSidebarCategory ( '/cat' ) ) . toEqual ( category . items ) ;
812
+ } ) ;
813
+
814
+ it ( 'works for sidebar root' , ( ) => {
815
+ const category : PropSidebarItemCategory = testCategory ( {
816
+ href : '/cat' ,
817
+ items : [ testLink ( ) , testLink ( ) ] ,
818
+ } ) ;
819
+ const sidebar : PropSidebar = [
820
+ testLink ( { href : '/rootLink' } ) ,
821
+ testLink ( ) ,
822
+ category ,
823
+ testCategory ( ) ,
824
+ ] ;
825
+
826
+ const mockUseCurrentSidebarCategory =
827
+ createUseCurrentSidebarSiblingsMock ( sidebar ) ;
828
+
829
+ expect ( mockUseCurrentSidebarCategory ( '/rootLink' ) ) . toEqual ( sidebar ) ;
830
+ } ) ;
831
+
832
+ it ( 'works for nested sidebar category' , ( ) => {
833
+ const category2 : PropSidebarItemCategory = testCategory ( {
834
+ href : '/cat2' ,
835
+ items : [ testLink ( ) , testCategory ( ) ] ,
836
+ } ) ;
837
+ const category1 : PropSidebarItemCategory = testCategory ( {
838
+ href : '/cat1' ,
839
+ items : [ testLink ( ) , testLink ( ) , category2 , testCategory ( ) ] ,
840
+ } ) ;
841
+ const sidebar : PropSidebar = [
842
+ testLink ( ) ,
843
+ testLink ( ) ,
844
+ category1 ,
845
+ testCategory ( ) ,
846
+ ] ;
847
+
848
+ const mockUseCurrentSidebarCategory =
849
+ createUseCurrentSidebarSiblingsMock ( sidebar ) ;
850
+
851
+ expect ( mockUseCurrentSidebarCategory ( '/cat2' ) ) . toEqual ( category2 . items ) ;
852
+ } ) ;
853
+
854
+ it ( 'works for category link item' , ( ) => {
855
+ const link = testLink ( { href : '/my/link/path' } ) ;
856
+ const category : PropSidebarItemCategory = testCategory ( {
857
+ href : '/cat1' ,
858
+ items : [ testLink ( ) , testLink ( ) , link , testCategory ( ) ] ,
859
+ } ) ;
860
+ const sidebar : PropSidebar = [
861
+ testLink ( ) ,
862
+ testLink ( ) ,
863
+ category ,
864
+ testCategory ( ) ,
865
+ ] ;
866
+
867
+ const mockUseCurrentSidebarCategory =
868
+ createUseCurrentSidebarSiblingsMock ( sidebar ) ;
869
+
870
+ expect ( mockUseCurrentSidebarCategory ( '/my/link/path' ) ) . toEqual (
871
+ category . items ,
872
+ ) ;
873
+ } ) ;
874
+
875
+ it ( 'works for nested category link item' , ( ) => {
876
+ const link = testLink ( { href : '/my/link/path' } ) ;
877
+ const category2 : PropSidebarItemCategory = testCategory ( {
878
+ href : '/cat2' ,
879
+ items : [ testLink ( ) , testLink ( ) , link , testCategory ( ) ] ,
880
+ } ) ;
881
+ const category1 : PropSidebarItemCategory = testCategory ( {
882
+ href : '/cat1' ,
883
+ items : [ testLink ( ) , testLink ( ) , category2 , testCategory ( ) ] ,
884
+ } ) ;
885
+ const sidebar : PropSidebar = [
886
+ testLink ( ) ,
887
+ testLink ( ) ,
888
+ category1 ,
889
+ testCategory ( ) ,
890
+ ] ;
891
+
892
+ const mockUseCurrentSidebarCategory =
893
+ createUseCurrentSidebarSiblingsMock ( sidebar ) ;
894
+
895
+ expect ( mockUseCurrentSidebarCategory ( '/my/link/path' ) ) . toEqual (
896
+ category2 . items ,
897
+ ) ;
898
+ } ) ;
899
+
900
+ it ( 'throws when sidebar is missing' , ( ) => {
901
+ const mockUseCurrentSidebarCategory = createUseCurrentSidebarSiblingsMock ( ) ;
902
+ expect ( ( ) =>
903
+ mockUseCurrentSidebarCategory ( '/cat' ) ,
904
+ ) . toThrowErrorMatchingInlineSnapshot (
905
+ `"Unexpected: cant find current sidebar in context"` ,
906
+ ) ;
907
+ } ) ;
908
+ } ) ;
0 commit comments