Created
March 13, 2015 21:09
-
-
Save lasergoat/9196ed7e462996468c0d to your computer and use it in GitHub Desktop.
example of unit testing the controller within a directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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