@@ -21,12 +21,13 @@ describe('Json Component', () => {
2121 getByText ( '"bar"' )
2222 } )
2323
24- it . for ( [
25- [ ] ,
26- [ 1 , 2 , 3 ] ,
27- Array . from ( { length : 101 } , ( _ , i ) => i ) ,
28- ] ) ( 'collapses any array with primitives' , ( array ) => {
29- const { getByRole } = render ( < Json json = { array } /> )
24+ it ( 'expands root array by default' , ( ) => {
25+ const { getByRole } = render ( < Json json = { [ 1 , 2 , 3 ] } /> )
26+ expect ( getByRole ( 'treeitem' ) . ariaExpanded ) . toBe ( 'true' )
27+ } )
28+
29+ it ( 'collapses array with primitives when expandRoot is false' , ( ) => {
30+ const { getByRole } = render ( < Json json = { [ 1 , 2 , 3 ] } expandRoot = { false } /> )
3031 expect ( getByRole ( 'treeitem' ) . ariaExpanded ) . toBe ( 'false' )
3132 } )
3233
@@ -41,10 +42,9 @@ describe('Json Component', () => {
4142 expect ( queryByText ( / l e n g t h / ) ) . toBeNull ( )
4243 } )
4344
44- it . for ( [
45- Array . from ( { length : 101 } , ( _ , i ) => i ) ,
46- ] ) ( 'hides long arrays with trailing comment about length' , ( array ) => {
47- const { getByText } = render ( < Json json = { array } /> )
45+ it ( 'hides long arrays with trailing comment about length when collapsed' , ( ) => {
46+ const longArray = Array . from ( { length : 101 } , ( _ , i ) => i )
47+ const { getByText } = render ( < Json json = { longArray } expandRoot = { false } /> )
4848 getByText ( '...' )
4949 getByText ( / l e n g t h / )
5050 } )
@@ -109,20 +109,19 @@ describe('Json Component', () => {
109109 getByText ( / e n t r i e s / )
110110 } )
111111
112- it . for ( [
113- { } ,
114- { a : 1 , b : 2 } ,
115- { a : 1 , b : true , c : null , d : undefined } ,
116- Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key ${ i } ` , { nested : true } ] ) ) ,
117- ] ) ( 'collapses long objects, or objects with only primitive values (included empty object) ' , ( obj ) => {
118- const { getByRole } = render ( < Json json = { obj } /> )
112+ it ( 'expands root object by default' , ( ) => {
113+ const { getByRole } = render ( < Json json = { { a : 1 , b : 2 } } /> )
114+ expect ( getByRole ( 'treeitem' ) . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' )
115+ } )
116+
117+ it ( 'collapses objects with only primitive values when expandRoot is false ' , ( ) => {
118+ const { getByRole } = render ( < Json json = { { a : 1 , b : 2 } } expandRoot = { false } /> )
119119 expect ( getByRole ( 'treeitem' ) . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' )
120120 } )
121121
122- it . for ( [
123- Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key${ i } ` , { nested : true } ] ) ) ,
124- ] ) ( 'hides the content and append number of entries when objects has many entries' , ( obj ) => {
125- const { getByText } = render ( < Json json = { obj } /> )
122+ it ( 'hides the content and append number of entries when objects has many entries when collapsed' , ( ) => {
123+ const longObject = Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key${ i } ` , { nested : true } ] ) )
124+ const { getByText } = render ( < Json json = { longObject } expandRoot = { false } /> )
126125 getByText ( '...' )
127126 getByText ( / e n t r i e s / )
128127 } )
@@ -131,24 +130,26 @@ describe('Json Component', () => {
131130 const longArray = Array . from ( { length : 101 } , ( _ , i ) => i )
132131 const { getByRole, getByText, queryByText } = render ( < Json json = { longArray } /> )
133132 const treeItem = getByRole ( 'treeitem' )
134- getByText ( '...' )
133+ expect ( queryByText ( '...' ) ) . toBeNull ( ) // expanded by default
135134 const user = userEvent . setup ( )
136- await user . click ( treeItem )
137- expect ( queryByText ( '...' ) ) . toBeNull ( )
138- await user . click ( treeItem )
135+ await user . click ( treeItem ) // collapse
139136 getByText ( '...' )
137+ await user . click ( treeItem ) // expand again
138+ expect ( queryByText ( '...' ) ) . toBeNull ( )
140139 } )
141140
142141 it ( 'toggles object collapse state' , async ( ) => {
143142 const longObject = Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key${ i } ` , { nested : true } ] ) )
144- const { getByRole, getByText, queryByText } = render ( < Json json = { longObject } /> )
145- const treeItem = getByRole ( 'treeitem' ) // only one treeitem because the inner objects are collapsed and not represented as treeitems
146- getByText ( '...' )
147- const user = userEvent . setup ( )
148- await user . click ( treeItem )
143+ const { getAllByRole, getByRole, getByText, queryByText } = render ( < Json json = { longObject } /> )
144+ const treeItem = getAllByRole ( 'treeitem' ) [ 0 ] // expanded by default due to expandRoot
145+ if ( ! treeItem ) throw new Error ( 'No root element found' )
149146 expect ( queryByText ( '...' ) ) . toBeNull ( )
150- await user . click ( treeItem )
147+ const user = userEvent . setup ( )
148+ await user . click ( treeItem ) // collapse
149+ getByRole ( 'treeitem' ) // now only one treeitem
151150 getByText ( '...' )
151+ await user . click ( treeItem ) // expand again
152+ expect ( queryByText ( '...' ) ) . toBeNull ( )
152153 } )
153154} )
154155
0 commit comments