mardi 6 janvier 2015

Using Modernizr in Angular unit test

I have an Angular app defined within a Grails gsp. The Modernizr library is included at the gsp level.


I need to use the library within a directive unit test. Since I don't have Modernizr defined as a module since it's used outside of the Angular app as well as inside it, how do I inject it into my Angular unit test?


Here's my directive:



'use strict';

angular.module('simplify.directives').directive('img', ['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function (elem, attrs) {
if ( typeof Modernizr !== 'undefined' && !Modernizr.svg ) {
$timeout(function(){
elem.attr('src', attrs.src.replace('.svg', '.png'));
});
}
}
};
}]);


Here's my unit test code:



'use strict';

describe('Testing SVG to PNG directive', function() {
var scope,
elem;

beforeEach(module('app'));

beforeEach(module(function($provide) {
$provide.service('appConstants', function(){});
}));

beforeEach(inject(function($compile, $rootScope) {
elem = angular.element('<img ng-src="test-img.svg" />');
scope = $rootScope;
$compile(elem)(scope);
scope.$digest();
}));

it('Should swap svg for png image if svg is not supported', function() {
//force Modernizr.svg to be undefined here for purposes of the test
expect(elem.attr('src')).toBe('test-img.png');
});

});


What's the best-practice way to do this?


Aucun commentaire:

Enregistrer un commentaire