Merge pull request #349 from kdietrich/tests

Unit Tests
This commit is contained in:
Pavel Reznikov 2016-02-22 10:13:40 -08:00
commit 9d269e18f2
6 changed files with 220 additions and 1 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
node_modules
bower_components
coverage

74
karma.conf.js Normal file
View file

@ -0,0 +1,74 @@
// Karma configuration
// Generated on Thu Feb 18 2016 22:00:23 GMT+0100 (CET)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'bower_components/jquery/dist/jquery.min.js',
'bower_components/jquery-ui/jquery-ui.min.js',
'bower_components/lodash/dist/lodash.min.js',
'src/gridstack.js',
'spec/*-spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'src/gridstack.js': ['coverage']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}

View file

@ -26,9 +26,16 @@
"grunt-contrib-cssmin": "^0.14.0",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-uglify": "^0.10.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-doctoc": "^0.1.1",
"grunt-sass": "^1.1.0",
"jasmine-core": "^2.4.1",
"karma": "^0.13.21",
"karma-coverage": "^0.5.3",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"phantomjs": "^2.1.3",
"phantomjs-prebuilt": "^2.1.4"
"grunt-contrib-watch": "^0.6.1",
"grunt-jscs": "^2.7.0"
}
}

92
spec/gridstack-spec.js Normal file
View file

@ -0,0 +1,92 @@
describe('gridstack', function() {
'use strict';
var e;
var w;
beforeEach(function() {
w = window;
e = w.GridStackUI.Engine;
});
describe('setup of gridstack', function() {
it('should exist setup function.', function() {
expect(e).not.toBeNull();
expect(typeof e).toBe('function');
});
it('should set default params correctly.', function() {
e.call(w);
expect(w.width).toBeUndefined();
expect(w.float).toBe(false);
expect(w.height).toEqual(0);
expect(w.nodes).toEqual([]);
expect(typeof w.onchange).toBe('function');
expect(w._updateCounter).toEqual(0);
expect(w._float).toEqual(w.float);
});
it('should set params correctly.', function() {
var fkt = function() { };
var arr = [1,2,3];
e.call(w, 1, fkt, true, 2, arr);
expect(w.width).toEqual(1);
expect(w.float).toBe(true);
expect(w.height).toEqual(2);
expect(w.nodes).toEqual(arr);
expect(w.onchange).toEqual(fkt);
expect(w._updateCounter).toEqual(0);
expect(w._float).toEqual(w.float);
});
});
describe('batch update', function() {
it('should set float and counter when calling batchUpdate.', function() {
e.prototype.batchUpdate.call(w);
expect(w.float).toBe(true);
expect(w._updateCounter).toEqual(1);
});
//test commit function
});
describe('sorting of nodes', function() {
it('should sort ascending with width.', function() {
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
e.prototype._sortNodes.call(w, 1);
expect(w.nodes).toEqual([{x: 0, y: 1}, {x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}]);
});
it('should sort descending with width.', function() {
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
e.prototype._sortNodes.call(w, -1);
expect(w.nodes).toEqual([{x: 9, y: 0}, {x: 4, y: 4}, {x: 7, y: 0}, {x: 0, y: 1}]);
});
it('should sort ascending without width.', function() {
w.width = false;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
e.prototype._sortNodes.call(w, 1);
expect(w.nodes).toEqual([{x: 0, y: 1}, {x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}]);
});
it('should sort descending without width.', function() {
w.width = false;
w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}];
e.prototype._sortNodes.call(w, -1);
expect(w.nodes).toEqual([{x: 9, y: 0}, {x: 4, y: 4}, {x: 7, y: 0}, {x: 0, y: 1}]);
});
});
});

43
spec/utils-spec.js Normal file
View file

@ -0,0 +1,43 @@
describe('gridstack utils', function() {
'use strict';
var utils;
beforeEach(function() {
utils = window.GridStackUI.Utils;
});
describe('setup of utils', function() {
it('should set gridstack utils.', function() {
expect(utils).not.toBeNull();
expect(typeof utils).toBe('object');
});
});
describe('test toBool', function() {
it('should return booleans.', function() {
expect(utils.toBool(true)).toEqual(true);
expect(utils.toBool(false)).toEqual(false);
});
it('should work with integer.', function() {
expect(utils.toBool(1)).toEqual(true);
expect(utils.toBool(0)).toEqual(false);
});
it('should work with Strings.', function() {
expect(utils.toBool('')).toEqual(false);
expect(utils.toBool('0')).toEqual(false);
expect(utils.toBool('no')).toEqual(false);
expect(utils.toBool('false')).toEqual(false);
expect(utils.toBool('yes')).toEqual(true);
expect(utils.toBool('yadda')).toEqual(true);
});
});
});

View file

@ -1450,6 +1450,7 @@
scope.GridStackUI = GridStack;
scope.GridStackUI.Utils = Utils;
scope.GridStackUI.Engine = GridStackEngine;
$.fn.gridstack = function(opts) {
return this.each(function() {