Skip to content

Instantly share code, notes, and snippets.

@adamweeks

adamweeks/test.js Secret

Created Dec 18, 2014
Embed
What would you like to do?
describe('myApp', function () {
var element, scope, innerScope, elementCtrl;
beforeEach(function () {
module('myApp');
//Create Element with our directive
element = angular.element('<my-drtv passed-var="passThis" passed-expression="myFunction()">');
inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
//Create scope variables to pass to the directive
scope.passThis = 'Passing';
scope.myFunction = function(){};
$compile(element)(scope);
scope.$digest();
//Now our element is ready and behaving like it would on a page
innerScope = element.isolateScope();
elementCtrl = innerScope.myDirectiveCtrl;
});
});
it('says hello', function () {
expect(element.text()).toBe('Hello Passing');
});
it('should call the passed function', function(){
//Watch our main scope's function
spyOn(scope,"myFunction");
expect(scope.myFunction).not.toHaveBeenCalled();
//Tell the element to call it's function that calls the parent's function
elementCtrl.doSomething();
expect(scope.myFunction).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment