This repository has been archived by the owner on Feb 20, 2018. It is now read-only.
/
helloworld.spec.js
executable file
·476 lines (394 loc) · 15.6 KB
/
helloworld.spec.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import React from 'react';
import { shallow, mount } from 'enzyme';
import assert from 'power-assert';
import Balloon from '../src/index';
describe('<Balloon/>', () => {
it('should have default properties', () => {
const balloon = mount(<Balloon />);
assert.equal(balloon.props().zIndex, 100);
assert.equal(balloon.props().className, '');
assert.equal(balloon.props().backgroundColor, '#f5f5f5');
assert.equal(balloon.props().minWidth, undefined);
assert.equal(balloon.props().minHeight, undefined);
assert.equal(balloon.props().maxWidth, undefined);
assert.equal(balloon.props().maxHeight, undefined);
assert.deepEqual(balloon.props().style, {});
assert.deepEqual(balloon.props().start, {
box: {
x: 0,
y: 0,
width: 100,
height: 100,
},
destination: {
x: 0,
y: 0,
},
});
});
it('Should root element have expected element and styles', () => {
const balloon = shallow(<Balloon />);
const expectedStyles = {width: '100%', height: '100%', zIndex: 100};
assert.equal(balloon.type(), 'div');
assert.deepEqual(balloon.prop('style'), expectedStyles);
});
//it('Shoud have style with color red', () => {
// const wrapper = shallow(<Hello />);
// const expectedStyles = {
// color: 'red'
// };
// assert.deepEqual(wrapper.prop('style'), expectedStyles);
//});
//
// it('Should contain string, Hello, world', () => {
// const wrapper = shallow(<Hello />);
// assert(wrapper.contains('Hello, world'));
// });
});
/*
describe('props', function () {
it('should have default properties', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
expect(drag.props.axis).toEqual('both');
expect(drag.props.handle).toEqual(null);
expect(drag.props.cancel).toEqual(null);
expect(drag.props.bounds).toBeFalsy();
expect(isNaN(drag.props.zIndex)).toEqual(true);
expect(typeof drag.props.onStart).toEqual('function');
expect(typeof drag.props.onDrag).toEqual('function');
expect(typeof drag.props.onStop).toEqual('function');
});
it('should pass style and className properly from child', function () {
drag = (<Draggable><div className="foo" style={{color: 'black'}}/></Draggable>);
expect(renderToHTML(drag)).toEqual('<div class="foo react-draggable" ' +
'style="touch-action:none;color:black;transform:translate(0px,0px);' + dashedBrowserPrefix +
'transform:translate(0px,0px);" data-reactid=".1"></div>');
});
// NOTE: this runs a shallow renderer, which DOES NOT actually render <DraggableCore>
it('should pass handle on to <DraggableCore>', function () {
drag = <Draggable handle=".foo"><div /></Draggable>;
var renderer = TestUtils.createRenderer();
renderer.render(drag);
var output = renderer.getRenderOutput();
var expected = (
<DraggableCore {...Draggable.defaultProps} handle=".foo">
<div
className="react-draggable"
style={{
'transform': 'translate(0px,0px)',
[browserPrefix + 'Transform']: 'translate(0px,0px)'
}}
transform={null} />
</DraggableCore>
);
// Not easy to actually test equality here. The functions are bound as static props so we can't test those easily.
var toOmit = ['onStart', 'onStop', 'onDrag'];
expect(_.isEqual(
_.omit(output.props, toOmit),
_.omit(expected.props, toOmit))
).toEqual(true);
});
it('should honor props', function () {
function handleStart() {}
function handleDrag() {}
function handleStop() {}
drag = TestUtils.renderIntoDocument(
<Draggable
axis="y"
handle=".handle"
cancel=".cancel"
grid={[10, 10]}
zIndex={1000}
onStart={handleStart}
onDrag={handleDrag}
onStop={handleStop}>
<div className="foo bar">
<div className="handle"/>
<div className="cancel"/>
</div>
</Draggable>
);
expect(drag.props.axis).toEqual('y');
expect(drag.props.handle).toEqual('.handle');
expect(drag.props.cancel).toEqual('.cancel');
expect(drag.props.grid).toEqual([10, 10]);
expect(drag.props.zIndex).toEqual(1000);
expect(drag.props.onStart).toEqual(handleStart);
expect(drag.props.onDrag).toEqual(handleDrag);
expect(drag.props.onStop).toEqual(handleStop);
});
it('should throw when setting className', function () {
drag = (<Draggable className="foo"><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(console.error).toHaveBeenCalledWith('Warning: Failed propType: Invalid prop className passed to Draggable - do not set this, set it on the child.');
});
it('should throw when setting style', function () {
drag = (<Draggable style={{color: 'red'}}><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(console.error).toHaveBeenCalledWith('Warning: Failed propType: Invalid prop style passed to Draggable - do not set this, set it on the child.');
});
it('should throw when setting transform', function () {
drag = (<Draggable transform="translate(100, 100)"><span /></Draggable>);
TestUtils.renderIntoDocument(drag);
expect(console.error).toHaveBeenCalledWith('Warning: Failed propType: Invalid prop transform passed to Draggable - do not set this, set it on the child.');
});
it('should call onStart when dragging begins', function () {
var called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStart={function () { called = true; }}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
expect(called).toEqual(true);
});
it('should call onStop when dragging ends', function () {
var called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStop={function () { called = true; }}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
expect(called).toEqual(true);
});
it('should not call onStart when dragging begins and disabled', function () {
var called = false;
drag = TestUtils.renderIntoDocument(
<Draggable onStart={function () { called = true; }} disabled={true}>
<div/>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
expect(called).toEqual(false);
});
it('should render with style translate() for DOM nodes', function () {
var dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }}>
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
var style = node.getAttribute('style');
expect(dragged).toEqual(true);
expect(style.indexOf('transform: translate(100px, 100px);')).not.toEqual(-1);
});
it('should honor "x" axis', function () {
var dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="x">
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
var style = node.getAttribute('style');
expect(dragged).toEqual(true);
expect(style.indexOf('transform: translate(100px, 0px);')).not.toEqual(-1);
});
it('should honor "y" axis', function () {
var dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="y">
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
var style = node.getAttribute('style');
expect(dragged).toEqual(true);
expect(style.indexOf('transform: translate(0px, 100px);')).not.toEqual(-1);
});
it('should honor "none" axis', function () {
var dragged = false;
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={function() { dragged = true; }} axis="none">
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
var style = node.getAttribute('style');
expect(dragged).toEqual(true);
// No idea why the spacing is different here
expect(style.indexOf('transform:translate(0px,0px);')).not.toEqual(-1);
});
it('should detect if an element is instanceof SVGElement and set state.isElementSVG to true', function() {
drag = TestUtils.renderIntoDocument(
<Draggable>
<svg />
</Draggable>
);
expect(drag.state.isElementSVG).toEqual(true);
});
it('should detect if an element is NOT an instanceof SVGElement and set state.isElementSVG to false', function() {
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);
expect(drag.state.isElementSVG).toEqual(false);
});
it('should render with transform translate() for SVG nodes', function () {
drag = TestUtils.renderIntoDocument(
<Draggable>
<svg />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
simulateMovementFromTo(drag, 0, 0, 100, 100);
var transform = node.getAttribute('transform');
expect(transform.indexOf('translate(100,100)')).not.toEqual(-1);
});
it('should add and remove user-select styles', function () {
// Karma runs in firefox in our tests
var userSelectStyle = ';user-select: none;' + dashedBrowserPrefix + 'user-select: none;';
drag = TestUtils.renderIntoDocument(
<Draggable>
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
expect(document.body.getAttribute('style')).toEqual('');
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
expect(document.body.getAttribute('style')).toEqual(userSelectStyle);
TestUtils.Simulate.mouseUp(node);
expect(document.body.getAttribute('style')).toEqual('');
});
it('should not add and remove user-select styles when disabled', function () {
// Karma runs in firefox in our tests
var userSelectStyle = ';user-select: none;' + dashedBrowserPrefix + 'user-select: none;';
drag = TestUtils.renderIntoDocument(
<Draggable enableUserSelectHack={false}>
<div />
</Draggable>
);
var node = ReactDOM.findDOMNode(drag);
expect(document.body.getAttribute('style')).toEqual('');
TestUtils.Simulate.mouseDown(node, {clientX: 0, clientY: 0});
expect(document.body.getAttribute('style')).toEqual('');
TestUtils.Simulate.mouseUp(node);
expect(document.body.getAttribute('style')).toEqual('');
});
});
describe('interaction', function () {
it('should initialize dragging onmousedown', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
expect(drag.state.dragging).toEqual(true);
});
it('should only initialize dragging onmousedown of handle', function () {
drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
<div>
<div className="handle">Handle</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag).querySelector('.content'));
expect(drag.state.dragging).toEqual(false);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag).querySelector('.handle'));
expect(drag.state.dragging).toEqual(true);
});
it('should not initialize dragging onmousedown of cancel', function () {
drag = TestUtils.renderIntoDocument(
<Draggable cancel=".cancel">
<div>
<div className="cancel">Cancel</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag).querySelector('.cancel'));
expect(drag.state.dragging).toEqual(false);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag).querySelector('.content'));
expect(drag.state.dragging).toEqual(true);
});
it('should discontinue dragging onmouseup', function () {
drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
expect(drag.state.dragging).toEqual(true);
TestUtils.Simulate.mouseUp(ReactDOM.findDOMNode(drag));
expect(drag.state.dragging).toEqual(false);
});
it('should modulate position on scroll', function (done) {
// This test fails in karma under PhantomJS & Firefox, scroll event quirks
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (!is_chrome) return done();
var dragCalled = false;
function onDrag(e, coreEvent) {
expect(coreEvent.deltaY).toEqual(500);
dragCalled = true;
}
drag = TestUtils.renderIntoDocument(<Draggable onDrag={onDrag}><div/></Draggable>);
var node = ReactDOM.findDOMNode(drag);
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag)); // start drag so window listener is up
expect(drag.state.dragging).toEqual(true);
document.body.style.height = '10000px';
window.scrollTo(0, 500);
setTimeout(function() {
expect(dragCalled).toEqual(true);
expect(drag.state.clientY).toEqual(500);
done();
}, 50);
});
});
describe('draggable callbacks', function () {
it('should call back on drag', function () {
function onDrag(event, data) {
expect(data.position.left).toEqual(100);
expect(data.position.top).toEqual(100);
expect(data.deltaX).toEqual(100);
expect(data.deltaY).toEqual(100);
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag}>
<div />
</Draggable>
);
// (element, fromX, fromY, toX, toY)
simulateMovementFromTo(drag, 0, 0, 100, 100);
});
it('should call back with offset left/top, not client', function () {
function onDrag(event, data) {
expect(data.position.left).toEqual(100);
expect(data.position.top).toEqual(100);
expect(data.deltaX).toEqual(100);
expect(data.deltaY).toEqual(100);
}
drag = TestUtils.renderIntoDocument(
<Draggable onDrag={onDrag} style={{position: 'relative', top: '200px', left: '200px'}}>
<div />
</Draggable>
);
simulateMovementFromTo(drag, 200, 200, 300, 300);
});
});
describe('validation', function () {
it('should result with invariant when there isn\'t a child', function () {
drag = (<Draggable/>);
var error = false;
try {
TestUtils.renderIntoDocument(drag);
} catch (e) {
error = true;
}
expect(error).toEqual(true);
});
it('should result with invariant if there\'s more than a single child', function () {
drag = (<Draggable><div/><div/></Draggable>);
var error = false;
try {
TestUtils.renderIntoDocument(drag);
} catch (e) {
error = true;
}
expect(error).toEqual(true);
});
});
});
*/