How to unit test keypress event in angular directive

I created an editing directive to wrap the html input in a fancy frame.

Now I am creating a unit test to verify that as soon as I type in the input, the dirty state of the form controller is set. This is what I got so far, but it fails in the second wait.

What is wrong here?

Thanks in advance!

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(); }); }); 

Edit:

For everything that matters, this applies to this plunker (without jQuery) ... or this one using jQuery

Any idea?

+6
source share
1 answer

Angular unit tests in ngKeySpec.js were useful:

 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); })); 
+1
source

All Articles