App generated by Yeoman with angular-generator.
Directive:
angular.module('psApp').directive('scrollTop', function () {
return {
restrict: 'A',
scope: true,
template: '<a href="#" ng-click="click()" class="scroll-top"><span class="glyphicon glyphicon-arrow-up"></span> Back to top</a>',
controller: ['$scope', '$element', '$document', function ($scope, $element, $document) {
$scope.click = function () {
$document.scrollTop(0, 500);
};
}]
};
});
Test:
describe('Directive: scrollTop', function () {
// load the directive's module
beforeEach(module('psApp'));
var scope, element;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = $compile('<div scroll-top></div>')(scope);
scope.$apply();
}));
it('should have "Back to top" as text', inject(function () {
expect(element.html()).toBe('<a href="#" ng-click="click()" class="scroll-top"><span class="glyphicon glyphicon-arrow-up"></span> Back to top</a>');
expect(element.text()).toBe(' Back to top');
element.click();
}));
});
Error:
PhantomJS 1.9.7 (Mac OS X) Directive: scrollTop should have "Back to top" as text TypeError: 'undefined' is not a function (evaluating 'element.click()')
I cannot understand where the problem is. :( Please post functional code.
This is not a deficiency of PhantomJS, but rather a known limitation of the jqLite package that is included with AngularJS. It does not have a 'click' function in its element class: https://docs.angularjs.org/api/ng/function/angular.element
There are two alternate approaches for testing purposes.
Use the 'triggerHandler()' function of jqLite to call the click handler. But this approach would call your handler with a dummy event; see the documentation for triggerHandler. Test code would look something like:
Since triggerHandler does not pass a 'real' event object, you might need to adjust your tests accordingly.
For some reason PhantomJS does not have a click() function in it. Here is the workaround:
And here is how to use it:
This isn't place for testing browser action (in this case click). In UNIT test you should test it isolated so test just click action from the scope:
The thing you want to test, click on the browser is more integration test since you are testing click in the browser and response in directive -> this is more job for Selenium for example.