Testing for focus an AngularJS directive

前端 未结 2 1843
清歌不尽
清歌不尽 2021-02-05 05:38

How can you test for focus in an AngularJS directive? I would expect the following to work:

describe(\'focus test\', function(){
    it(\'should focus element\',         


        
相关标签:
2条回答
  • 2021-02-05 06:19

    In Jasmine 2, this is now:

    beforeEach(function() {
      jasmine.addMatchers({
        toHaveFocus: function() {
          return {
            compare: function(actual) {
              return {
                pass: document.activeElement === actual[0]
              };
            }
          };
        }
      });
    });
    
    0 讨论(0)
  • 2021-02-05 06:35

    Try 'document.activeElement' instead of ':focus'. I haven't tested it in karma, but $('document.activeElement') behaves as desired under standard jQuery.

    0 讨论(0)
提交回复
热议问题