I've got a suite that passes or fails seemingly at random, and I think it's probably something to do with the asynch nature of event handling but I can't find a solution. I am testing a simple directive that prevents the default action on the enter keypress (it is being used in a form in the actual app).
Relevant code:
describe('preventSubmitOnEnter', function(){
var e;
var e2;
var element;
beforeEach(function(){
element = $compile('<input type="submit" prevent-submit-on-enter />')($rootScope);
$('body').append(element);
e = $.Event('keypress');
e.keyCode = 13;
e.which = 13;
$(element).trigger(e);
e2 = $.Event('keypress');
e2.keyCode = 110;
e2.which = 110;
$(element).trigger(e2);
});
afterEach(function(){
$('input[type="submit"]').remove();
});
it('should prevent default event handling for enter keypress and do nothing otherwise', function(){
expect(e.isDefaultPrevented()).toBe(true);
expect(e.isPropagationStopped()).toBe(true);
expect(e2.isDefaultPrevented()).toBe(false);
expect(e2.isPropagationStopped()).toBe(false);
});
})