Skip to content

Commit e258d59

Browse files
committed
tests(gridster): add basic test suite for gridster
Tests are build with mocha + chai, and you can run them from the command line with `grunt karma` or opening `test/index.html` in your browser. This is a first approach, there are hundreds of tests more that should be added. I’ve ported some tests on PRs but more needs to be done.
1 parent 2002c45 commit e258d59

11 files changed

+1133
-111
lines changed

Gruntfile.js

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ module.exports = function(grunt) {
156156
watch: {
157157
files: ['libs/*.js', 'src/*.js', 'src/*.css', 'Gruntfile.js'],
158158
tasks: ['concat', 'uglify', 'cssmin']
159+
},
160+
161+
karma: {
162+
gridster: {
163+
configFile: 'test/karma.conf.js'
164+
}
159165
}
160166
});
161167

@@ -168,6 +174,7 @@ module.exports = function(grunt) {
168174
grunt.loadNpmTasks('grunt-contrib-yuidoc');
169175
grunt.loadNpmTasks('grunt-bump');
170176
grunt.loadNpmTasks('grunt-conventional-changelog');
177+
grunt.loadNpmTasks('grunt-karma');
171178

172179
// Default task.
173180
grunt.registerTask('default', ['jshint', 'concat', 'uglify', 'cssmin']);

package.json

+15-6
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,26 @@
2626
"jquery": "2.1.3"
2727
},
2828
"devDependencies": {
29+
"chai": "^2.2.0",
2930
"grunt": "~0.4.1",
30-
"grunt-contrib-uglify": "~0.2.0",
31-
"grunt-contrib-jshint": "~0.3.0",
31+
"grunt-bump": "0.0.11",
3232
"grunt-contrib-concat": "~0.1.3",
33-
"grunt-contrib-watch": "~0.3.1",
3433
"grunt-contrib-cssmin": "~0.5.0",
34+
"grunt-contrib-jshint": "~0.3.0",
35+
"grunt-contrib-uglify": "~0.2.0",
36+
"grunt-contrib-watch": "~0.3.1",
3537
"grunt-contrib-yuidoc": "~0.4.0",
36-
"bower": "~0.9.2",
38+
"grunt-conventional-changelog": "~1.0.0",
39+
"grunt-karma": "^0.10.1",
40+
"karma": "^0.12.31",
41+
"karma-chai": "^0.1.0",
42+
"karma-chrome-launcher": "^0.1.7",
43+
"karma-mocha": "^0.1.10",
44+
"karma-mocha-reporter": "^1.0.2",
45+
"karma-requirejs": "^0.2.2",
46+
"mocha": "^2.2.4",
3747
"qunitjs": "~1.11.0",
38-
"grunt-bump": "0.0.11",
39-
"grunt-conventional-changelog": "~1.0.0"
48+
"requirejs": "^2.1.17"
4049
},
4150
"browserify": {
4251
"transform": [

test/amd/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>gridster.js tests</title>
6+
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
7+
8+
<link rel="stylesheet" href="../../dist/jquery.gridster.css" />
9+
<script src="../../node_modules/requirejs/require.js"></script>
10+
11+
<script src="../../node_modules/mocha/mocha.js"></script>
12+
<script>mocha.setup('bdd');</script>
13+
14+
<script>
15+
require.config({
16+
baseUrl : '../../',
17+
18+
paths: {
19+
mocha: 'node_modules/mocha/mocha',
20+
chai: 'node_modules/chai/chai',
21+
jquery: 'node_modules/jquery/dist/jquery',
22+
gridster: 'dist/jquery.gridster'
23+
}
24+
25+
});
26+
27+
require(['jquery'], function($) {
28+
$.noConflict( true );
29+
30+
require(['test/amd/index'], function(Gridster) {
31+
mocha.setup('bdd');
32+
});
33+
});
34+
</script>
35+
</head>
36+
<body>
37+
<div id="mocha"></div>
38+
<div id="fixture"></div>
39+
</body>
40+
</html>

test/amd/index.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*global Gridster:false*/
2+
/*global chai:false, describe:false, beforeEach:false, afterEach:false, it:false*/
3+
4+
require.config({
5+
baseUrl : '../../',
6+
7+
paths: {
8+
mocha: 'node_modules/mocha/mocha',
9+
chai: 'node_modules/chai/chai',
10+
jquery: 'node_modules/jquery/dist/jquery',
11+
gridster: 'dist/jquery.gridster'
12+
}
13+
14+
});
15+
16+
require(['jquery'], function($) {
17+
$.noConflict( true );
18+
19+
require(['test/amd/index'], function(Gridster) {
20+
mocha.setup('bdd');
21+
22+
$(function() {
23+
mocha.run();
24+
});
25+
});
26+
});
27+
28+
29+
define(['chai', 'jquery', 'gridster'], function(chai, $, Gridster) {
30+
'use strict';
31+
32+
var expect = chai.expect;
33+
34+
describe('AMD support', function() {
35+
describe('Gridster', function() {
36+
it('should not define jQuery as global', function() {
37+
expect(window.$).to.be.undefined;
38+
expect(window.jQuery).to.be.undefined;
39+
});
40+
41+
it('should not define Gridster as global', function() {
42+
expect(window.Gridster).to.be.undefined;
43+
expect(window.GridsterDraggable).to.be.undefined;
44+
expect(window.GridsterCoords).to.be.undefined;
45+
expect(window.GridsterCollision).to.be.undefined;
46+
});
47+
48+
it('should return Gridster class', function() {
49+
expect(Gridster).to.be.a('function');
50+
expect(Gridster.name).to.equal('Gridster');
51+
});
52+
53+
it('should define the jquery bridge', function() {
54+
expect($.fn.gridster).to.be.a('function');
55+
});
56+
});
57+
58+
describe('Draggable', function() {
59+
var Draggable = require('gridster-draggable');
60+
61+
it('should not be defined in the global scope', function() {
62+
expect(window.GridsterDraggable).to.be.undefined;
63+
});
64+
65+
it('should return the Draggable class', function() {
66+
expect(Draggable.name).to.equal('Draggable');
67+
});
68+
});
69+
});
70+
71+
});

test/index.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>gridster.js tests</title>
6+
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
7+
8+
<link rel="stylesheet" href="../dist/jquery.gridster.css" />
9+
<script src="../node_modules/jquery/dist/jquery.js"></script>
10+
<script src="../dist/jquery.gridster.js"></script>
11+
12+
13+
<script src="../node_modules/chai/chai.js"></script>
14+
<script src="../node_modules/mocha/mocha.js"></script>
15+
<script>mocha.setup('bdd');</script>
16+
<link rel="stylesheet" href="lib/test.css" />
17+
<script src="lib/test_utils.js"></script>
18+
<script src="index.js"></script>
19+
<script>
20+
$(function() {
21+
mocha.run();
22+
});
23+
</script>
24+
</head>
25+
<body>
26+
<div id="mocha"></div>
27+
<div id="fixture"></div>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)