2

I currently use jasmine to test my angularjs code and I have some autocomplete-like directive which should start to request data from server when a user has typed some chars into the input.

Now, here's my test code:

        var element = linkingFn(scope);

    var $input = $("input", element);
    expect($input.length).toBe(1);

    var keyVal = 111;   // 'o'
    $input.trigger({
        type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
    });
    keyVal = 110;   // 'n'
    $input.trigger({
        type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
    });
    keyVal = 101;   // 'e'
    $input.trigger({
        type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
    });

    // THIS DOES NOT WORK
    expect($input.val()).toBe("one");

$input.val() is always empty. My understanding is that since it's not running in a browser there are no events fired (obviously) so the val() is not set. So what are my options here?

AlexB
  • 4,167
  • 4
  • 45
  • 117
  • 1
    Welcome to a world of pain. Here is my question from a while back on similar lines - maybe you'll find something useful here: http://stackoverflow.com/questions/22574431/testing-keydown-events-in-jasmine-with-specific-keycode – Michael Bromley Aug 21 '14 at 11:10

0 Answers0