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