Skip to content

Commit ff80fe5

Browse files
committed
Updates Tree tests
1 parent e04bfc2 commit ff80fe5

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

src/Tree/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ export default class Tree extends React.Component {
1414
super(props);
1515
this.state = {
1616
initialRender: true,
17-
data: this.assignInternalProperties(clone(this.props.data)),
17+
data: this.assignInternalProperties(clone(props.data)),
1818
zoom: undefined,
1919
};
2020
this.findNodesById = this.findNodesById.bind(this);
2121
this.collapseNode = this.collapseNode.bind(this);
2222
this.handleNodeToggle = this.handleNodeToggle.bind(this);
2323
}
2424

25+
2526
componentDidMount() {
2627
this.bindZoomListener();
2728

2829
// TODO find better way of setting initialDepth, re-render here is suboptimal
2930
this.setState({ initialRender: false }); // eslint-disable-line
3031
}
3132

33+
3234
componentWillReceiveProps(nextProps) {
3335
// Clone new data & assign internal properties
3436
if (this.props.data !== nextProps.data) {

src/Tree/tests/index.test.js

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,48 @@ describe('<Tree />', () => {
3434
},
3535
];
3636

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
3868
afterEach(() => jest.clearAllMocks());
3969

70+
4071
it('builds a tree on each render', () => {
41-
jest.spyOn(Tree.prototype, 'generateTree');
4272
const renderedComponent = shallow(
4373
<Tree data={mockData} />
4474
);
4575
expect(renderedComponent.instance().generateTree).toHaveBeenCalled();
4676
});
4777

78+
4879
it('maps every node onto a <Node />', () => {
4980
const nodeCount = 3; // 1 top level node + 2 child nodes in mockData
5081
const renderedComponent = shallow(
@@ -54,6 +85,7 @@ describe('<Tree />', () => {
5485
expect(renderedComponent.find(Node).length).toBe(nodeCount);
5586
});
5687

88+
5789
it('maps every parent-child relation onto a <Link />', () => {
5890
const linkCount = 2;
5991
const renderedComponent = shallow(
@@ -63,6 +95,28 @@ describe('<Tree />', () => {
6395
expect(renderedComponent.find(Link).length).toBe(linkCount);
6496
});
6597

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+
66120
it('applies the `translate` prop when specified', () => {
67121
const fixture = { x: 123, y: 321 };
68122
const expected = `translate(${fixture.x},${fixture.y})`;
@@ -75,6 +129,7 @@ describe('<Tree />', () => {
75129
expect(renderedComponent.find(TransitionGroup).prop('transform')).toBe(expected);
76130
});
77131

132+
78133
it('passes `props.orientation` to its <Node /> and <Link /> children', () => {
79134
const fixture = 'vertical';
80135
const renderedComponent = shallow(
@@ -92,6 +147,7 @@ describe('<Tree />', () => {
92147
).toBe(true);
93148
});
94149

150+
95151
it('passes `handleNodeToggle()` to its <Node /> children as onClick prop', () => {
96152
const renderedComponent = shallow(
97153
<Tree data={mockData} />
@@ -102,9 +158,8 @@ describe('<Tree />', () => {
102158
).toBe(true);
103159
});
104160

161+
105162
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');
108163
const renderedComponent = mount(
109164
<Tree data={mockData} />
110165
);
@@ -114,10 +169,8 @@ describe('<Tree />', () => {
114169
expect(Tree.prototype.collapseNode).toHaveBeenCalled();
115170
});
116171

172+
117173
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');
121174
const renderedComponent = mount(
122175
<Tree data={mockData} />
123176
);
@@ -129,9 +182,8 @@ describe('<Tree />', () => {
129182
expect(Tree.prototype.expandNode).toHaveBeenCalled();
130183
});
131184

185+
132186
it('does not collapse a node if `props.collapsible` is false', () => {
133-
jest.spyOn(Tree.prototype, 'handleNodeToggle');
134-
jest.spyOn(Tree.prototype, 'collapseNode');
135187
const renderedComponent = mount(
136188
<Tree
137189
data={mockData}
@@ -144,8 +196,8 @@ describe('<Tree />', () => {
144196
expect(Tree.prototype.collapseNode).toHaveBeenCalledTimes(0);
145197
});
146198

199+
147200
it('sets tree depth to `props.initialDepth` if specified', () => {
148-
jest.spyOn(Tree.prototype, 'setInitialTreeDepth');
149201
mount(
150202
<Tree
151203
data={mockData}
@@ -156,6 +208,7 @@ describe('<Tree />', () => {
156208
expect(Tree.prototype.setInitialTreeDepth).toHaveBeenCalled();
157209
});
158210

211+
159212
// it('allows zooming in/out according to `props.scaleExtent` if `props.zoomable`', () => {
160213
// const zoomableComponent = mount(
161214
// <Tree

0 commit comments

Comments
 (0)