Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The actual value from the control's view (ctrl.$viewValue instead of … #140

Merged
merged 3 commits into from
Sep 28, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for actualView
  • Loading branch information
hueitan committed Sep 25, 2015
commit 065d9fd2182d988d0256091fa4a37877ec45a4ef
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ module.exports = function(grunt) {
}
},
jsbeautifier: {
files: ['*.js', 'src/**/*.js'],
files: ['*.js', 'src/**/*.js', 'test/unit/*.js'],
options: {}
},
jshint: {
90 changes: 90 additions & 0 deletions test/unit/actualValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'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 "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();

}));
});
46 changes: 23 additions & 23 deletions test/unit/providerSpec.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

/* jasmine specs for provider go here */

describe('provider', function () {
describe('provider', function() {

var $rootScope,
$compile,
@@ -12,9 +12,9 @@ describe('provider', function () {
validationProvider,
myApp;

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

@@ -23,7 +23,7 @@ describe('provider', function () {

beforeEach(module('myApp'));

beforeEach(inject(function ($injector) {
beforeEach(inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$scope = $rootScope.$new();
@@ -32,7 +32,7 @@ describe('provider', function () {
element = $compile('<form name="Form"><input type="text" name="required" ng-model="required" validator="required"></form>')($scope);
}));

it('set/get Expression (RegExp or Function)', inject(function () {
it('set/get Expression (RegExp or Function)', inject(function() {
var model = {
huei: /^huei$/
};
@@ -41,7 +41,7 @@ describe('provider', function () {
expect(validationProvider.getExpression('huei')).toEqual(model.huei);

model = {
huei: function () {
huei: function() {
return true;
}
};
@@ -50,7 +50,7 @@ describe('provider', function () {
expect(validationProvider.getExpression('huei')).toBe(model.huei);
}));

it('set/get DefaultMsg (String)', inject(function () {
it('set/get DefaultMsg (String)', inject(function() {
var obj = {
huei: {
error: 'It\'s should be huei',
@@ -67,12 +67,12 @@ describe('provider', function () {
}
}));

it('set/get successHTML', inject(function () {
it('set/get successHTML', inject(function() {
validationProvider.setSuccessHTML('sethtml');
expect(validationProvider.getSuccessHTML('true')).not.toEqual('sethtml');
expect(validationProvider.getSuccessHTML('true')).toEqual('<p class="validation-valid">true</p>');

validationProvider.setSuccessHTML(function (msg) {
validationProvider.setSuccessHTML(function(msg) {
return '<p class="success">' + msg + '</p>';
});

@@ -81,37 +81,37 @@ describe('provider', function () {

}));

it('set/get errorHTML', inject(function () {
it('set/get errorHTML', inject(function() {
validationProvider.setErrorHTML('sethtml');
expect(validationProvider.getErrorHTML('false')).not.toEqual('sethtml');
expect(validationProvider.getErrorHTML('false')).toEqual('<p class="validation-invalid">false</p>');

validationProvider.setErrorHTML(function (msg) {
validationProvider.setErrorHTML(function(msg) {
return '<p class="error">' + msg + '</p>';
});

expect(validationProvider.getErrorHTML('error')).toEqual('<p class="error">error</p>');
}));

it('checkValid', inject(function () {
it('checkValid', inject(function() {
expect(validationProvider.checkValid($scope.Form)).toBe(false);
$scope.Form.required.$setViewValue('required');
expect(validationProvider.checkValid($scope.Form)).toBe(true);
$scope.Form.required.$setViewValue('');
expect(validationProvider.checkValid($scope.Form)).toBe(false);
}));

it('reset', inject(function () {
it('reset', inject(function() {
var resetSpy = jasmine.createSpy('resetSpy');
$scope.$on('requiredreset-' + $scope.Form.required.validationId, function () {
$scope.$on('requiredreset-' + $scope.Form.required.validationId, function() {
resetSpy();
});
validationProvider.reset($scope.Form);
expect(element.find('p')[0]).toBeUndefined();
expect(resetSpy).toHaveBeenCalled();
}));

it('validate - submit', inject(function () {
it('validate - submit', inject(function() {
var submitSpy = jasmine.createSpy('submitSpy'),
successSpy = jasmine.createSpy('successSpy'),
errorSpy = jasmine.createSpy('errorSpy'),
@@ -120,17 +120,17 @@ describe('provider', function () {
errorSpy2 = jasmine.createSpy('errorSpy2');

// test .success()
$scope.$on('requiredsubmit-' + $scope.Form.required.validationId, function () {
$scope.$on('requiredsubmit-' + $scope.Form.required.validationId, function() {
submitSpy();
});
$scope.$apply(function () {
$scope.$apply(function() {
$scope.required = 'Required';
});
validationProvider.validate($scope.Form)
.success(function () {
.success(function() {
successSpy();
})
.error(function () {
.error(function() {
errorSpy();
});

@@ -140,19 +140,19 @@ describe('provider', function () {
expect(errorSpy).not.toHaveBeenCalled();

// test .error()
$scope.$apply(function () {
$scope.$apply(function() {
$scope.required = '';
});

$scope.$on('requiredsubmit-' + $scope.Form.required.validationId, function () {
$scope.$on('requiredsubmit-' + $scope.Form.required.validationId, function() {
submitSpy2();
});

validationProvider.validate($scope.Form)
.success(function () {
.success(function() {
successSpy2();
})
.error(function () {
.error(function() {
errorSpy2();
});