diff --git a/.jshintrc b/.jshintrc index fe86382..300d6de 100644 --- a/.jshintrc +++ b/.jshintrc @@ -34,6 +34,13 @@ "define": true, "require": true, "Chromath": false, - "setImmediate": true + "setImmediate": true, + "expect": true, + "it": true, + "describe": true, + "sinon": true, + "module": true, + "beforeEach": true, + "inject": true } } diff --git a/Gruntfile.js b/Gruntfile.js index 646705d..7c5c08d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -40,11 +40,13 @@ module.exports = function(grunt) { babel: { options: { - sourceMap: true, - presets: ["es2015"], - plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"], + presets: ["es2015"] }, dist: { + options: { + sourceMap: true, + plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"] + }, files: [{ cwd: 'src', expand: true, @@ -57,6 +59,34 @@ module.exports = function(grunt) { dest: 'dist/' }] }, + distTestNoSystemJs: { + files: [{ + cwd: 'src', + expand: true, + src: ['**/*.js'], + dest: 'dist/test' + }] + }, + distTestsSpecsNoSystemJs: { + files: [{ + expand: true, + cwd: 'specs', + src: ['**/*.js'], + dest: 'dist/test/specs' + }] + } + }, + + mochaTest: { + test: { + options: { + reporter: 'spec' + }, + src: [ + 'dist/test/datasource-zabbix/specs/test-main.js', + 'dist/test/datasource-zabbix/specs/*_specs.js' + ] + } }, sass: { @@ -77,6 +107,7 @@ module.exports = function(grunt) { 'copy:src_to_dist', 'copy:pluginDef', 'babel', - 'sass' + 'sass', + 'mochaTest' ]); }; diff --git a/package.json b/package.json index b4b96f1..d433b0e 100644 --- a/package.json +++ b/package.json @@ -23,16 +23,23 @@ "grunt-contrib-copy": "~0.8.2", "grunt-contrib-watch": "^0.6.1", "grunt-contrib-uglify": "~0.11.0", + "grunt-mocha-test": "~0.12.7", "grunt-systemjs-builder": "^0.2.5", "load-grunt-tasks": "~3.2.0", "grunt-execute": "~0.2.2", - "grunt-contrib-clean": "~0.6.0" + "grunt-contrib-clean": "~0.6.0", + "prunk": "~1.2.1", + "jsdom": "~3.1.2", + "q": "~1.4.1", + "chai": "~3.5.0", + "moment": "~2.14.1" }, "dependencies": { "babel-plugin-transform-es2015-modules-systemjs": "^6.5.0", - "babel-plugin-transform-es2015-for-of": "^6.5.0", + "babel-plugin-transform-es2015-for-of": "^6.6.0", "babel-preset-es2015": "^6.5.0", - "lodash": "~4.0.0" + "lodash": "~4.0.0", + "mocha": "^2.4.5" }, "homepage": "http://grafana-zabbix.org" } diff --git a/src/datasource-zabbix/specs/datasource_specs.js b/src/datasource-zabbix/specs/datasource_specs.js new file mode 100644 index 0000000..9f8faff --- /dev/null +++ b/src/datasource-zabbix/specs/datasource_specs.js @@ -0,0 +1,40 @@ +import {Datasource} from "../module"; +import Q from "q"; + +describe('ZabbixDatasource', function() { + var ctx = {}; + + beforeEach(function() { + ctx.instanceSettings = { + jsonData: { + username: 'zabbix', + password: 'zabbix', + trends: false + } + }; + ctx.$q = Q; + ctx.templateSrv = {}; + ctx.alertSrv = {}; + ctx.zabbixAPIService = function() {}; + ctx.ZabbixCachingProxy = function() {}; + ctx.QueryProcessor = function() {}; + ctx.ds = new Datasource(ctx.instanceSettings, ctx.$q, ctx.templateSrv, ctx.alertSrv, + ctx.zabbixAPIService, ctx.ZabbixCachingProxy, ctx.QueryProcessor); + }); + + describe('When querying data', function() { + + it('should return an empty array when no targets are set', function(done) { + var options = { + targets: [], + range: {from: null, to: null} + }; + ctx.ds.query(options).then(function(result) { + expect(result.data).to.have.length(0); + done(); + }); + }); + + }); + +}); diff --git a/src/datasource-zabbix/specs/test-main.js b/src/datasource-zabbix/specs/test-main.js new file mode 100644 index 0000000..cc846f1 --- /dev/null +++ b/src/datasource-zabbix/specs/test-main.js @@ -0,0 +1,43 @@ +// JSHint options +/* globals global: false */ + +import prunk from 'prunk'; +import {jsdom} from 'jsdom'; +import chai from 'chai'; + +// Mock angular module +var angularMocks = { + module: function() { + return { + directive: function() {}, + service: function() {}, + factory: function() {} + }; + } +}; + +var datemathMock = { + parse: function() {} +}; + +// Mock Grafana modules that are not available outside of the core project +// Required for loading module.js +prunk.mock('./css/query-editor.css!', 'no css, dude.'); +prunk.mock('app/plugins/sdk', { + QueryCtrl: null +}); +prunk.mock('app/core/utils/datemath', datemathMock); +prunk.mock('angular', angularMocks); +prunk.mock('jquery', 'module not found'); + +// Setup jsdom +// Required for loading angularjs +global.document = jsdom(''); +global.window = global.document.parentWindow; +global.navigator = window.navigator = {}; +global.Node = window.Node; + +// Setup Chai +chai.should(); +global.assert = chai.assert; +global.expect = chai.expect;