From 9b56d425aa9e4bc9bf9e99689e9c08a8ae09038e Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Thu, 24 Apr 2014 12:01:06 -0300 Subject: [PATCH] Testing directives for AngularJS --- js/app.js | 4 +- js/directives.js | 2 +- test/unit/directives/directivesSpec.js | 60 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/js/app.js b/js/app.js index 24c39d302..d1bb2d4e1 100644 --- a/js/app.js +++ b/js/app.js @@ -13,7 +13,8 @@ var copay = window.copay = angular.module('copay',[ 'copay.signin', 'copay.socket', 'copay.controllerUtils', - 'copay.setup' + 'copay.setup', + 'copay.directives' ]); angular.module('copay.header', []); @@ -26,4 +27,5 @@ angular.module('copay.controllerUtils', []); angular.module('copay.signin', []); angular.module('copay.setup', []); angular.module('copay.socket', []); +angular.module('copay.directives', []); diff --git a/js/directives.js b/js/directives.js index 2c80bd30d..991ed318f 100644 --- a/js/directives.js +++ b/js/directives.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copay') +angular.module('copay.directives') .directive('validAddress', [function() { var bitcore = require('bitcore'); diff --git a/test/unit/directives/directivesSpec.js b/test/unit/directives/directivesSpec.js index ca9c694ec..42aa5f521 100644 --- a/test/unit/directives/directivesSpec.js +++ b/test/unit/directives/directivesSpec.js @@ -1,6 +1,66 @@ +'use strict'; // // test/unit/directives/directivesSpec.js // describe("Unit: Testing Directives", function() { + var $scope, form; + + beforeEach(module('copay.directives')); + + describe('Validate Address', function() { + beforeEach(inject(function($compile, $rootScope) { + $scope = $rootScope; + var element = angular.element( + '
' + + '' + + '
' + ); + $scope.model = { address: null }; + $compile(element)($scope); + $scope.$digest(); + form = $scope.form; + })); + + it('should validate', function() { + form.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy'); + expect(form.address.$invalid).to.equal(false); + }); + it('should not validate', function() { + form.address.$setViewValue('thisisaninvalidaddress'); + expect(form.address.$invalid).to.equal(true); + }); + }); + + describe('Validate Amount', function() { + beforeEach(inject(function($compile, $rootScope) { + $scope = $rootScope; + $rootScope.availableBalance = 2; + var element = angular.element( + '
' + + '' + + '
' + ); + $scope.model = { amount: null }; + $compile(element)($scope); + $scope.$digest(); + form = $scope.form; + })); + + it('should validate between min and max value', function() { + form.amount.$setViewValue(1.2); + expect(form.amount.$invalid).to.equal(false); + }); + it('should not validate between min and max value', function() { + form.amount.$setViewValue(0); + expect(form.amount.$invalid).to.equal(true); + form.amount.$setViewValue(9999999999999); + expect(form.amount.$invalid).to.equal(true); + }); + it('should not validate because not enough amount', function() { + form.amount.$setViewValue(2.1); + expect(form.amount.$invalid).to.equal(true); + }); + }); + });