Skip to content

Commit 0ced684

Browse files
committed
Simplifies structuring for top-level style object
1 parent 8a215f9 commit 0ced684

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"coverageThreshold": {
3030
"global": {
3131
"statements": 90,
32-
"branches": 83,
32+
"branches": 82,
3333
"functions": 90,
3434
"lines": 90
3535
}

src/Node/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,20 @@ export default class Node extends React.Component {
6666

6767
render() {
6868
const { nodeData, styles } = this.props;
69-
const nodeStyle = nodeData._children ?
70-
{ ...this.state.initialStyle, ...styles.node } :
71-
{ ...this.state.initialStyle, ...styles.leafNode };
72-
69+
const nodeStyle = nodeData._children ? { ...styles.node } : { ...styles.leafNode };
7370
return (
7471
<g
7572
id={nodeData.id}
7673
ref={(n) => { this.node = n; }}
77-
style={nodeStyle}
74+
style={this.state.initialStyle}
7875
className={nodeData._children ? 'nodeBase' : 'leafNodeBase'}
7976
transform={this.state.transform}
8077
onClick={this.handleClick}
8178
>
8279
<text
8380
className="nodeNameBase"
8481
textAnchor={this.props.textAnchor}
85-
style={styles.name}
82+
style={nodeStyle.name}
8683
x="10"
8784
y="-10"
8885
dy=".35em"
@@ -92,14 +89,14 @@ export default class Node extends React.Component {
9289

9390
<circle
9491
r={this.props.circleRadius}
95-
style={nodeData._children ? styles.circle : styles.leafCircle}
92+
style={nodeStyle.circle}
9693
/>
9794

9895
<text
9996
className="nodeAttributesBase"
10097
y="0"
10198
textAnchor={this.props.textAnchor}
102-
style={styles.attributes}
99+
style={nodeStyle.attributes}
103100
>
104101
{
105102
this.props.attributes &&

src/Node/tests/index.test.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ describe('<Node />', () => {
5757
});
5858

5959

60-
it('applies correct node styles depending on `nodeData._children`', () => {
61-
const initialStyle = { opacity: 0 }; // state.initialStyle
60+
it('applies correct <circle> styles depending on `nodeData._children`', () => {
6261
const fixture = {
63-
node: { ...initialStyle, fill: 'blue' },
64-
leafNode: { ...initialStyle, fill: 'green' },
62+
leafNode: {
63+
circle: { fill: 'blue' },
64+
},
65+
node: {
66+
circle: { fill: 'green' },
67+
},
6568
};
6669
const leafNodeComponent = shallow(
6770
<Node
@@ -77,49 +80,70 @@ describe('<Node />', () => {
7780
/>
7881
);
7982

80-
expect(leafNodeComponent.prop('style')).toEqual(fixture.leafNode);
81-
expect(nodeComponent.prop('style')).toEqual(fixture.node);
83+
expect(leafNodeComponent.find('circle').prop('style'))
84+
.toBe(fixture.leafNode.circle);
85+
expect(nodeComponent.find('circle').prop('style'))
86+
.toBe(fixture.node.circle);
8287
});
8388

8489

85-
it('applies correct <circle> styles depending on `nodeData._children`', () => {
86-
const leafCircle = { fill: 'blue' };
87-
const circle = { fill: 'green' };
88-
const styles = { circle, leafCircle };
90+
it('applies correct node name styles depending on `nodeData._children`', () => {
91+
const fixture = {
92+
node: {
93+
name: { stroke: '#000' },
94+
},
95+
leafNode: {
96+
name: { stroke: '#fff' },
97+
},
98+
};
8999
const leafNodeComponent = shallow(
90100
<Node
91101
{...mockProps}
92-
styles={styles}
102+
styles={fixture}
93103
/>
94104
);
95105
const nodeComponent = shallow(
96106
<Node
97107
{...mockProps}
98108
nodeData={{ ...nodeData, _children: [] }}
99-
styles={styles}
109+
styles={fixture}
100110
/>
101111
);
102112

103-
expect(leafNodeComponent.find('circle').prop('style')).toBe(leafCircle);
104-
expect(nodeComponent.find('circle').prop('style')).toBe(circle);
113+
expect(leafNodeComponent.find('.nodeNameBase').prop('style'))
114+
.toBe(fixture.leafNode.name);
115+
expect(nodeComponent.find('.nodeNameBase').prop('style'))
116+
.toBe(fixture.node.name);
105117
});
106118

107119

108-
it('applies correct node attributes styles', () => {
120+
it('applies correct node attributes styles depending on `nodeData._children`', () => {
109121
const fixture = {
110-
attributes: {
111-
stroke: '#000',
112-
strokeWidth: 12,
122+
node: {
123+
attributes: { stroke: '#000' },
124+
},
125+
leafNode: {
126+
attributes: { stroke: '#fff' },
113127
},
114128
};
115-
const renderedComponent = shallow(
129+
const leafNodeComponent = shallow(
116130
<Node
117131
{...mockProps}
118132
styles={fixture}
119133
/>
120134
);
135+
const nodeComponent = shallow(
136+
<Node
137+
{...mockProps}
138+
nodeData={{ ...nodeData, _children: [] }}
139+
styles={fixture}
140+
/>
141+
);
121142

122-
expect(renderedComponent.find('.nodeAttributesBase').prop('style')).toBe(fixture.attributes);
143+
expect(leafNodeComponent.find('.nodeAttributesBase').prop('style'))
144+
.toBe(fixture.leafNode.attributes);
145+
expect(nodeComponent.find('.nodeAttributesBase').prop('style'))
146+
.toBe(fixture.node.attributes);
123147
});
124148

125149

@@ -195,7 +219,8 @@ describe('<Node />', () => {
195219
/>
196220
);
197221

198-
expect(renderedComponent.instance().applyTransform).toHaveBeenCalledWith(fixture);
222+
expect(renderedComponent.instance().applyTransform)
223+
.toHaveBeenCalledWith(fixture);
199224
});
200225

201226

@@ -217,9 +242,13 @@ describe('<Node />', () => {
217242
/>
218243
);
219244

220-
expect(renderedComponent.instance().applyTransform).toHaveBeenCalledWith(initialTransform);
245+
expect(renderedComponent.instance().applyTransform)
246+
.toHaveBeenCalledWith(initialTransform);
247+
221248
renderedComponent.setProps(updatedProps);
222-
expect(renderedComponent.instance().applyTransform).toHaveBeenCalledWith(updatedTransform);
249+
250+
expect(renderedComponent.instance().applyTransform)
251+
.toHaveBeenCalledWith(updatedTransform);
223252
});
224253

225254
// TODO Find a way to meaningfully test `componentWillLeave`

src/Tree/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,18 @@ Tree.defaultProps = {
285285
zoomable: true,
286286
scaleExtent: { min: 0.1, max: 1 },
287287
styles: {
288-
nodes: {},
288+
nodes: {
289+
node: {
290+
circle: {},
291+
name: {},
292+
attributes: {},
293+
},
294+
leafNode: {
295+
circle: {},
296+
name: {},
297+
attributes: {},
298+
},
299+
},
289300
links: {},
290301
},
291302
};

0 commit comments

Comments
 (0)