1
1
import React from 'react' ;
2
- import { shallow } from 'enzyme' ;
2
+ import { shallow , mount } from 'enzyme' ;
3
3
4
4
import Node from '../index' ;
5
5
@@ -8,19 +8,24 @@ describe('<Node />', () => {
8
8
id : 'abc123' ,
9
9
name : 'mockNode' ,
10
10
depth : 3 ,
11
- x : 123 ,
12
- y : 321 ,
11
+ x : 111 ,
12
+ y : 222 ,
13
+ parent : {
14
+ x : 999 ,
15
+ y : 888 ,
16
+ } ,
13
17
} ;
14
18
15
19
const mockProps = {
16
20
nodeData,
17
21
orientation : 'horizontal' ,
18
- transitions : {
19
- enabled : true ,
20
- duration : 500 ,
21
- } ,
22
+ transitionDuration : 500 ,
23
+ onClick : ( ) => { } ,
22
24
} ;
23
25
26
+ // Clear method spies on prototype after each test
27
+ afterEach ( ( ) => jest . clearAllMocks ( ) ) ;
28
+
24
29
it ( 'has the correct `id` attribute value' , ( ) => {
25
30
const renderedComponent = shallow (
26
31
< Node { ...mockProps } />
@@ -67,9 +72,9 @@ describe('<Node />', () => {
67
72
expect ( withChildrenComponent . find ( 'circle' ) . prop ( 'style' ) ) . toBe ( circleStyle ) ;
68
73
} ) ;
69
74
70
- it ( 'applies correct `transform` prop, depending on parent `orientation`' , ( ) => {
71
- const horizontalTransform = `translate(${ nodeData . y } ,${ nodeData . x } )` ;
72
- const verticalTransform = `translate(${ nodeData . x } ,${ nodeData . y } )` ;
75
+ it ( 'applies correct `transform` prop based on its `orientation`' , ( ) => {
76
+ const horizontalTransform = `translate(${ nodeData . parent . y } ,${ nodeData . parent . x } )` ;
77
+ const verticalTransform = `translate(${ nodeData . parent . x } ,${ nodeData . parent . y } )` ;
73
78
const horizontalComponent = shallow (
74
79
< Node { ...mockProps } />
75
80
) ;
@@ -79,9 +84,8 @@ describe('<Node />', () => {
79
84
orientation = "vertical"
80
85
/>
81
86
) ;
82
-
83
- expect ( horizontalComponent . prop ( 'transform' ) ) . toBe ( horizontalTransform ) ;
84
- expect ( verticalComponent . prop ( 'transform' ) ) . toBe ( verticalTransform ) ;
87
+ expect ( horizontalComponent . find ( 'g' ) . prop ( 'transform' ) ) . toBe ( horizontalTransform ) ;
88
+ expect ( verticalComponent . find ( 'g' ) . prop ( 'transform' ) ) . toBe ( verticalTransform ) ;
85
89
} ) ;
86
90
87
91
it ( 'should take an `onClick` prop' , ( ) => {
@@ -95,7 +99,7 @@ describe('<Node />', () => {
95
99
expect ( renderedComponent . prop ( 'onClick' ) ) . toBeDefined ( ) ;
96
100
} ) ;
97
101
98
- it ( 'handles click events and pass the nodeId to onClick handler' , ( ) => {
102
+ it ( 'handles click events and passes its nodeId to onClick handler' , ( ) => {
99
103
const onClickSpy = jest . fn ( ) ;
100
104
const renderedComponent = shallow (
101
105
< Node
@@ -128,7 +132,7 @@ describe('<Node />', () => {
128
132
) . toBe ( 1 ) ;
129
133
} ) ;
130
134
131
- it ( 'mutates a node\'s `y` property according to `depthFactor`, when specified' , ( ) => {
135
+ it ( 'mutates the node\'s `y` property according to `depthFactor`, when specified' , ( ) => {
132
136
const depthFactor = 100 ;
133
137
const expectedY = nodeData . depth * depthFactor ;
134
138
const renderedComponent = shallow (
@@ -139,6 +143,56 @@ describe('<Node />', () => {
139
143
/>
140
144
) ;
141
145
142
- expect ( renderedComponent . prop ( 'transform' ) ) . toBe ( `translate(${ nodeData . x } ,${ expectedY } )` ) ;
146
+ expect ( renderedComponent . prop ( 'transform' ) ) . toBe ( `translate(${ nodeData . parent . x } ,${ expectedY } )` ) ;
143
147
} ) ;
148
+
149
+ it ( 'applies its own x/y coords on `transform` once mounted' , ( ) => {
150
+ jest . spyOn ( Node . prototype , 'applyTransform' ) ;
151
+ const fixture = `translate(${ nodeData . y } ,${ nodeData . x } )` ;
152
+ const renderedComponent = mount (
153
+ < Node
154
+ { ...mockProps }
155
+ />
156
+ ) ;
157
+
158
+ expect ( renderedComponent . instance ( ) . applyTransform ) . toHaveBeenCalledWith ( fixture ) ;
159
+ } ) ;
160
+
161
+ it ( 'updates its transform attribute if either the `x` or `y` prop changes' , ( ) => {
162
+ jest . spyOn ( Node . prototype , 'applyTransform' ) ;
163
+ const updatedProps = {
164
+ ...mockProps ,
165
+ nodeData : {
166
+ ...mockProps . nodeData ,
167
+ x : 1 ,
168
+ y : 2 ,
169
+ } ,
170
+ } ;
171
+ const initialTransform = `translate(${ mockProps . nodeData . y } ,${ mockProps . nodeData . x } )` ;
172
+ const updatedTransform = `translate(${ updatedProps . nodeData . y } ,${ updatedProps . nodeData . x } )` ;
173
+ const renderedComponent = mount (
174
+ < Node
175
+ { ...mockProps }
176
+ />
177
+ ) ;
178
+
179
+ expect ( renderedComponent . instance ( ) . applyTransform ) . toHaveBeenCalledWith ( initialTransform ) ;
180
+ renderedComponent . setProps ( updatedProps ) ;
181
+ expect ( renderedComponent . instance ( ) . applyTransform ) . toHaveBeenCalledWith ( updatedTransform ) ;
182
+ } ) ;
183
+
184
+ // TODO Find a way to meaningfully test `componentWillLeave`
185
+ // it('regresses to its parent coords when unmounting/leaving', () => {
186
+ // jest.spyOn(Node.prototype, 'applyTransform');
187
+ // const fixture = `translate(${nodeData.parent.y},${nodeData.parent.x})`;
188
+ // const renderedComponent = mount(
189
+ // <Node
190
+ // {...mockProps}
191
+ // />
192
+ // );
193
+ //
194
+ // renderedComponent.unmount();
195
+ // expect(Node.prototype.applyTransform).toHaveBeenCalledWith(fixture);
196
+ // expect(Node.prototype.applyTransform).toHaveBeenCalledTimes(1);
197
+ // });
144
198
} ) ;
0 commit comments