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

后端 未结 4 1728
梦毁少年i
梦毁少年i 2020-12-14 18:43

I have the following directive to autofocus a field:

.directive(\'ngAutofocus\', function ($timeout) {
    return {
        restrict: \'A\',
        link: fu         


        
4条回答
  •  隐瞒了意图╮
    2020-12-14 19:19

    I figured it out, and it was pretty obvious actually;

    it('should set the focus on timeout', function () {
        spyOn(elm[0],'focus');
        $timeout.flush();
        expect(elm[0].focus).toHaveBeenCalled();
    })
    

    My problem was two-fold:

    1. I wasn't calling the timeout flush function so timeout wasn't occuring, and
    2. I was trying to look at the focus attribute of the element whereas just looking at the call of the focus() function is much more like unit-testing. The focus attribute is something that really belongs to the e2e testing territory.

提交回复
热议问题