Skip to content

Commit 95f6ec9

Browse files
committed
Adding tests
1 parent 64191fa commit 95f6ec9

File tree

7 files changed

+235
-2
lines changed

7 files changed

+235
-2
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ build
44
example
55
script
66
specs
7+
.travis.yml
8+
karma.conf.js
79
webpack.config.js

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
before_script:
5+
- export DISPLAY=:99.0
6+
- sh -e /etc/init.d/xvfb start
7+
email:
8+
on_failure: change
9+
on_success: never

karma.conf.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module.exports = function(config) {
2+
config.set({
3+
4+
basePath: '',
5+
6+
frameworks: ['jasmine'],
7+
8+
files: [
9+
'specs/main.js'
10+
],
11+
12+
exclude: [
13+
],
14+
15+
preprocessors: {
16+
'specs/main.js': ['webpack']
17+
},
18+
19+
webpack: {
20+
cache: true,
21+
module: {
22+
loaders: [
23+
{test: /\.js$/, loader: 'jsx-loader'}
24+
]
25+
}
26+
},
27+
28+
webpackServer: {
29+
stats: {
30+
colors: true
31+
}
32+
},
33+
34+
reporters: ['progress'],
35+
36+
port: 9876,
37+
38+
colors: true,
39+
40+
logLevel: config.LOG_INFO,
41+
42+
autoWatch: false,
43+
44+
browsers: ['Chrome'],
45+
46+
singleRun: false,
47+
48+
plugins: [
49+
require('karma-jasmine'),
50+
require('karma-chrome-launcher'),
51+
require('karma-firefox-launcher'),
52+
require('karma-webpack')
53+
]
54+
});
55+
};

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "React draggable component",
55
"main": "lib/main.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "script/test --browsers Firefox --single-run",
88
"build": "script/build"
99
},
1010
"repository": {
@@ -23,6 +23,14 @@
2323
"homepage": "https://github.com/mzabriskie/react-draggable",
2424
"devDependencies": {
2525
"react": "^0.11.0",
26-
"jsx-loader": "^0.11.0"
26+
"jsx-loader": "^0.11.0",
27+
"karma": "^0.12.19",
28+
"karma-cli": "0.0.4",
29+
"karma-jasmine": "^0.1.5",
30+
"karma-chrome-launcher": "^0.1.4",
31+
"karma-firefox-launcher": "^0.1.3",
32+
"webpack": "^1.3.2-beta8",
33+
"webpack-dev-server": "^1.4.7",
34+
"karma-webpack": "^1.2.1"
2735
}
2836
}

script/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
node_modules/.bin/karma start "$@"

specs/draggable.spec.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
});

specs/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./draggable.spec.js');

0 commit comments

Comments
 (0)