11/* eslint-disable import/no-extraneous-dependencies */
2- import React from 'react' ;
2+ import React , { PropTypes } from 'react' ;
33import { mount } from 'enzyme' ;
44import jasmineEnzyme from 'jasmine-enzyme' ;
55
66import { AutoSizer } from 'react-virtualized' ;
7+ import SortableTree from './react-sortable-tree' ;
8+ import sortableTreeStyles from './react-sortable-tree.scss' ;
79import TreeNode from './tree-node' ;
10+ import treeNodeStyles from './tree-node.scss' ;
11+ import DefaultNodeRenderer from './node-renderer-default' ;
12+ import defaultNodeRendererStyles from './node-renderer-default.scss' ;
813
9- import SortableTree from './react-sortable-tree' ;
1014
1115describe ( '<SortableTree />' , ( ) => {
1216 beforeEach ( ( ) => {
@@ -105,6 +109,52 @@ describe('<SortableTree />', () => {
105109 expect ( wrapper . find ( TreeNode ) . length ) . toEqual ( 3 ) ;
106110 } ) ;
107111
112+ it ( 'should reveal hidden nodes when visibility toggled' , ( ) => {
113+ const wrapper = mount (
114+ < SortableTree
115+ treeData = { [ { title : 'a' , children : [ { title : 'b' } ] } ] }
116+ onChange = { treeData => wrapper . setProps ( { treeData } ) }
117+ />
118+ ) ;
119+
120+ // Check nodes in collapsed state
121+ expect ( wrapper . find ( TreeNode ) . length ) . toEqual ( 1 ) ;
122+
123+ // Expand node and check for the existence of the revealed child
124+ wrapper . find ( `.${ defaultNodeRendererStyles . expandButton } ` ) . first ( ) . simulate ( 'click' ) ;
125+ expect ( wrapper . find ( TreeNode ) . length ) . toEqual ( 2 ) ;
126+
127+ // Collapse node and make sure the child has been hidden
128+ wrapper . find ( `.${ defaultNodeRendererStyles . collapseButton } ` ) . first ( ) . simulate ( 'click' ) ;
129+ expect ( wrapper . find ( TreeNode ) . length ) . toEqual ( 1 ) ;
130+ } ) ;
131+
132+ it ( 'should change outer wrapper style via `style` and `className` props' , ( ) => {
133+ const wrapper = mount (
134+ < SortableTree
135+ treeData = { [ { title : 'a' } ] }
136+ onChange = { ( ) => { } }
137+ style = { { borderWidth : 42 } }
138+ className = "extra-classy"
139+ />
140+ ) ;
141+
142+ expect ( wrapper . find ( `.${ sortableTreeStyles . tree } ` ) ) . toHaveStyle ( 'borderWidth' , 42 ) ;
143+ expect ( wrapper . find ( `.${ sortableTreeStyles . tree } ` ) ) . toHaveClassName ( 'extra-classy' ) ;
144+ } ) ;
145+
146+ it ( 'should change style of scroll container with `innerStyle` prop' , ( ) => {
147+ const wrapper = mount (
148+ < SortableTree
149+ treeData = { [ { title : 'a' } ] }
150+ onChange = { ( ) => { } }
151+ innerStyle = { { borderWidth : 42 } }
152+ />
153+ ) ;
154+
155+ expect ( wrapper . find ( `.${ sortableTreeStyles . virtualScrollOverride } ` ) ) . toHaveStyle ( 'borderWidth' , 42 ) ;
156+ } ) ;
157+
108158 it ( 'should change height according to rowHeight prop' , ( ) => {
109159 const wrapper = mount (
110160 < SortableTree
@@ -116,4 +166,61 @@ describe('<SortableTree />', () => {
116166
117167 expect ( wrapper . find ( TreeNode ) ) . toHaveStyle ( 'height' , 12 ) ;
118168 } ) ;
169+
170+ it ( 'should change scaffold width according to scaffoldBlockPxWidth prop' , ( ) => {
171+ const wrapper = mount (
172+ < SortableTree
173+ treeData = { [ { title : 'a' } ] }
174+ onChange = { ( ) => { } }
175+ scaffoldBlockPxWidth = { 12 }
176+ />
177+ ) ;
178+
179+ expect ( wrapper . find ( `.${ treeNodeStyles . lineBlock } ` ) ) . toHaveStyle ( 'width' , 12 ) ;
180+ } ) ;
181+
182+ it ( 'should pass props to the node renderer from `generateNodeProps`' , ( ) => {
183+ const title = 42 ;
184+ const wrapper = mount (
185+ < SortableTree
186+ treeData = { [ { title } ] }
187+ onChange = { ( ) => { } }
188+ generateNodeProps = { ( { node } ) => ( { buttons : [ node . title ] } ) }
189+ />
190+ ) ;
191+
192+ expect ( wrapper . find ( DefaultNodeRenderer ) ) . toHaveProp ( 'buttons' , [ title ] ) ;
193+ } ) ;
194+
195+ it ( 'should call the callback in `onVisibilityToggle` when visibility toggled' , ( ) => {
196+ let out = null ;
197+
198+ const wrapper = mount (
199+ < SortableTree
200+ treeData = { [ { title : 'a' , children : [ { title : 'b' } ] } ] }
201+ onChange = { treeData => wrapper . setProps ( { treeData } ) }
202+ onVisibilityToggle = { ( { expanded } ) => { out = expanded ? 'expanded' : 'collapsed' ; } }
203+ />
204+ ) ;
205+
206+ wrapper . find ( `.${ defaultNodeRendererStyles . expandButton } ` ) . first ( ) . simulate ( 'click' ) ;
207+ expect ( out ) . toEqual ( 'expanded' ) ;
208+ wrapper . find ( `.${ defaultNodeRendererStyles . collapseButton } ` ) . first ( ) . simulate ( 'click' ) ;
209+ expect ( out ) . toEqual ( 'collapsed' ) ;
210+ } ) ;
211+
212+ it ( 'should render with a custom `nodeContentRenderer`' , ( ) => {
213+ const FakeNode = ( ( { node } ) => ( < div > { node . title } </ div > ) ) ;
214+ FakeNode . propTypes = { node : PropTypes . object . isRequired } ;
215+
216+ const wrapper = mount (
217+ < SortableTree
218+ treeData = { [ { title : 'a' } ] }
219+ onChange = { ( ) => { } }
220+ nodeContentRenderer = { FakeNode }
221+ />
222+ ) ;
223+
224+ expect ( wrapper . find ( FakeNode ) . length ) . toEqual ( 1 ) ;
225+ } ) ;
119226} ) ;
0 commit comments