-
Notifications
You must be signed in to change notification settings - Fork 270
/
index.test.js
181 lines (157 loc) · 6.88 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React from 'react';
import { shallow, mount } from 'enzyme';
import Link from '../index.tsx';
describe('<Link />', () => {
const linkData = {
source: {
x: 123,
y: 321,
},
target: {
x: 456,
y: 654,
},
};
const mockProps = {
linkData,
pathFunc: 'diagonal',
orientation: 'horizontal',
transitionDuration: 500,
onClick: () => {},
onMouseOver: () => {},
onMouseOut: () => {},
};
const pathFuncs = {
testPathFunc: (d, orientation) =>
orientation && `M${d.source.y},${d.source.x}V${d.target.x}H${d.target.y}`,
};
jest.spyOn(Link.prototype, 'drawPath');
jest.spyOn(Link.prototype, 'drawDiagonalPath');
jest.spyOn(Link.prototype, 'drawElbowPath');
jest.spyOn(Link.prototype, 'drawStraightPath');
jest.spyOn(Link.prototype, 'drawStepPath');
jest.spyOn(Link.prototype, 'applyOpacity');
jest.spyOn(pathFuncs, 'testPathFunc');
// Clear method spies on prototype after each test
afterEach(() => jest.clearAllMocks());
it('binds IDs of source & target nodes to data-source-id/data-target-id', () => {
linkData.source.id = 1;
linkData.target.id = 2;
const renderedComponent = shallow(<Link {...mockProps} />);
expect(renderedComponent.find('path').prop('data-source-id')).toBe(linkData.source.id);
expect(renderedComponent.find('path').prop('data-target-id')).toBe(linkData.target.id);
delete linkData.source.id;
delete linkData.target.id;
});
describe('handling classNames', () => {
it('applies the expected internal className', () => {
const renderedComponent = shallow(<Link {...mockProps} />);
expect(renderedComponent.prop('className')).toBe('rd3t-link');
});
it('applies any additional classNames returned by `pathClassFunc` if defined', () => {
const fixture = 'additionalPathClassName';
const renderedComponent = shallow(<Link {...mockProps} pathClassFunc={() => fixture} />);
expect(renderedComponent.prop('className')).toBe(['rd3t-link', fixture].join(' '));
});
});
describe('drawing paths', () => {
it('calls the appropriate path func based on `props.pathFunc`', () => {
const diagonalComponent = shallow(<Link {...mockProps} />);
const elbowComponent = shallow(<Link {...mockProps} pathFunc="elbow" />);
const straightComponent = shallow(<Link {...mockProps} pathFunc="straight" />);
const stepComponent = shallow(<Link {...mockProps} pathFunc="step" />);
shallow(<Link {...mockProps} pathFunc={pathFuncs.testPathFunc} />);
expect(diagonalComponent.instance().drawDiagonalPath).toHaveBeenCalled();
expect(elbowComponent.instance().drawElbowPath).toHaveBeenCalled();
expect(straightComponent.instance().drawStraightPath).toHaveBeenCalled();
expect(stepComponent.instance().drawStepPath).toHaveBeenCalled();
expect(pathFuncs.testPathFunc).toHaveBeenCalled();
expect(Link.prototype.drawPath).toHaveBeenCalledTimes(5);
});
it('returns an appropriate elbowPath according to `props.orientation`', () => {
expect(Link.prototype.drawElbowPath(linkData, 'horizontal')).toBe(
`M${linkData.source.y},${linkData.source.x}V${linkData.target.x}H${linkData.target.y}`
);
expect(Link.prototype.drawElbowPath(linkData, 'vertical')).toBe(
`M${linkData.source.x},${linkData.source.y}V${linkData.target.y}H${linkData.target.x}`
);
});
it('returns an appropriate diagonal according to `props.orientation`', () => {
const ymean = (linkData.target.y + linkData.source.y) / 2;
expect(Link.prototype.drawDiagonalPath(linkData, 'horizontal')).toBe(
`M${linkData.source.y},${linkData.source.x}` +
`C${ymean},${linkData.source.x},${ymean},${linkData.target.x},` +
`${linkData.target.y},${linkData.target.x}`
);
expect(Link.prototype.drawDiagonalPath(linkData, 'vertical')).toBe(
`M${linkData.source.x},${linkData.source.y}` +
`C${linkData.source.x},${ymean},${linkData.target.x},${ymean},` +
`${linkData.target.x},${linkData.target.y}`
);
});
it('returns an appropriate straightPath according to `props.orientation`', () => {
expect(Link.prototype.drawStraightPath(linkData, 'horizontal')).toBe(
`M${linkData.source.y},${linkData.source.x}L${linkData.target.y},${linkData.target.x}`
);
expect(Link.prototype.drawStraightPath(linkData, 'vertical')).toBe(
`M${linkData.source.x},${linkData.source.y}L${linkData.target.x},${linkData.target.y}`
);
});
it('return an appropriate stepPath according to `props.orientation`', () => {
const { source, target } = linkData;
const deltaY = target.y - source.y;
expect(Link.prototype.drawStepPath(linkData, 'horizontal')).toBe(
`M${source.y},${source.x} H${source.y + deltaY / 2} V${target.x} H${target.y}`
);
expect(Link.prototype.drawStepPath(linkData, 'vertical')).toBe(
`M${source.x},${source.y} V${source.y + deltaY / 2} H${target.x} V${target.y}`
);
});
});
it('fades in once it has been mounted', () => {
const fixture = 1;
const renderedComponent = mount(<Link {...mockProps} />);
expect(renderedComponent.instance().applyOpacity).toHaveBeenCalledWith(
fixture,
mockProps.transitionDuration
);
});
describe('Events', () => {
it('handles onClick events and passes its nodeId & event object to onClick handler', () => {
const onClickSpy = jest.fn();
const mockEvt = { mock: 'event' };
const renderedComponent = shallow(<Link {...mockProps} onClick={onClickSpy} />);
renderedComponent.simulate('click', mockEvt);
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith(
linkData.source,
linkData.target,
expect.objectContaining(mockEvt)
);
});
it('handles onMouseOver events and passes its nodeId & event object to onMouseOver handler', () => {
const onMouseOverSpy = jest.fn();
const mockEvt = { mock: 'event' };
const renderedComponent = shallow(<Link {...mockProps} onMouseOver={onMouseOverSpy} />);
renderedComponent.simulate('mouseover', mockEvt);
expect(onMouseOverSpy).toHaveBeenCalledTimes(1);
expect(onMouseOverSpy).toHaveBeenCalledWith(
linkData.source,
linkData.target,
expect.objectContaining(mockEvt)
);
});
it('handles onMouseOut events and passes its nodeId & event object to onMouseOut handler', () => {
const onMouseOutSpy = jest.fn();
const mockEvt = { mock: 'event' };
const renderedComponent = shallow(<Link {...mockProps} onMouseOut={onMouseOutSpy} />);
renderedComponent.simulate('mouseout', mockEvt);
expect(onMouseOutSpy).toHaveBeenCalledTimes(1);
expect(onMouseOutSpy).toHaveBeenCalledWith(
linkData.source,
linkData.target,
expect.objectContaining(mockEvt)
);
});
});
});