Skip to content

Commit e0dfc23

Browse files
committed
Update Node tests for transitions
1 parent 0cdbabc commit e0dfc23

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

src/Node/tests/index.test.js

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { shallow } from 'enzyme';
2+
import { shallow, mount } from 'enzyme';
33

44
import Node from '../index';
55

@@ -8,19 +8,24 @@ describe('<Node />', () => {
88
id: 'abc123',
99
name: 'mockNode',
1010
depth: 3,
11-
x: 123,
12-
y: 321,
11+
x: 111,
12+
y: 222,
13+
parent: {
14+
x: 999,
15+
y: 888,
16+
},
1317
};
1418

1519
const mockProps = {
1620
nodeData,
1721
orientation: 'horizontal',
18-
transitions: {
19-
enabled: true,
20-
duration: 500,
21-
},
22+
transitionDuration: 500,
23+
onClick: () => {},
2224
};
2325

26+
// Clear method spies on prototype after each test
27+
afterEach(() => jest.clearAllMocks());
28+
2429
it('has the correct `id` attribute value', () => {
2530
const renderedComponent = shallow(
2631
<Node {...mockProps} />
@@ -67,9 +72,9 @@ describe('<Node />', () => {
6772
expect(withChildrenComponent.find('circle').prop('style')).toBe(circleStyle);
6873
});
6974

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})`;
7378
const horizontalComponent = shallow(
7479
<Node {...mockProps} />
7580
);
@@ -79,9 +84,8 @@ describe('<Node />', () => {
7984
orientation="vertical"
8085
/>
8186
);
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);
8589
});
8690

8791
it('should take an `onClick` prop', () => {
@@ -95,7 +99,7 @@ describe('<Node />', () => {
9599
expect(renderedComponent.prop('onClick')).toBeDefined();
96100
});
97101

98-
it('handles click events and pass the nodeId to onClick handler', () => {
102+
it('handles click events and passes its nodeId to onClick handler', () => {
99103
const onClickSpy = jest.fn();
100104
const renderedComponent = shallow(
101105
<Node
@@ -128,7 +132,7 @@ describe('<Node />', () => {
128132
).toBe(1);
129133
});
130134

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', () => {
132136
const depthFactor = 100;
133137
const expectedY = nodeData.depth * depthFactor;
134138
const renderedComponent = shallow(
@@ -139,6 +143,56 @@ describe('<Node />', () => {
139143
/>
140144
);
141145

142-
expect(renderedComponent.prop('transform')).toBe(`translate(${nodeData.x},${expectedY})`);
146+
expect(renderedComponent.prop('transform')).toBe(`translate(${nodeData.parent.x},${expectedY})`);
143147
});
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+
// });
144198
});

0 commit comments

Comments
 (0)