angular-test-context
Version:
A simple API that implements core ngMock capabilities needed for unit testing.
132 lines (109 loc) • 2.98 kB
JavaScript
/*
* Copyright 2015. Author: Jeffrey Hing. All Rights Reserved.
*
* MIT License
*
* AngularTestContext class.
*/
;
//-------------------------------------
// Module exports
//-------------------------------------
module.exports = AngularTestContext;
//-------------------------------------
// Module dependencies and variables
//-------------------------------------
// Private model name.
var MODEL = '_angularTestContext';
//-------------------------------------
// Page object.
//-------------------------------------
/*
* AngularTestContext is a class which provides a simple API for
* invoking common angular functions needed during unit testing.
*
* @constructor
*
* @param {...string[]|string} [moduleNames]
* The names of the required modules.
*/
function AngularTestContext() {
var m = this[MODEL] = {};
m.scope = null;
var moduleDependencies = ['ng'];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (arg instanceof Array) {
for (var j = 0; j < arg.length; j++) {
moduleDependencies.push(arg[j]);
}
} else {
moduleDependencies.push(arg);
}
}
m.injector = angular.injector(moduleDependencies);
}
var proto = AngularTestContext.prototype;
/*
* Compiles the HTML against the provided scope properties.
*
* @param {string} html
* @param {object} [scopeProperties]
* The properties to add to the scope at compilation.
* @return {object} The root element of the compiled HTML.
*/
proto.compile = function(html, scopeProperties) {
var element = null;
var tc = this;
compileHtml.$inject = ['$compile', '$rootScope'];
function compileHtml($compile) {
var scope = tc.scope();
// Add the properties to the current scope.
if (scopeProperties) {
angular.extend(scope, scopeProperties);
}
// Compile the HTML against the scope.
element = $compile(html)(scope);
scope.$digest();
}
this.inject(compileHtml);
return element;
};
/*
* Return the angular scope object used to compile the HTML.
*
* @return {object} scope
*/
proto.scope = function() {
var m = this[MODEL];
if (!m.scope) {
this.inject(function($rootScope) {
m.scope = $rootScope.$new();
});
}
return m.scope;
};
/*
* Executes a digest cycle on the current scope.
*/
proto.digest = function() {
this.scope().$digest();
};
/*
* Invokes the function and injects any specified module dependencies.
*
* @param {function} func
*/
proto.inject = function(func) {
var m = this[MODEL];
m.injector.invoke(func);
};
/*
* Instantiates a new instance and injects any specified module dependencies.
*
* @param {function} constructor
*/
proto.instantiate = function(constructor) {
var m = this[MODEL];
return m.injector.instantiate(constructor, {'$scope': this.scope()});
};