From a835ddbc4e503066a6e9b198b6fe4b55729e7331 Mon Sep 17 00:00:00 2001 From: Arkadiusz Adamski Date: Fri, 18 Sep 2015 18:38:43 +0200 Subject: [PATCH] added tests to DC visualisation Motivation: Code without tests is bad code. Added way to test code written in JS by QUnit and PhantomJS. --- bower.json | 4 +- gulpfile.js | 7 +-- .../static/js/app/data_center/controllers.js | 2 +- .../partials/data_center/data_center.html | 2 +- src/ralph/js_tests/angular_runner.html | 36 +++++++++++++ .../dc_view/data_center/controllers.test.js | 52 +++++++++++++++++++ 6 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/ralph/js_tests/angular_runner.html create mode 100644 src/ralph/js_tests/dc_view/data_center/controllers.test.js diff --git a/bower.json b/bower.json index 3b5ccd14c9..5410f8f7d4 100644 --- a/bower.json +++ b/bower.json @@ -25,6 +25,8 @@ "angular-loading-bar": "~0.8.0" }, "devDependencies": { - "qunit": "~1.18.0" + "qunit": "~1.18.0", + "sinon": "http://sinonjs.org/releases/sinon-1.16.1.js", + "sinon-qunit": "~2.0.0" } } diff --git a/gulpfile.js b/gulpfile.js index 76bfc61cb7..c44189bff3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,4 +1,4 @@ -var gulp = require('gulp'); +var gulp = require('gulp'), watch = require('gulp-watch'), runSequence = require('run-sequence'), rename = require('gulp-rename'), @@ -77,8 +77,9 @@ gulp.task('js', function(){ }); gulp.task('test', function() { - qunit('./src/ralph/js_tests/test_runner.html', {}, function(code) { - process.exit(code); + var runners = ['test_runner', 'angular_runner']; + runners.forEach(function(runner) { + qunit('./src/ralph/js_tests/' + runner + '.html'); }); }); diff --git a/src/ralph/dc_view/static/js/app/data_center/controllers.js b/src/ralph/dc_view/static/js/app/data_center/controllers.js index 86a6b7fb9d..68bf2321f4 100644 --- a/src/ralph/dc_view/static/js/app/data_center/controllers.js +++ b/src/ralph/dc_view/static/js/app/data_center/controllers.js @@ -18,7 +18,7 @@ $scope.info = item; }; - $scope.newRack = function(data_center, x, y) { + $scope.newRack = function(x, y) { var rack = new RackModel(); rack.visualization_col = x; rack.visualization_row = y; diff --git a/src/ralph/dc_view/static/partials/data_center/data_center.html b/src/ralph/dc_view/static/partials/data_center/data_center.html index 11a0dd8bab..428345bd61 100644 --- a/src/ralph/dc_view/static/partials/data_center/data_center.html +++ b/src/ralph/dc_view/static/partials/data_center/data_center.html @@ -73,7 +73,7 @@
-
+
+
+
+ + + + QUnit Example + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ralph/js_tests/dc_view/data_center/controllers.test.js b/src/ralph/js_tests/dc_view/data_center/controllers.test.js new file mode 100644 index 0000000000..cbe91a2bdd --- /dev/null +++ b/src/ralph/js_tests/dc_view/data_center/controllers.test.js @@ -0,0 +1,52 @@ +var serviceLocator = angular.injector(['ng', 'dataCenterVisualizationApp']); +var $controllers = serviceLocator.get('$controller'); +var scope = {}; +var dataCenterMock = { + rack_set: [] +}; +var rackModel; + +QUnit.module('Data Center controllers', { + setup: function () { + scope = serviceLocator.get('$rootScope').$new(); + rackModel = serviceLocator.get('RackModel'); + $controllers('DataCenterController', {$scope: scope, data_center: dataCenterMock, RackModel: rackModel}); + } +}); + +test('newRack should add rack to rack_set', function(){ + ok(scope.data_center.rack_set.length === 0); + scope.newRack(1, 1); + ok(scope.data_center.rack_set.length === 1); +}); + +test('updatePosition should not update variables actualX and actualY when target is not equal currentTarget', function() { + var mockedPos = 1; + scope.actualX = mockedPos; + scope.actualY = mockedPos; + var mockedEvent = { + target: {element: 'rack'}, + currentTarget: {element: 'grid'}, + offsetX: 800, + offsetY: 400, + }; + scope.updatePosition(mockedEvent); + ok(scope.actualX == mockedPos); + ok(scope.actualY == mockedPos); +}); + +test('updatePosition should update variables actualX and actualY', function() { + var mockedTarget = { + offsetLeft: 0, + offsetTop: 0 + }; + var mockedEvent = { + target: mockedTarget, + currentTarget: mockedTarget, + offsetX: 80, // 2 times grid size + offsetY: 40 + }; + scope.updatePosition(mockedEvent); + ok(scope.actualX === 2); + ok(scope.actualY === 1); +});