AngularJS: how to test directive which gives focus to element?

半世苍凉 提交于 2020-01-13 13:52:12

问题


Note: the suggested link is an answer to a question about a service, and doesn't give a clear explanation on how to solve this issue

I'm trying to setup a karma test for my simple (and working) AngularJS autofocus directive:

app.directive('autofocus', function ($timeout) {
  return {
    replace: false,
    link: function (scope, element, attr) {
      scope.$watch(attr.autofocus,
        function (value) {
          if (value) {
            $timeout(function () {
              element[0].focus();
              console.log('focus called');
            });
          }
        }
      );
    }
  };
});

This is my current test:

describe('The autofocus directive', function () {
  var timeout;
  beforeEach(module('myApp'));
  beforeEach(inject(function($timeout) {
    timeout = $timeout;
  }));

  it('should set focus to first autofocus element', function () {
    var form = angular.element('<form />');
    var input1 = angular.element('<input type="text" name="first" />');
    var input2 = angular.element('<input type="text" name="second" autofocus="true" />');
    form.append(input1);
    form.append(input2);
    spyOn(input2[0], 'focus');
    timeout.flush();
    expect(input2[0].focus).toHaveBeenCalled();
  });

This is the (FAILED) output from karma:

$ node_modules/karma/bin/karma start test/karma.conf.js
INFO [karma]: Karma v0.12.23 server started at http:// localhost:8080/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux)]: Connected on socket U34UATs8jZDPB74AXpqR with id 96802943
PhantomJS 1.9.8 (Linux): Executed 0 of 20 SUCCESS (0 secs / 0 secs)
PhantomJS 1.9.8 (Linux) The autofocus directive should set focus to first autofocus element FAILED
Expected spy focus to have been called.
PhantomJS 1.9.8 (Linux): Executed 20 of 20 (1 FAILED) (0.156 secs / 0.146 secs)


Just adding

input[0].focus();

after spyOn(input[0], 'focus') the test succeeds, of course, but it's not what I want...


The final question is: How do I karma-test a directive which sets focus to an element ?


回答1:


When you call angular.element only in unit test, Angular will not understand the "autofocus" is a directive. Therefore, in your code :

var form = angular.element('<form />');
var input1 = angular.element('<input type="text" name="first" />');
var input2 = angular.element('<input type="text" name="second" autofocus="true" />');
form.append(input1);
form.append(input2);

Angular will not set autofocus as a directive and will not assign anythings relating to the "autofocus" directive that you have already declared. In order to do this in unit test, you have to use $compile to assign it with a scope. You can change the fiddle to like this to pass the test:

http://jsfiddle.net/ksqhmkqm/1/.

As you can see, I have created a new scope

scope = $rootScope.$new();

And create a new template

element = angular.element('<form><input type="text" name="first" /><input type="text" name="second" autofocus="true" /></form>');

And assign the created scope to this new template

$compile(element)(scope);
scope.$digest();

With this way, angular will understand the "autofocus" is a directive and assign all the event that you have created to this element that "autofocus" is working on.

The testing for the focus event I have followed this reference:

How do I check if my element has been focussed in a unit test



来源:https://stackoverflow.com/questions/26633684/angularjs-how-to-test-directive-which-gives-focus-to-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!