@@ -34,17 +34,48 @@ describe('<Tree />', () => {
34
34
} ,
35
35
] ;
36
36
37
- // Clear method spies on prototype before each test
37
+ const mockData2 = [
38
+ {
39
+ name : 'Top Level' ,
40
+ parent : 'null' ,
41
+ attributes : {
42
+ keyA : 'val A' ,
43
+ keyB : 'val B' ,
44
+ keyC : 'val C' ,
45
+ } ,
46
+ children : [
47
+ {
48
+ name : 'Level 2: A' ,
49
+ parent : 'Top Level' ,
50
+ attributes : {
51
+ keyA : 'val A' ,
52
+ keyB : 'val B' ,
53
+ keyC : 'val C' ,
54
+ } ,
55
+ } ,
56
+ ] ,
57
+ } ,
58
+ ] ;
59
+
60
+ jest . spyOn ( Tree . prototype , 'generateTree' ) ;
61
+ jest . spyOn ( Tree . prototype , 'assignInternalProperties' ) ;
62
+ jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
63
+ jest . spyOn ( Tree . prototype , 'collapseNode' ) ;
64
+ jest . spyOn ( Tree . prototype , 'expandNode' ) ;
65
+ jest . spyOn ( Tree . prototype , 'setInitialTreeDepth' ) ;
66
+
67
+ // Clear method spies on prototype after each test
38
68
afterEach ( ( ) => jest . clearAllMocks ( ) ) ;
39
69
70
+
40
71
it ( 'builds a tree on each render' , ( ) => {
41
- jest . spyOn ( Tree . prototype , 'generateTree' ) ;
42
72
const renderedComponent = shallow (
43
73
< Tree data = { mockData } />
44
74
) ;
45
75
expect ( renderedComponent . instance ( ) . generateTree ) . toHaveBeenCalled ( ) ;
46
76
} ) ;
47
77
78
+
48
79
it ( 'maps every node onto a <Node />' , ( ) => {
49
80
const nodeCount = 3 ; // 1 top level node + 2 child nodes in mockData
50
81
const renderedComponent = shallow (
@@ -54,6 +85,7 @@ describe('<Tree />', () => {
54
85
expect ( renderedComponent . find ( Node ) . length ) . toBe ( nodeCount ) ;
55
86
} ) ;
56
87
88
+
57
89
it ( 'maps every parent-child relation onto a <Link />' , ( ) => {
58
90
const linkCount = 2 ;
59
91
const renderedComponent = shallow (
@@ -63,6 +95,28 @@ describe('<Tree />', () => {
63
95
expect ( renderedComponent . find ( Link ) . length ) . toBe ( linkCount ) ;
64
96
} ) ;
65
97
98
+
99
+ it ( 'reassigns internal props if `props.data` changes' , ( ) => {
100
+ // `assignInternalProperties` recurses by depth: 1 level -> 1 call
101
+ const mockDataDepth = 2 ;
102
+ const mockData2Depth = 2 ;
103
+ const nextProps = {
104
+ data : mockData2 ,
105
+ } ;
106
+ const renderedComponent = mount (
107
+ < Tree data = { mockData } />
108
+ ) ;
109
+
110
+ expect ( renderedComponent . instance ( ) . assignInternalProperties )
111
+ . toHaveBeenCalledTimes ( mockDataDepth ) ;
112
+
113
+ renderedComponent . setProps ( nextProps ) ;
114
+
115
+ expect ( renderedComponent . instance ( ) . assignInternalProperties )
116
+ . toHaveBeenCalledTimes ( mockDataDepth + mockData2Depth ) ;
117
+ } ) ;
118
+
119
+
66
120
it ( 'applies the `translate` prop when specified' , ( ) => {
67
121
const fixture = { x : 123 , y : 321 } ;
68
122
const expected = `translate(${ fixture . x } ,${ fixture . y } )` ;
@@ -75,6 +129,7 @@ describe('<Tree />', () => {
75
129
expect ( renderedComponent . find ( TransitionGroup ) . prop ( 'transform' ) ) . toBe ( expected ) ;
76
130
} ) ;
77
131
132
+
78
133
it ( 'passes `props.orientation` to its <Node /> and <Link /> children' , ( ) => {
79
134
const fixture = 'vertical' ;
80
135
const renderedComponent = shallow (
@@ -92,6 +147,7 @@ describe('<Tree />', () => {
92
147
) . toBe ( true ) ;
93
148
} ) ;
94
149
150
+
95
151
it ( 'passes `handleNodeToggle()` to its <Node /> children as onClick prop' , ( ) => {
96
152
const renderedComponent = shallow (
97
153
< Tree data = { mockData } />
@@ -102,9 +158,8 @@ describe('<Tree />', () => {
102
158
) . toBe ( true ) ;
103
159
} ) ;
104
160
161
+
105
162
it ( 'collapses a node\'s children when it is clicked in an expanded state' , ( ) => {
106
- jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
107
- jest . spyOn ( Tree . prototype , 'collapseNode' ) ;
108
163
const renderedComponent = mount (
109
164
< Tree data = { mockData } />
110
165
) ;
@@ -114,10 +169,8 @@ describe('<Tree />', () => {
114
169
expect ( Tree . prototype . collapseNode ) . toHaveBeenCalled ( ) ;
115
170
} ) ;
116
171
172
+
117
173
it ( 'expands a node\'s children when it is clicked in a collapsed state' , ( ) => {
118
- jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
119
- jest . spyOn ( Tree . prototype , 'collapseNode' ) ;
120
- jest . spyOn ( Tree . prototype , 'expandNode' ) ;
121
174
const renderedComponent = mount (
122
175
< Tree data = { mockData } />
123
176
) ;
@@ -129,9 +182,8 @@ describe('<Tree />', () => {
129
182
expect ( Tree . prototype . expandNode ) . toHaveBeenCalled ( ) ;
130
183
} ) ;
131
184
185
+
132
186
it ( 'does not collapse a node if `props.collapsible` is false' , ( ) => {
133
- jest . spyOn ( Tree . prototype , 'handleNodeToggle' ) ;
134
- jest . spyOn ( Tree . prototype , 'collapseNode' ) ;
135
187
const renderedComponent = mount (
136
188
< Tree
137
189
data = { mockData }
@@ -144,8 +196,8 @@ describe('<Tree />', () => {
144
196
expect ( Tree . prototype . collapseNode ) . toHaveBeenCalledTimes ( 0 ) ;
145
197
} ) ;
146
198
199
+
147
200
it ( 'sets tree depth to `props.initialDepth` if specified' , ( ) => {
148
- jest . spyOn ( Tree . prototype , 'setInitialTreeDepth' ) ;
149
201
mount (
150
202
< Tree
151
203
data = { mockData }
@@ -156,6 +208,7 @@ describe('<Tree />', () => {
156
208
expect ( Tree . prototype . setInitialTreeDepth ) . toHaveBeenCalled ( ) ;
157
209
} ) ;
158
210
211
+
159
212
// it('allows zooming in/out according to `props.scaleExtent` if `props.zoomable`', () => {
160
213
// const zoomableComponent = mount(
161
214
// <Tree
0 commit comments