如何在角度指令中对按键事件进行单元测试

spa*_*gas 6 events jasmine angularjs angularjs-directive

我已经创建了一个编辑指令来将html输入包装在一个奇特的框架中.

现在我正在创建一个单元测试来检查一旦输入输入,就会设置表单控制器的脏状态.这是我到目前为止所得到的,但它在第二次预期失败了.

这有什么不对?

提前致谢!

describe('dirty', function () {
    it('false', function () {
        var scope = $rootScope.$new(),
            form = $compile('<form name="form"><edit ng-model="model"></edit></form>')(scope),
            input = form.find('input');
        scope.$digest();
        expect(scope.form.$dirty).toBeFalsy();
        input.triggerHandler('keydown', { which: 65 });
        expect(scope.form.$dirty).toBeTruthy();
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑:

对于所有重要的事情,它归结为这个plunker(没有jQuery)......或者这个使用jQuery

任何的想法?

aet*_*aet 1

ngKeySpec.js 中的角度单元测试很有帮助:

it('should get called on a keydown', inject(function($rootScope, $compile) {
    element = $compile('<input ng-keydown="touched = true">')($rootScope);
    $rootScope.$digest();
    expect($rootScope.touched).toBeFalsy();

    browserTrigger(element, 'keydown');
    expect($rootScope.touched).toEqual(true);
  }));
Run Code Online (Sandbox Code Playgroud)