@@ -27,9 +27,8 @@ describe('Json Component', () => {
2727 [ 1 , 'foo' , null ] ,
2828 Array . from ( { length : 101 } , ( _ , i ) => i ) ,
2929 ] ) ( 'collapses any array' , ( array ) => {
30- const { queryByText } = render ( < Json json = { array } /> )
31- expect ( queryByText ( '▶' ) ) . toBeTruthy ( )
32- expect ( queryByText ( '▼' ) ) . toBeNull ( )
30+ const { getByRole } = render ( < Json json = { array } /> )
31+ expect ( getByRole ( 'treeitem' ) . ariaExpanded ) . toBe ( 'false' )
3332 } )
3433
3534 it . for ( [
@@ -40,13 +39,12 @@ describe('Json Component', () => {
4039 ] ) ( 'shows short arrays of primitive items, without trailing comment about length' , ( array ) => {
4140 const { queryByText } = render ( < Json json = { array } /> )
4241 expect ( queryByText ( '...' ) ) . toBeNull ( )
43- expect ( queryByText ( ' length' ) ) . toBeNull ( )
42+ expect ( queryByText ( / l e n g t h / ) ) . toBeNull ( )
4443 } )
4544
4645 it . for ( [
47- // [1, 'foo', [1, 2, 3]], // TODO(SL): this one does not collapses, what to do? The text is wrong
4846 Array . from ( { length : 101 } , ( _ , i ) => i ) ,
49- ] ) ( 'hides long arrays, and non-primitive items, with trailing comment about length' , ( array ) => {
47+ ] ) ( 'hides long arrays with trailing comment about length' , ( array ) => {
5048 const { getByText } = render ( < Json json = { array } /> )
5149 getByText ( '...' )
5250 getByText ( / l e n g t h / )
@@ -66,20 +64,39 @@ describe('Json Component', () => {
6664 getByText ( '"42"' )
6765 } )
6866
67+ it . for ( [
68+ [ 1 , 'foo' , [ 1 , 2 , 3 ] ] ,
69+ [ 1 , 'foo' , { nested : true } ] ,
70+ ] ) ( 'expands short arrays with non-primitive values' , ( arr ) => {
71+ const { getAllByRole } = render ( < Json json = { arr } /> )
72+ const treeItems = getAllByRole ( 'treeitem' )
73+ expect ( treeItems . length ) . toBe ( 2 )
74+ expect ( treeItems [ 0 ] ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) // the root
75+ expect ( treeItems [ 1 ] ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) // the non-primitive value (object/array)
76+ } )
77+
6978 it . for ( [
7079 { obj : [ 314 , null ] } ,
7180 { obj : { nested : true } } ,
7281 ] ) ( 'expands short objects with non-primitive values' , ( obj ) => {
73- const { getByText } = render ( < Json json = { obj } /> )
74- getByText ( '▼' )
82+ const { getAllByRole } = render ( < Json json = { obj } /> )
83+ const treeItems = getAllByRole ( 'treeitem' )
84+ expect ( treeItems . length ) . toBe ( 2 )
85+ expect ( treeItems [ 0 ] ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) // the root
86+ expect ( treeItems [ 1 ] ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) // the non-primitive value (object/array)
7587 } )
7688
7789 it . for ( [
7890 { obj : [ 314 , null ] } ,
7991 { obj : { nested : true } } ,
8092 ] ) ( 'hides the content and append number of entries when objects with non-primitive values are collapsed' , ( obj ) => {
81- const { getByText } = render ( < Json json = { obj } /> )
82- fireEvent . click ( getByText ( '▼' ) )
93+ const { getAllByRole, getByText } = render ( < Json json = { obj } /> )
94+ const root = getAllByRole ( 'treeitem' ) [ 0 ]
95+ if ( ! root ) { /* type assertion, getAllByRole would already have thrown */
96+ throw new Error ( 'No root element found' )
97+ }
98+ fireEvent . click ( root )
99+ expect ( root . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' )
83100 getByText ( '...' )
84101 getByText ( / e n t r i e s / )
85102 } )
@@ -90,9 +107,8 @@ describe('Json Component', () => {
90107 { a : 1 , b : true , c : null , d : undefined } ,
91108 Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key${ i } ` , { nested : true } ] ) ) ,
92109 ] ) ( 'collapses long objects, or objects with only primitive values (included empty object)' , ( obj ) => {
93- const { queryByText } = render ( < Json json = { obj } /> )
94- expect ( queryByText ( '▶' ) ) . toBeTruthy ( )
95- expect ( queryByText ( '▼' ) ) . toBeNull ( )
110+ const { getByRole } = render ( < Json json = { obj } /> )
111+ expect ( getByRole ( 'treeitem' ) . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' )
96112 } )
97113
98114 it . for ( [
@@ -105,21 +121,23 @@ describe('Json Component', () => {
105121
106122 it ( 'toggles array collapse state' , ( ) => {
107123 const longArray = Array . from ( { length : 101 } , ( _ , i ) => i )
108- const { getByText, queryByText } = render ( < Json json = { longArray } /> )
124+ const { getByRole, getByText, queryByText } = render ( < Json json = { longArray } /> )
125+ const treeItem = getByRole ( 'treeitem' )
109126 getByText ( '...' )
110- fireEvent . click ( getByText ( '▶' ) )
127+ fireEvent . click ( treeItem )
111128 expect ( queryByText ( '...' ) ) . toBeNull ( )
112- fireEvent . click ( getByText ( '▼' ) )
129+ fireEvent . click ( treeItem )
113130 getByText ( '...' )
114131 } )
115132
116133 it ( 'toggles object collapse state' , ( ) => {
117134 const longObject = Object . fromEntries ( Array . from ( { length : 101 } , ( _ , i ) => [ `key${ i } ` , { nested : true } ] ) )
118- const { getByText, queryByText } = render ( < Json json = { longObject } /> )
135+ const { getByRole, getByText, queryByText } = render ( < Json json = { longObject } /> )
136+ const treeItem = getByRole ( 'treeitem' ) // only one treeitem because the inner objects are collapsed and not represented as treeitems
119137 getByText ( '...' )
120- fireEvent . click ( getByText ( '▶' ) )
138+ fireEvent . click ( treeItem )
121139 expect ( queryByText ( '...' ) ) . toBeNull ( )
122- fireEvent . click ( getByText ( '▼' ) )
140+ fireEvent . click ( treeItem )
123141 getByText ( '...' )
124142 } )
125143} )
0 commit comments