Skip to content

Commit

Permalink
Merge pull request #140 from huei90/actualValue
Browse files Browse the repository at this point in the history
The actual value from the control's view (ctrl.$viewValue instead of …
  • Loading branch information
Huei Tan committed Sep 28, 2015
2 parents b268827 + 2db2ad7 commit 4dcdb68
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = function(grunt) {
}
},
jsbeautifier: {
files: ['*.js', 'src/**/*.js'],
files: ['*.js', 'src/**/*.js', 'test/unit/*.js'],
options: {}
},
jshint: {
Expand Down
2 changes: 2 additions & 0 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@
if (attrs.validMethod === 'submit') {
watch(); // clear previous scope.$watch
watch = scope.$watch('model', function(value, oldValue) {
value = ctrl.$viewValue;

// don't watch when init
if (value === oldValue) {
Expand Down Expand Up @@ -610,6 +611,7 @@
* This is the default method
*/
scope.$watch('model', function(value) {
value = ctrl.$viewValue;
/**
* dirty, pristine, viewValue control here
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
if (attrs.validMethod === 'submit') {
watch(); // clear previous scope.$watch
watch = scope.$watch('model', function(value, oldValue) {
value = ctrl.$viewValue;

// don't watch when init
if (value === oldValue) {
Expand Down Expand Up @@ -318,6 +319,7 @@
* This is the default method
*/
scope.$watch('model', function(value) {
value = ctrl.$viewValue;
/**
* dirty, pristine, viewValue control here
*/
Expand Down
119 changes: 119 additions & 0 deletions test/unit/actualValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';

/* jasmine specs for provider go here */

describe('provider', function() {

var $rootScope,
$compile,
$scope,
$timeout,
element,
validationProvider,
myApp;

beforeEach(function() {
myApp = angular.module('myApp', ['validation', 'validation.rule'])
.config(function($validationProvider) {
validationProvider = $validationProvider;
});

return myApp;
});

beforeEach(module('myApp'));

beforeEach(inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$scope = $rootScope.$new();
$timeout = $injector.get('$timeout');

element = $compile('<form name="Form"><input type="number" name="numberWatch" ng-model="number" validator="number,maxlength=4" email-error-message="Error Number" email-success-message="Good Number"/></form>')($scope);
angular.element(document.body).append(element);
$scope.$digest();
}));

it('set value to 123', inject(function() {

var submitSpy = jasmine.createSpy('submitSpy'),
successSpy = jasmine.createSpy('successSpy'),
errorSpy = jasmine.createSpy('errorSpy');

$scope.$apply(function() {
$scope.number = 123;
});

$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
submitSpy();
});

validationProvider.validate($scope.Form)
.success(function() {
successSpy();
})
.error(function() {
errorSpy();
});

$timeout.flush();
expect(submitSpy).toHaveBeenCalled();
expect(successSpy).toHaveBeenCalled();
expect(errorSpy).not.toHaveBeenCalled();

}));

it('set value to 1234567', inject(function() {

var submitSpy = jasmine.createSpy('submitSpy'),
successSpy = jasmine.createSpy('successSpy'),
errorSpy = jasmine.createSpy('errorSpy');

$scope.$apply(function() {
$scope.number = 1234567;
});

$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
submitSpy();
});

validationProvider.validate($scope.Form)
.success(function() {
successSpy();
})
.error(function() {
errorSpy();
});

$timeout.flush();
expect(submitSpy).toHaveBeenCalled();
expect(successSpy).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();

}));

it('set value to "ABC"', inject(function() {

var submitSpy = jasmine.createSpy('submitSpy'),
successSpy = jasmine.createSpy('successSpy'),
errorSpy = jasmine.createSpy('errorSpy');

// expect error because we are using <input type="number"/> not type="text"
try {
$scope.$apply(function() {
$scope.number = 'ABC';
});
} catch (e) {
errorSpy();
}

$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
submitSpy();
});

expect(submitSpy).not.toHaveBeenCalled();
expect(successSpy).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();

}));
});
Loading

0 comments on commit 4dcdb68

Please sign in to comment.