@@ -6,36 +6,46 @@ import { TextContentEntry } from '../NavEntry'
66const mockEntries : TextContentEntry [ ] = [
77 {
88 id : 'entry1' ,
9- data : { id : 'Entry1' , section : 'section1 ' } ,
9+ data : { id : 'Entry1' , section : 'section-one ' } ,
1010 collection : 'textContent' ,
1111 } ,
1212 {
1313 id : 'entry2' ,
14- data : { id : 'Entry2' , section : 'section1 ' } ,
14+ data : { id : 'Entry2' , section : 'section-two ' } ,
1515 collection : 'textContent' ,
1616 } ,
1717 {
1818 id : 'entry3' ,
19- data : { id : 'Entry3' , section : 'section2' } ,
19+ data : { id : 'Entry3' , section : 'section-two' } ,
20+ collection : 'textContent' ,
21+ } ,
22+ {
23+ id : 'entry4' ,
24+ data : { id : 'Entry4' , section : 'section-three' } ,
25+ collection : 'textContent' ,
26+ } ,
27+ {
28+ id : 'entry5' ,
29+ data : { id : 'Entry5' , section : 'section-four' } ,
2030 collection : 'textContent' ,
2131 } ,
2232]
2333
2434it ( 'renders without crashing' , ( ) => {
2535 render ( < Navigation navEntries = { mockEntries } /> )
26- expect ( screen . getByText ( 'Section1 ' ) ) . toBeInTheDocument ( )
27- expect ( screen . getByText ( 'Section2 ' ) ) . toBeInTheDocument ( )
36+ expect ( screen . getByText ( 'Section one ' ) ) . toBeInTheDocument ( )
37+ expect ( screen . getByText ( 'Section two ' ) ) . toBeInTheDocument ( )
2838} )
2939
3040it ( 'renders the correct number of sections' , ( ) => {
3141 render ( < Navigation navEntries = { mockEntries } /> )
32- expect ( screen . getAllByRole ( 'listitem' ) ) . toHaveLength ( 2 )
42+ expect ( screen . getAllByRole ( 'listitem' ) ) . toHaveLength ( 4 )
3343} )
3444
3545it ( 'sets the active item based on the current pathname' , ( ) => {
3646 Object . defineProperty ( window , 'location' , {
3747 value : {
38- pathname : '/section1 /entry1' ,
48+ pathname : '/section-one /entry1' ,
3949 } ,
4050 writable : true ,
4151 } )
@@ -48,17 +58,60 @@ it('sets the active item based on the current pathname', () => {
4858} )
4959
5060it ( 'updates the active item on selection' , async ( ) => {
61+ // prevent errors when trying to navigate from logging in the console and cluttering the test output
62+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
63+
5164 const user = userEvent . setup ( )
5265
5366 render ( < Navigation navEntries = { mockEntries } /> )
5467
68+ const sectionTwo = screen . getByRole ( 'button' , { name : 'Section two' } )
69+
70+ await user . click ( sectionTwo )
71+
5572 const entryLink = screen . getByRole ( 'link' , { name : 'Entry2' } )
5673
5774 await user . click ( entryLink )
5875
5976 expect ( entryLink ) . toHaveClass ( 'pf-m-current' )
6077} )
6178
79+ it ( 'sorts all sections alphabetically by default' , ( ) => {
80+ render ( < Navigation navEntries = { mockEntries } /> )
81+
82+ const sections = screen . getAllByRole ( 'button' )
83+
84+ expect ( sections [ 0 ] ) . toHaveTextContent ( 'Section four' )
85+ expect ( sections [ 1 ] ) . toHaveTextContent ( 'Section one' )
86+ expect ( sections [ 2 ] ) . toHaveTextContent ( 'Section three' )
87+ expect ( sections [ 3 ] ) . toHaveTextContent ( 'Section two' )
88+ } )
89+
90+ it ( 'sorts sections based on the order provided' , ( ) => {
91+ render (
92+ < Navigation
93+ navEntries = { mockEntries }
94+ navSectionOrder = { [ 'section-two' , 'section-one' ] }
95+ /> ,
96+ )
97+
98+ const sections = screen . getAllByRole ( 'button' )
99+
100+ expect ( sections [ 0 ] ) . toHaveTextContent ( 'Section two' )
101+ expect ( sections [ 1 ] ) . toHaveTextContent ( 'Section one' )
102+ } )
103+
104+ it ( 'sorts unordered sections alphabetically after ordered sections' , ( ) => {
105+ render (
106+ < Navigation navEntries = { mockEntries } navSectionOrder = { [ 'section-two' ] } /> ,
107+ )
108+
109+ const sections = screen . getAllByRole ( 'button' )
110+
111+ expect ( sections [ 2 ] ) . toHaveTextContent ( 'Section one' )
112+ expect ( sections [ 3 ] ) . toHaveTextContent ( 'Section three' )
113+ } )
114+
62115it ( 'matches snapshot' , ( ) => {
63116 const { asFragment } = render ( < Navigation navEntries = { mockEntries } /> )
64117 expect ( asFragment ( ) ) . toMatchSnapshot ( )
0 commit comments