Skip to content

Instantly share code, notes, and snippets.

@lasergoat
Created March 13, 2015 21:09
Show Gist options
  • Save lasergoat/9196ed7e462996468c0d to your computer and use it in GitHub Desktop.
Save lasergoat/9196ed7e462996468c0d to your computer and use it in GitHub Desktop.
example of unit testing the controller within a directive
describe('Common: recordDirective', function() {
var $scope, $stateParams, $state, $compile, $q, element, makeHtml;
// each test in this file MUST call this method
// to customize var form and var $scope for itself
makeHtml = function(nonClickable) {
// $scope = $scope.$new();
var nonClickableClause = '';
if (nonClickable) {
nonClickableClause = 'non-clickable';
}
element = angular.element(
'<record '+nonClickableClause+'></record>'
);
return $compile(element)($scope);
};
beforeEach(function() {
module('NgpApp');
module('templates');
module(function($provide) {
$provide.value('sessionState', sessionState = {});
});
module(function($urlRouterProvider) {
$urlRouterProvider.deferIntercept();
});
});
beforeEach(function() {
inject(function(_$rootScope_, _$state_, _$stateParams_, _$compile_, _$q_) {
$scope = _$rootScope_;
$stateParams = _$stateParams_;
$state = _$state_;
$compile = _$compile_;
$q = _$q_;
});
var pingDefer = $q.defer();
pingDefer.resolve();
// nearly every test uses these:
sessionState.ping = jasmine.createSpy('ping').andReturn(pingDefer.promise);
});
it('should set $scope.nonClickable to false when not passed in', function() {
var element = makeHtml();
$scope.$digest();
expect(element.scope().nonClickable).toBeFalsy();
});
it('should set $scope.nonClickable to true when passed in', function() {
var element = makeHtml(true);
$scope.$digest();
expect(element.scope().nonClickable).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment