@pie-lib/plot 2.7.4-next.0 → 2.8.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`gridDraggable snapshot render with decimals 1`] = `
4
+ <mockConstructor
5
+ axis="both"
6
+ grid={
7
+ Array [
8
+ 1,
9
+ 1,
10
+ ]
11
+ }
12
+ onDrag={[Function]}
13
+ onMouseDown={[Function]}
14
+ onStart={[Function]}
15
+ onStop={[Function]}
16
+ >
17
+ <Component
18
+ domain={
19
+ Object {
20
+ "max": 1.6,
21
+ "min": -1.5,
22
+ "step": 0.3,
23
+ }
24
+ }
25
+ graphProps={
26
+ Object {
27
+ "domain": Object {
28
+ "max": 1,
29
+ "min": 0,
30
+ "step": 1,
31
+ },
32
+ "getRootNode": [Function],
33
+ "range": Object {
34
+ "max": 1,
35
+ "min": 0,
36
+ "step": 1,
37
+ },
38
+ "scale": Object {
39
+ "x": [MockFunction] {
40
+ "calls": Array [
41
+ Array [
42
+ 1,
43
+ ],
44
+ Array [
45
+ 0,
46
+ ],
47
+ ],
48
+ "results": Array [
49
+ Object {
50
+ "type": "return",
51
+ "value": 1,
52
+ },
53
+ Object {
54
+ "type": "return",
55
+ "value": 0,
56
+ },
57
+ ],
58
+ },
59
+ "y": [MockFunction] {
60
+ "calls": Array [
61
+ Array [
62
+ 1,
63
+ ],
64
+ Array [
65
+ 0,
66
+ ],
67
+ ],
68
+ "results": Array [
69
+ Object {
70
+ "type": "return",
71
+ "value": 1,
72
+ },
73
+ Object {
74
+ "type": "return",
75
+ "value": 0,
76
+ },
77
+ ],
78
+ },
79
+ },
80
+ "size": Object {
81
+ "height": 500,
82
+ "width": 500,
83
+ },
84
+ "snap": Object {
85
+ "x": [MockFunction],
86
+ "y": [MockFunction],
87
+ },
88
+ }
89
+ }
90
+ isDragging={false}
91
+ range={
92
+ Object {
93
+ "max": 3,
94
+ "min": -2,
95
+ "step": 0.2,
96
+ }
97
+ }
98
+ />
99
+ </mockConstructor>
100
+ `;
101
+
102
+ exports[`gridDraggable snapshot reqular 1`] = `
103
+ <mockConstructor
104
+ axis="both"
105
+ grid={
106
+ Array [
107
+ 1,
108
+ 1,
109
+ ]
110
+ }
111
+ onDrag={[Function]}
112
+ onMouseDown={[Function]}
113
+ onStart={[Function]}
114
+ onStop={[Function]}
115
+ >
116
+ <Component
117
+ graphProps={
118
+ Object {
119
+ "domain": Object {
120
+ "max": 1,
121
+ "min": 0,
122
+ "step": 1,
123
+ },
124
+ "getRootNode": [Function],
125
+ "range": Object {
126
+ "max": 1,
127
+ "min": 0,
128
+ "step": 1,
129
+ },
130
+ "scale": Object {
131
+ "x": [MockFunction] {
132
+ "calls": Array [
133
+ Array [
134
+ 1,
135
+ ],
136
+ Array [
137
+ 0,
138
+ ],
139
+ ],
140
+ "results": Array [
141
+ Object {
142
+ "type": "return",
143
+ "value": 1,
144
+ },
145
+ Object {
146
+ "type": "return",
147
+ "value": 0,
148
+ },
149
+ ],
150
+ },
151
+ "y": [MockFunction] {
152
+ "calls": Array [
153
+ Array [
154
+ 1,
155
+ ],
156
+ Array [
157
+ 0,
158
+ ],
159
+ ],
160
+ "results": Array [
161
+ Object {
162
+ "type": "return",
163
+ "value": 1,
164
+ },
165
+ Object {
166
+ "type": "return",
167
+ "value": 0,
168
+ },
169
+ ],
170
+ },
171
+ },
172
+ "size": Object {
173
+ "height": 500,
174
+ "width": 500,
175
+ },
176
+ "snap": Object {
177
+ "x": [MockFunction],
178
+ "y": [MockFunction],
179
+ },
180
+ }
181
+ }
182
+ isDragging={false}
183
+ />
184
+ </mockConstructor>
185
+ `;
@@ -0,0 +1,18 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`root snapshot matches 1`] = `
4
+ <div>
5
+ <div>
6
+ <svg
7
+ height={480}
8
+ width={540}
9
+ >
10
+ <g
11
+ transform="translate(70, 40)"
12
+ >
13
+ hi
14
+ </g>
15
+ </svg>
16
+ </div>
17
+ </div>
18
+ `;
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import { shallow } from 'enzyme';
3
+ import Draggable from '../draggable';
4
+
5
+ const wrapper = () => {
6
+ return shallow(
7
+ <Draggable>
8
+ <div>hellow</div>
9
+ </Draggable>,
10
+ { disableLifecycleMethods: true },
11
+ );
12
+ };
13
+
14
+ describe('draggable', () => {
15
+ describe('local', () => {
16
+ it('resets x/y in state', () => {
17
+ const w = wrapper();
18
+ w.setState({ x: 1, y: 1 });
19
+ w.instance().componentWillReceiveProps({});
20
+ expect(w.state()).toMatchObject({ x: 0, y: 0 });
21
+ });
22
+ });
23
+ });
@@ -0,0 +1,326 @@
1
+ import { shallow } from 'enzyme';
2
+ import React from 'react';
3
+ import { gridDraggable } from '../grid-draggable';
4
+ import { getDelta } from '../utils';
5
+
6
+ import { clientPoint } from 'd3-selection';
7
+
8
+ jest.mock('d3-selection', () => ({
9
+ clientPoint: jest.fn().mockReturnValue([0, 0]),
10
+ }));
11
+
12
+ jest.mock('../draggable', () => ({
13
+ DraggableCore: jest.fn((type, props, children) => children),
14
+ }));
15
+
16
+ jest.mock('../utils', () => ({
17
+ getDelta: jest.fn(),
18
+ }));
19
+
20
+ const xyFn = () => {
21
+ const out = jest.fn((n) => n);
22
+ out.invert = jest.fn((n) => n);
23
+ return out;
24
+ };
25
+ const getGraphProps = () => ({
26
+ scale: {
27
+ x: xyFn(),
28
+ y: xyFn(),
29
+ },
30
+ snap: {
31
+ x: xyFn(),
32
+ y: xyFn(),
33
+ },
34
+ domain: {
35
+ min: 0,
36
+ max: 1,
37
+ step: 1,
38
+ },
39
+ range: {
40
+ min: 0,
41
+ max: 1,
42
+ step: 1,
43
+ },
44
+ size: {
45
+ width: 500,
46
+ height: 500,
47
+ },
48
+ getRootNode: () => ({}),
49
+ });
50
+
51
+ describe('gridDraggable', () => {
52
+ const wrapper = (opts, extras) => {
53
+ const defaults = {
54
+ graphProps: getGraphProps(),
55
+ };
56
+
57
+ defaults.graphProps.scale.x.invert = jest.fn((x) => x);
58
+ defaults.graphProps.scale.y.invert = jest.fn((x) => x);
59
+
60
+ const props = { ...defaults, ...extras };
61
+
62
+ opts = {
63
+ anchorPoint: jest.fn().mockReturnValue({ x: 0, y: 0 }),
64
+ bounds: jest.fn().mockReturnValue({ left: 0, top: 0, bottom: 0, right: 0 }),
65
+ fromDelta: jest.fn(),
66
+ ...opts,
67
+ };
68
+
69
+ const Comp = gridDraggable(opts)(() => <div />);
70
+ return shallow(<Comp {...props} />);
71
+ };
72
+
73
+ describe('snapshot', () => {
74
+ it('reqular', () => {
75
+ const w = wrapper();
76
+ expect(w).toMatchSnapshot();
77
+ });
78
+
79
+ it('render with decimals', () => {
80
+ const w = wrapper(
81
+ {},
82
+ {
83
+ domain: {
84
+ min: -1.5,
85
+ max: 1.6,
86
+ step: 0.3,
87
+ },
88
+ range: {
89
+ min: -2,
90
+ max: 3,
91
+ step: 0.2,
92
+ },
93
+ },
94
+ );
95
+ expect(w).toMatchSnapshot();
96
+ });
97
+ });
98
+
99
+ describe('logic', () => {
100
+ describe('grid', () => {
101
+ it('returns the grid', () => {
102
+ const w = wrapper();
103
+ const g = w.instance().grid();
104
+ expect(g).toEqual({ x: 1, y: 1 });
105
+ });
106
+ });
107
+
108
+ describe('onStart', () => {
109
+ it('sets the drag state', () => {
110
+ const w = wrapper();
111
+ w.instance().onStart({ clientX: 100, clientY: 100 });
112
+ expect(w.state().startX).toEqual(100);
113
+ expect(w.state().startY).toEqual(100);
114
+ });
115
+
116
+ it('calls the handler', () => {
117
+ const onDragStart = jest.fn();
118
+ const w = wrapper(
119
+ {},
120
+ {
121
+ onDragStart,
122
+ },
123
+ );
124
+ w.instance().onStart({ clientX: 100, clientY: 100 });
125
+ expect(onDragStart).toHaveBeenCalled();
126
+ });
127
+ });
128
+
129
+ describe('position', () => {
130
+ it('returns position object', () => {
131
+ const w = wrapper();
132
+ const pos = w.instance().position();
133
+
134
+ const anchorPoint = {
135
+ x: 0,
136
+ y: 0,
137
+ };
138
+ expect(pos).toEqual({
139
+ anchorPoint,
140
+ x: expect.any(Function),
141
+ y: expect.any(Function),
142
+ });
143
+ });
144
+ });
145
+
146
+ describe('tiny', () => {
147
+ it('returns true for 10 ', () => {
148
+ const w = wrapper();
149
+ w.setState({ startX: 0 });
150
+ const result = w.instance().tiny('x', { clientX: 10 });
151
+ expect(result).toBe(false);
152
+ });
153
+ it('returns true for 0.01', () => {
154
+ const w = wrapper();
155
+ w.setState({ startX: 0 });
156
+ const result = w.instance().tiny('x', { clientX: 0.01 });
157
+ expect(result).toBe(true);
158
+ });
159
+ });
160
+
161
+ describe('onDrag', () => {
162
+ let onDrag, w;
163
+
164
+ beforeEach(() => {
165
+ onDrag = jest.fn();
166
+ w = wrapper({}, { onDrag });
167
+ w.instance().applyDelta = jest.fn().mockReturnValue(0);
168
+ w.instance().props.graphProps.getRootNode = jest.fn().mockReturnValue({
169
+ ownerSVGElement: null,
170
+ getBoundingClientRect: jest.fn(() => ({
171
+ left: 0,
172
+ top: 0,
173
+ right: 100,
174
+ bottom: 100,
175
+ })),
176
+ });
177
+ w.instance().getClientPoint = jest.fn().mockReturnValue([50, 50]); // Mocking getClientPoint
178
+ w.instance().onDrag({}, { x: 1, y: 1 });
179
+ });
180
+
181
+ it('calls applyDelta', () => {
182
+ expect(w.instance().applyDelta).toHaveBeenCalled();
183
+ });
184
+
185
+ it('calls callback', () => {
186
+ expect(onDrag).toHaveBeenCalledWith(0);
187
+ });
188
+
189
+ const bounds = (left, right, top, bottom) => ({ left, right, top, bottom });
190
+ describe('bounds', () => {
191
+ const assertEarlyExit = (bounds, dd) => {
192
+ it(`${JSON.stringify(bounds)}, ${dd.deltaX}, ${dd.deltaY} `, () => {
193
+ w = wrapper({}, { onDrag });
194
+ w.instance().getScaledBounds = jest.fn().mockReturnValue(bounds);
195
+ w.instance().getClientPoint = jest.fn().mockReturnValue([50, 50]);
196
+ w.instance().onDrag({}, dd);
197
+ expect(w.instance().getClientPoint).not.toHaveBeenCalled();
198
+ });
199
+ };
200
+ assertEarlyExit(bounds(0, 0, 0, 0), { deltaX: -10 });
201
+ assertEarlyExit(bounds(0, 0, 0, 0), { deltaX: 10 });
202
+ assertEarlyExit(bounds(-100, 100, 0, 0), { deltaY: -10 });
203
+ assertEarlyExit(bounds(-100, 100, -100, 0), { deltaY: 10 });
204
+ it('calls client point if it doesnt exit early bounds', () => {
205
+ w = wrapper({}, { onDrag });
206
+ w.instance().getScaledBounds = jest.fn().mockReturnValue(bounds(100, 100, 100, 100));
207
+ w.instance().getClientPoint = jest.fn().mockReturnValue([50, 50]);
208
+ w.instance().onDrag({}, { deltaX: 10 });
209
+ expect(w.instance().getClientPoint).toHaveBeenCalled(); // Asserting that getClientPoint is called
210
+ });
211
+ });
212
+ });
213
+
214
+ describe('skipDragOutsideOfBounds', () => {
215
+ let w;
216
+ const assertSkipDrag = (dd, rawXFn, rawYFn, expected) => {
217
+ rawXFn = rawXFn || ((x) => x);
218
+ rawYFn = rawYFn || ((y) => y);
219
+
220
+ it(`${dd.deltaX}, ${dd.deltaY}, ${expected}`, () => {
221
+ w = wrapper({});
222
+ const gp = getGraphProps();
223
+
224
+ const mockGetBoundingClientRect = jest.fn(() => ({
225
+ left: 0,
226
+ top: 0,
227
+ right: 100,
228
+ bottom: 100,
229
+ }));
230
+
231
+ w.instance().getClientPoint = jest.fn(() => [
232
+ rawXFn(gp.domain.min, gp.domain.max),
233
+ rawYFn(gp.range.min, gp.range.max),
234
+ ]);
235
+
236
+ const rootNode = {
237
+ ownerSVGElement: null,
238
+ getBoundingClientRect: mockGetBoundingClientRect,
239
+ };
240
+
241
+ const result = w.instance().skipDragOutsideOfBounds(dd, {}, { ...gp, getRootNode: () => rootNode });
242
+ expect(result).toEqual(expected);
243
+ });
244
+ };
245
+ assertSkipDrag(
246
+ { deltaX: 1 },
247
+ (min, max) => min - 1,
248
+ (min, max) => min,
249
+ true,
250
+ );
251
+ assertSkipDrag(
252
+ { deltaX: -1 },
253
+ (min, max) => max + 1,
254
+ (min, max) => min,
255
+ true,
256
+ );
257
+ assertSkipDrag(
258
+ { deltaY: 1 },
259
+ (min, max) => max,
260
+ (min, max) => max + 1,
261
+ true,
262
+ );
263
+ assertSkipDrag(
264
+ { deltaY: -1 },
265
+ (min, max) => max,
266
+ (min, max) => min - 1,
267
+ true,
268
+ );
269
+ assertSkipDrag(
270
+ { deltaY: 1 },
271
+ (min, max) => max,
272
+ (min, max) => max,
273
+ false,
274
+ );
275
+ assertSkipDrag(
276
+ { deltaY: -1 },
277
+ (min, max) => max,
278
+ (min, max) => min,
279
+ false,
280
+ );
281
+ });
282
+
283
+ describe('getDelta', () => {
284
+ it('calls utils.getDelta', () => {
285
+ const w = wrapper();
286
+ w.instance().position = jest.fn().mockReturnValue({
287
+ anchorPoint: {
288
+ x: 0,
289
+ y: 0,
290
+ },
291
+ x: jest.fn((x) => x),
292
+ y: jest.fn((y) => y),
293
+ });
294
+ w.instance().getDelta({ x: 1, y: 1 });
295
+ expect(getDelta).toHaveBeenCalledWith({ x: 0, y: 0 }, { x: 1, y: 1 });
296
+ });
297
+ });
298
+ describe('applyDelta', () => {
299
+ it('calls fromDelta', () => {
300
+ const fromDelta = jest.fn();
301
+ const w = wrapper({ fromDelta });
302
+ w.instance().getDelta = jest.fn();
303
+ w.instance().applyDelta({ x: 1, y: 1 });
304
+ expect(fromDelta).toHaveBeenCalledWith(expect.anything(), undefined);
305
+ });
306
+ });
307
+ describe('onStop', () => {
308
+ it('calls onDragStop', () => {
309
+ const onDragStop = jest.fn();
310
+ const w = wrapper({}, { onDragStop });
311
+ w.setState({ startX: 0, startY: 0 });
312
+ w.instance().onStop({}, {});
313
+ expect(onDragStop).toHaveBeenCalled();
314
+ });
315
+
316
+ it('calls onClick if tiny', () => {
317
+ const onClick = jest.fn();
318
+ const w = wrapper({}, { onClick });
319
+ w.instance().tiny = jest.fn().mockReturnValue(true);
320
+ clientPoint.mockReturnValue([0, 0]);
321
+ w.instance().onStop({}, {});
322
+ expect(onClick).toHaveBeenCalledWith({ x: 0, y: 0 });
323
+ });
324
+ });
325
+ });
326
+ });
@@ -0,0 +1,118 @@
1
+ import { shallow } from 'enzyme';
2
+ import React from 'react';
3
+ import { Root } from '../root';
4
+ import { select, mouse } from 'd3-selection';
5
+
6
+ const scaleMock = () => {
7
+ const fn = jest.fn((n) => n);
8
+ fn.invert = jest.fn((n) => n);
9
+ return fn;
10
+ };
11
+
12
+ const graphProps = () => ({
13
+ scale: {
14
+ x: scaleMock(),
15
+ y: scaleMock(),
16
+ },
17
+ snap: {
18
+ x: jest.fn((n) => n),
19
+ y: jest.fn((n) => n),
20
+ },
21
+ domain: {
22
+ min: 0,
23
+ max: 1,
24
+ step: 1,
25
+ },
26
+ range: {
27
+ min: 0,
28
+ max: 1,
29
+ step: 1,
30
+ },
31
+ size: {
32
+ width: 400,
33
+ height: 400,
34
+ },
35
+ });
36
+
37
+ const wrapper = (props) => {
38
+ props = {
39
+ classes: {},
40
+ graphProps: graphProps(),
41
+ ...props,
42
+ };
43
+
44
+ return shallow(<Root {...props}>hi</Root>, { disableLifecycleMethods: true });
45
+ };
46
+
47
+ jest.mock('d3-selection', () => ({
48
+ select: jest.fn(),
49
+ mouse: jest.fn(),
50
+ }));
51
+
52
+ describe('root', () => {
53
+ describe('snapshot', () => {
54
+ it('matches', () => {
55
+ const w = wrapper();
56
+ expect(w).toMatchSnapshot();
57
+ });
58
+ });
59
+
60
+ describe('logic', () => {
61
+ describe('mousemove', () => {
62
+ describe('mount/unmount', () => {
63
+ it('adds mousemove listener on compenentDidMount', () => {
64
+ const w = wrapper();
65
+ const g = {
66
+ on: jest.fn(),
67
+ };
68
+ select.mockReturnValue(g);
69
+ w.instance().componentDidMount();
70
+ expect(g.on).toHaveBeenCalledWith('mousemove', expect.any(Function));
71
+ });
72
+ it('unsets mousemove listener on componentWillUnmount', () => {
73
+ const w = wrapper();
74
+ const g = {
75
+ on: jest.fn(),
76
+ };
77
+ select.mockReturnValue(g);
78
+ w.instance().componentWillUnmount();
79
+ expect(g.on).toHaveBeenCalledWith('mousemove', null);
80
+ });
81
+ });
82
+
83
+ describe('mouseMove function', () => {
84
+ let onMouseMove, w, gp;
85
+ beforeEach(() => {
86
+ onMouseMove = jest.fn();
87
+ gp = graphProps();
88
+ w = wrapper({
89
+ onMouseMove,
90
+ graphProps: gp,
91
+ });
92
+ mouse.mockReturnValue([0, 0]);
93
+ const g = { _groups: [[[0, 0]]] };
94
+ w.instance().mouseMove(g);
95
+ });
96
+ it('calls mouse', () => {
97
+ expect(mouse).toHaveBeenCalledWith([0, 0]);
98
+ });
99
+ it('calls, scale.x.invert', () => {
100
+ expect(gp.scale.x.invert).toHaveBeenCalledWith(0);
101
+ });
102
+ it('calls, scale.y.invert', () => {
103
+ expect(gp.scale.y.invert).toHaveBeenCalledWith(0);
104
+ });
105
+ it('calls, snap.x', () => {
106
+ expect(gp.snap.x).toHaveBeenCalledWith(0);
107
+ });
108
+ it('calls, snap.y', () => {
109
+ expect(gp.snap.y).toHaveBeenCalledWith(0);
110
+ });
111
+
112
+ it('calls handler', () => {
113
+ expect(onMouseMove).toHaveBeenCalledWith({ x: 0, y: 0 });
114
+ });
115
+ });
116
+ });
117
+ });
118
+ });