|
| 1 | +'use strict'; |
1 | 2 | var React = require('react'); |
2 | 3 | var TestUtils = require('react/lib/ReactTestUtils'); |
3 | 4 | var Draggable = require('../index'); |
4 | 5 |
|
| 6 | +/*global describe,it,expect */ |
5 | 7 | describe('react-draggable', function () { |
6 | | - describe('props', function () { |
7 | | - it('should have default properties', function () { |
8 | | - var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
9 | | - |
10 | | - expect(drag.props.axis).toEqual('both'); |
11 | | - expect(drag.props.handle).toEqual(null); |
12 | | - expect(drag.props.cancel).toEqual(null); |
13 | | - expect(isNaN(drag.props.zIndex)).toEqual(true); |
14 | | - expect(typeof drag.props.onStart).toEqual('function'); |
15 | | - expect(typeof drag.props.onDrag).toEqual('function'); |
16 | | - expect(typeof drag.props.onStop).toEqual('function'); |
17 | | - }); |
18 | | - |
19 | | - it('should pass style and className properly from child', function () { |
20 | | - var el = <Draggable><div className="foo" style={{color: 'black'}}/></Draggable>; |
21 | | - var renderer = TestUtils.createRenderer(); |
22 | | - renderer.render(el); |
23 | | - var output = renderer.getRenderOutput(); |
24 | | - |
25 | | - expect(output.props.className).toEqual('foo react-draggable'); |
26 | | - expect(output.props.style).toEqual({color: 'black'}); |
27 | | - }); |
28 | | - |
29 | | - it('should honor props', function () { |
30 | | - function handleStart() {} |
31 | | - function handleDrag() {} |
32 | | - function handleStop() {} |
33 | | - |
34 | | - var drag = TestUtils.renderIntoDocument( |
35 | | - <Draggable |
36 | | - axis="y" |
37 | | - handle=".handle" |
38 | | - cancel=".cancel" |
39 | | - grid={[10, 10]} |
40 | | - zIndex={1000} |
41 | | - onStart={handleStart} |
42 | | - onDrag={handleDrag} |
43 | | - onStop={handleStop}> |
44 | | - <div> |
45 | | - <div className="handle"/> |
46 | | - <div className="cancel"/> |
47 | | - </div> |
48 | | - </Draggable> |
49 | | - ); |
50 | | - |
51 | | - expect(drag.props.axis).toEqual('y'); |
52 | | - expect(drag.props.handle).toEqual('.handle'); |
53 | | - expect(drag.props.cancel).toEqual('.cancel'); |
54 | | - expect(drag.props.grid).toEqual([10, 10]); |
55 | | - expect(drag.props.zIndex).toEqual(1000); |
56 | | - expect(drag.props.onStart).toEqual(handleStart); |
57 | | - expect(drag.props.onDrag).toEqual(handleDrag); |
58 | | - expect(drag.props.onStop).toEqual(handleStop); |
59 | | - }); |
60 | | - |
61 | | - it('should call onStart when dragging begins', function () { |
62 | | - var called = false; |
63 | | - var drag = TestUtils.renderIntoDocument( |
64 | | - <Draggable onStart={function () { called = true; }}> |
65 | | - <div/> |
66 | | - </Draggable> |
67 | | - ); |
68 | | - |
69 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
70 | | - expect(called).toEqual(true); |
71 | | - }); |
72 | | - |
73 | | - it('should call onStop when dragging ends', function () { |
74 | | - var called = false; |
75 | | - var drag = TestUtils.renderIntoDocument( |
76 | | - <Draggable onStop={function () { called = true; }}> |
77 | | - <div/> |
78 | | - </Draggable> |
79 | | - ); |
80 | | - |
81 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
82 | | - TestUtils.Simulate.mouseUp(drag.getDOMNode()); |
83 | | - expect(called).toEqual(true); |
84 | | - }); |
85 | | - }); |
86 | | - |
87 | | - describe('interaction', function () { |
88 | | - it('should initialize dragging onmousedown', function () { |
89 | | - var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
90 | | - |
91 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
92 | | - expect(drag.state.dragging).toEqual(true); |
93 | | - expect(drag.state.mounted).toEqual(true); |
94 | | - }); |
95 | | - |
96 | | - it('should only initialize dragging onmousedown of handle', function () { |
97 | | - var drag = TestUtils.renderIntoDocument( |
98 | | - <Draggable handle=".handle"> |
99 | | - <div> |
100 | | - <div className="handle">Handle</div> |
101 | | - <div className="content">Lorem ipsum...</div> |
102 | | - </div> |
103 | | - </Draggable> |
104 | | - ); |
105 | | - |
106 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.content')); |
107 | | - expect(drag.state.dragging).toEqual(false); |
108 | | - |
109 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.handle')); |
110 | | - expect(drag.state.dragging).toEqual(true); |
111 | | - }); |
112 | | - |
113 | | - it('should not initialize dragging onmousedown of cancel', function () { |
114 | | - var drag = TestUtils.renderIntoDocument( |
115 | | - <Draggable cancel=".cancel"> |
116 | | - <div> |
117 | | - <div className="cancel">Cancel</div> |
118 | | - <div className="content">Lorem ipsum...</div> |
119 | | - </div> |
120 | | - </Draggable> |
121 | | - ); |
122 | | - |
123 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.cancel')); |
124 | | - expect(drag.state.dragging).toEqual(false); |
125 | | - |
126 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.content')); |
127 | | - expect(drag.state.dragging).toEqual(true); |
128 | | - }); |
129 | | - |
130 | | - it('should discontinue dragging onmouseup', function () { |
131 | | - var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
132 | | - |
133 | | - TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
134 | | - expect(drag.state.dragging).toEqual(true); |
135 | | - |
136 | | - TestUtils.Simulate.mouseUp(drag.getDOMNode()); |
137 | | - expect(drag.state.dragging).toEqual(false); |
138 | | - }); |
139 | | - }); |
140 | | - |
141 | | - describe('validation', function () { |
142 | | - it('should result with invariant when there isn\'t any children', function () { |
143 | | - var drag = (<Draggable/>); |
144 | | - |
145 | | - var error = false; |
146 | | - try { |
147 | | - TestUtils.renderIntoDocument(drag); |
148 | | - } catch (e) { |
149 | | - error = true; |
150 | | - } |
151 | | - |
152 | | - expect(error).toEqual(true); |
153 | | - }); |
154 | | - |
155 | | - it('should result with invariant if there\'s more than a single child', function () { |
156 | | - var drag = (<Draggable><div/><div/></Draggable>); |
157 | | - |
158 | | - var error = false; |
159 | | - try { |
160 | | - TestUtils.renderIntoDocument(drag); |
161 | | - } catch (e) { |
162 | | - error = true; |
163 | | - } |
164 | | - |
165 | | - expect(error).toEqual(true); |
166 | | - }); |
167 | | - }); |
| 8 | + describe('props', function () { |
| 9 | + it('should have default properties', function () { |
| 10 | + var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
| 11 | + |
| 12 | + expect(drag.props.axis).toEqual('both'); |
| 13 | + expect(drag.props.handle).toEqual(null); |
| 14 | + expect(drag.props.cancel).toEqual(null); |
| 15 | + expect(isNaN(drag.props.zIndex)).toEqual(true); |
| 16 | + expect(typeof drag.props.onStart).toEqual('function'); |
| 17 | + expect(typeof drag.props.onDrag).toEqual('function'); |
| 18 | + expect(typeof drag.props.onStop).toEqual('function'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should pass style and className properly from child', function () { |
| 22 | + var el = <Draggable><div className="foo" style={{color: 'black'}}/></Draggable>; |
| 23 | + var renderer = TestUtils.createRenderer(); |
| 24 | + renderer.render(el); |
| 25 | + var output = renderer.getRenderOutput(); |
| 26 | + |
| 27 | + expect(output.props.className).toEqual('foo react-draggable'); |
| 28 | + expect(output.props.style).toEqual({color: 'black'}); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should honor props', function () { |
| 32 | + function handleStart() {} |
| 33 | + function handleDrag() {} |
| 34 | + function handleStop() {} |
| 35 | + |
| 36 | + var drag = TestUtils.renderIntoDocument( |
| 37 | + <Draggable |
| 38 | + axis="y" |
| 39 | + handle=".handle" |
| 40 | + cancel=".cancel" |
| 41 | + grid={[10, 10]} |
| 42 | + zIndex={1000} |
| 43 | + onStart={handleStart} |
| 44 | + onDrag={handleDrag} |
| 45 | + onStop={handleStop}> |
| 46 | + <div> |
| 47 | + <div className="handle"/> |
| 48 | + <div className="cancel"/> |
| 49 | + </div> |
| 50 | + </Draggable> |
| 51 | + ); |
| 52 | + |
| 53 | + expect(drag.props.axis).toEqual('y'); |
| 54 | + expect(drag.props.handle).toEqual('.handle'); |
| 55 | + expect(drag.props.cancel).toEqual('.cancel'); |
| 56 | + expect(drag.props.grid).toEqual([10, 10]); |
| 57 | + expect(drag.props.zIndex).toEqual(1000); |
| 58 | + expect(drag.props.onStart).toEqual(handleStart); |
| 59 | + expect(drag.props.onDrag).toEqual(handleDrag); |
| 60 | + expect(drag.props.onStop).toEqual(handleStop); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should call onStart when dragging begins', function () { |
| 64 | + var called = false; |
| 65 | + var drag = TestUtils.renderIntoDocument( |
| 66 | + <Draggable onStart={function () { called = true; }}> |
| 67 | + <div/> |
| 68 | + </Draggable> |
| 69 | + ); |
| 70 | + |
| 71 | + TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
| 72 | + expect(called).toEqual(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should call onStop when dragging ends', function () { |
| 76 | + var called = false; |
| 77 | + var drag = TestUtils.renderIntoDocument( |
| 78 | + <Draggable onStop={function () { called = true; }}> |
| 79 | + <div/> |
| 80 | + </Draggable> |
| 81 | + ); |
| 82 | + |
| 83 | + TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
| 84 | + TestUtils.Simulate.mouseUp(drag.getDOMNode()); |
| 85 | + expect(called).toEqual(true); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('interaction', function () { |
| 90 | + it('should initialize dragging onmousedown', function () { |
| 91 | + var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
| 92 | + |
| 93 | + TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
| 94 | + expect(drag.state.dragging).toEqual(true); |
| 95 | + expect(drag.state.mounted).toEqual(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should only initialize dragging onmousedown of handle', function () { |
| 99 | + var drag = TestUtils.renderIntoDocument( |
| 100 | + <Draggable handle=".handle"> |
| 101 | + <div> |
| 102 | + <div className="handle">Handle</div> |
| 103 | + <div className="content">Lorem ipsum...</div> |
| 104 | + </div> |
| 105 | + </Draggable> |
| 106 | + ); |
| 107 | + |
| 108 | + TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.content')); |
| 109 | + expect(drag.state.dragging).toEqual(false); |
| 110 | + |
| 111 | + TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.handle')); |
| 112 | + expect(drag.state.dragging).toEqual(true); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not initialize dragging onmousedown of cancel', function () { |
| 116 | + var drag = TestUtils.renderIntoDocument( |
| 117 | + <Draggable cancel=".cancel"> |
| 118 | + <div> |
| 119 | + <div className="cancel">Cancel</div> |
| 120 | + <div className="content">Lorem ipsum...</div> |
| 121 | + </div> |
| 122 | + </Draggable> |
| 123 | + ); |
| 124 | + |
| 125 | + TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.cancel')); |
| 126 | + expect(drag.state.dragging).toEqual(false); |
| 127 | + |
| 128 | + TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.content')); |
| 129 | + expect(drag.state.dragging).toEqual(true); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should discontinue dragging onmouseup', function () { |
| 133 | + var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>); |
| 134 | + |
| 135 | + TestUtils.Simulate.mouseDown(drag.getDOMNode()); |
| 136 | + expect(drag.state.dragging).toEqual(true); |
| 137 | + |
| 138 | + TestUtils.Simulate.mouseUp(drag.getDOMNode()); |
| 139 | + expect(drag.state.dragging).toEqual(false); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('validation', function () { |
| 144 | + it('should result with invariant when there isn\'t any children', function () { |
| 145 | + var drag = (<Draggable/>); |
| 146 | + |
| 147 | + var error = false; |
| 148 | + try { |
| 149 | + TestUtils.renderIntoDocument(drag); |
| 150 | + } catch (e) { |
| 151 | + error = true; |
| 152 | + } |
| 153 | + |
| 154 | + expect(error).toEqual(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should result with invariant if there\'s more than a single child', function () { |
| 158 | + var drag = (<Draggable><div/><div/></Draggable>); |
| 159 | + |
| 160 | + var error = false; |
| 161 | + try { |
| 162 | + TestUtils.renderIntoDocument(drag); |
| 163 | + } catch (e) { |
| 164 | + error = true; |
| 165 | + } |
| 166 | + |
| 167 | + expect(error).toEqual(true); |
| 168 | + }); |
| 169 | + }); |
168 | 170 | }); |
0 commit comments