Skip to content

Instantly share code, notes, and snippets.

@cladera
Last active April 30, 2020 00:59
Show Gist options
  • Save cladera/16ba1c16eadc8f40c5160ad09d4f7293 to your computer and use it in GitHub Desktop.
Save cladera/16ba1c16eadc8f40c5160ad09d4f7293 to your computer and use it in GitHub Desktop.
AngularJs: Testing directive with templateUrl
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-06-05 using
// generator-karma 0.9.0
module.exports = function(config) {
'use strict';
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: './',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/angular-mocks/angular-mocks.js',
'./**/*.js',
'./**/*.spec.js',
'./**/*.html'
],
// list of files / patterns to exclude
exclude: [
],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'Chrome'
],
// Which plugins to enable
plugins: [
'karma-chrome-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'app/views/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
stripPrefix: 'app/',
moduleName: 'myAppTestTemplates'
},
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
<div>I'm my-directive</div>
'use strict';
/**
* @ngdoc directive
* @name myApp.directive:myDirective
* @description
* # My Directive
*/
angular.module('myApp')
.directive('myDirective', MyDirective);
function MyDirective() {
return {
restrict: 'E',
templateUrl: 'invoices-list.html',
scope: {},
controllerAs: 'vm',
bindToController: true
};
}
'use strict';
describe('Directive: myDirective', function () {
// load the directive's module
beforeEach(module('myApp'));
beforeEach(module('myAppTestTemplates'));
var element,
scope;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element('<my-directive></my-directive>');
element = $compile(element)(scope);
scope.$digest();
}));
it('should show directive test', function () {
expect(element.text()).toContain('I\'m my-directive');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment