Skip to content

Instantly share code, notes, and snippets.

/isSecure.js Secret

Created February 27, 2015 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/61e7d79021391536f7f2 to your computer and use it in GitHub Desktop.
Save anonymous/61e7d79021391536f7f2 to your computer and use it in GitHub Desktop.
I'd like to use the mock $window object only for my isSecure directive. I'd like to use the real $window in the isSecure.spec.js test file. Note the errors when running the tests due to the mock $window being used in the test code as well.
angular.module( 'validators.isSecure', [] )
.directive( 'isSecure', function( $window ) {
'use strict';
var SECURE_URL_REGEX = /^https|^\//;
var HTTP_PROTOCOL = 'http:';
var ERROR_KEY = 'isSecure';
return {
require : 'ngModel',
link : function( scope, el, attrs, ngModel ) {
ngModel.$parsers.unshift( function( viewValue ) {
var isSecure = SECURE_URL_REGEX.test( viewValue ) || $window.location.protocol === HTTP_PROTOCOL;
ngModel.$setValidity( ERROR_KEY, isSecure );
return viewValue;
} );
}
};
} );
describe( 'isSecure validator', function() {
var $window;
var $compile;
var $rootScope;
var htmlString;
var httpsUrl;
var httpUrl;
var relativeUrl;
var protocolRelativeUrl;
beforeEach( module( 'validators.isSecure' ) );
beforeEach( function() {
$window = {
location : {
protocol : 'https:'
}
};
module( function( $provide ) {
$provide.value('$window', $window);
} );
} );
beforeEach( inject( function( _$compile_, _$rootScope_ ) {
$compile = _$compile_;
$rootScope = _$rootScope_;
htmlString = '<form name="form"><input name="url" ng-model="url" is-secure></form>';
$compile( htmlString )( $rootScope );
httpsUrl = 'https://example.com/photo.jpg';
httpUrl = 'http://example.com/photo.jpg';
relativeUrl = '/photo.jpg';
protocolRelativeUrl = '//example.com/photo.jpg';
} ) );
it( 'should treat an undefined ng-model as valid', function() {
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( true );
} );
it( 'should treat an empty string as valid', function() {
$rootScope.form.url.$setViewValue( '' );
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( true );
} );
it( 'should treat a https url as valid', function() {
$rootScope.form.url.$setViewValue( httpsUrl );
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( true );
} );
it( 'should treat a relative url as valid', function() {
$rootScope.form.url.$setViewValue( relativeUrl );
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( true );
} );
it( 'should treat a protocol-relative url as valid', function() {
$rootScope.form.url.$setViewValue( protocolRelativeUrl );
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( true );
} );
it( 'should treat a http url as invalid', function() {
$rootScope.form.url.$setViewValue( httpUrl );
$rootScope.$digest();
expect( $rootScope.form.url.$valid ).toBe( false );
} );
} );
PhantomJS 1.9.8 (Mac OS X) isSecure validator should treat a https url as valid FAILED
TypeError: 'undefined' is not a function (evaluating 'document.createElement('div')')
at /Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:14220
at textInputType (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:17035)
at /Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:17403
at nodeLinkFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:6705)
at compositeLinkFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:6098)
at nodeLinkFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:6698)
at compositeLinkFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:6098)
at publicLinkFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:5994)
at /Users/jtherrell/src/myapp/myapp-ui/src/common/validators/isSecure/isSecure.spec.js:29
at invoke (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular/angular.js:3966)
at workFn (/Users/jtherrell/src/myapp/myapp-ui/vendor/angular-mocks/angular-mocks.js:2159)
Expected false to be true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment