@@ -3,7 +3,9 @@ import PropTypes from 'prop-types';
3
3
import renderer from 'react-test-renderer' ;
4
4
import { mount } from 'enzyme' ;
5
5
import { List } from 'react-virtualized' ;
6
- import SortableTree from './react-sortable-tree' ;
6
+ import SortableTree , {
7
+ SortableTreeWithoutDndContext ,
8
+ } from './react-sortable-tree' ;
7
9
import sortableTreeStyles from './react-sortable-tree.scss' ;
8
10
import TreeNode from './tree-node' ;
9
11
import treeNodeStyles from './tree-node.scss' ;
@@ -268,4 +270,53 @@ describe('<SortableTree />', () => {
268
270
269
271
expect ( wrapper . find ( FakeNode ) . length ) . toEqual ( 1 ) ;
270
272
} ) ;
273
+
274
+ it ( 'search should call searchFinishCallback' , ( ) => {
275
+ const searchFinishCallback = jest . fn ( ) ;
276
+ mount (
277
+ < SortableTree
278
+ treeData = { [ { title : 'a' , children : [ { title : 'b' } ] } ] }
279
+ searchQuery = "b"
280
+ searchFocusOffset = { 0 }
281
+ searchFinishCallback = { searchFinishCallback }
282
+ onChange = { ( ) => { } }
283
+ />
284
+ ) ;
285
+
286
+ expect ( searchFinishCallback ) . toHaveBeenCalledWith ( [
287
+ // Node should be found expanded
288
+ { node : { title : 'b' } , path : [ 0 , 1 ] , treeIndex : 1 } ,
289
+ ] ) ;
290
+ } ) ;
291
+
292
+ it ( 'search should expand all matches and seek out the focus offset' , ( ) => {
293
+ const wrapper = mount (
294
+ < SortableTree
295
+ treeData = { [
296
+ { title : 'a' , children : [ { title : 'b' } ] } ,
297
+ { title : 'a' , children : [ { title : 'be' } ] } ,
298
+ ] }
299
+ searchQuery = "b"
300
+ onChange = { ( ) => { } }
301
+ />
302
+ ) ;
303
+
304
+ const tree = wrapper . find ( SortableTreeWithoutDndContext ) . instance ( ) ;
305
+ expect ( tree . state . searchMatches ) . toEqual ( [
306
+ { node : { title : 'b' } , path : [ 0 , 1 ] , treeIndex : 1 } ,
307
+ { node : { title : 'be' } , path : [ 2 , 3 ] , treeIndex : 3 } ,
308
+ ] ) ;
309
+ expect ( tree . state . searchFocusTreeIndex ) . toEqual ( null ) ;
310
+
311
+ wrapper . setProps ( { searchFocusOffset : 0 } ) ;
312
+ expect ( tree . state . searchFocusTreeIndex ) . toEqual ( 1 ) ;
313
+
314
+ wrapper . setProps ( { searchFocusOffset : 1 } ) ;
315
+ // As the empty `onChange` we use here doesn't actually change
316
+ // the tree, the expansion of all nodes doesn't get preserved
317
+ // after the first mount, and this change in searchFocusOffset
318
+ // only triggers the opening of a single path.
319
+ // Therefore it's 2 instead of 3.
320
+ expect ( tree . state . searchFocusTreeIndex ) . toEqual ( 2 ) ;
321
+ } ) ;
271
322
} ) ;
0 commit comments