From 0ffd48cef58c1d35f83273a37bf3fa7873db72f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Fri, 22 May 2015 12:03:05 +0200 Subject: [PATCH] Added more tests --- .../plugins/core/widgets/luci.input.port.js | 2 +- .../core/widgets/luci.input.port.test.js | 63 +++++++++++++++++++ luciexpress/tests/lib-juci.js | 4 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 luciexpress/htdocs/plugins/core/widgets/luci.input.port.test.js diff --git a/luciexpress/htdocs/plugins/core/widgets/luci.input.port.js b/luciexpress/htdocs/plugins/core/widgets/luci.input.port.js index 59fea5d1e..beb75457f 100644 --- a/luciexpress/htdocs/plugins/core/widgets/luci.input.port.js +++ b/luciexpress/htdocs/plugins/core/widgets/luci.input.port.js @@ -1,4 +1,4 @@ -$juci.module("core") +JUCI.app .directive("luciInputPort", function () { var plugin_root = $juci.module("core").plugin_root; return { diff --git a/luciexpress/htdocs/plugins/core/widgets/luci.input.port.test.js b/luciexpress/htdocs/plugins/core/widgets/luci.input.port.test.js new file mode 100644 index 000000000..c7fb05507 --- /dev/null +++ b/luciexpress/htdocs/plugins/core/widgets/luci.input.port.test.js @@ -0,0 +1,63 @@ +//! Author: Martin K. Schröder + +require("../../../../tests/lib-juci"); +require("./luci.input.port"); + +describe("luciInputPortController", function(){ + var $scope; + var IP = "192.168.1.1"; + beforeEach(function() { + $scope = { model: "100-300", portRange: 0 }; + controller("luciInputPortController", $scope); + }); + it("should correctly read port range from model", function(){ + ["1-10", "100-300", "1-4000"].map(function(range){ + $scope.portRange = 1; + $scope.model = range; + var parts = range.split("-"); + //console.log(JSON.stringify($scope)); + expect($scope.startPort).to.be(parts[0]); + expect($scope.endPort).to.be(parts[1]); + }); + }); + it("should not allow invalid port ranges", function(){ + ["0-300", "1-100000", "200000-1", "10-1", "300000-400000"].map(function(range){ + $scope.portRange = 1; + $scope.model = range; + expect($scope.startPort).to.be(""); + expect($scope.endPort).to.be(""); + }); + }); + it("should not allow single ports out of range", function(){ + ["0", "100000", "-10000", "-3000"].map(function(range){ + $scope.portRange = 0; + $scope.model = range; + expect($scope.port).to.be(""); + }); + }); + it("should not allow negative numbers that look like port ranges", function(){ + ["-10000", "-3000"].map(function(range){ + $scope.portRange = 1; + $scope.model = range; + expect($scope.port).to.be(""); + expect($scope.startPort).to.be(""); + expect($scope.endPort).to.be(""); + }); + }); + it("should update port range when values change", function(){ + [["11", "35"], ["200", "400"]].map(function(parts){ + $scope.portRange = 1; + $scope.startPort = parts[0]; + $scope.endPort = parts[1]; + expect($scope.model).to.be(parts[0]+"-"+parts[1]); + }); + }); + it("should not allow invalid numbers to be entered in port range", function(){ + [["0", "1"], ["-1", "30"], ["100000", "400000"]].map(function(parts){ + $scope.portRange = 1; + $scope.startPort = parts[0]; + $scope.endPort = parts[1]; + expect($scope.model).to.be(""); + }); + }); +}); diff --git a/luciexpress/tests/lib-juci.js b/luciexpress/tests/lib-juci.js index 66d8ebd7a..d4977bde7 100644 --- a/luciexpress/tests/lib-juci.js +++ b/luciexpress/tests/lib-juci.js @@ -110,7 +110,9 @@ function JUCIMock(){ console.log("$scope.apply"); } scope.$watch = function(name, fn){ - global.watch(this, name, fn); + global.watch(this, name, function(){ + fn(this[name]); + }); } var args = annotate(ctrl).map(function(x){ switch(x){