How to mock an event like 'keypress' or 'keydown' in an AngularJS unit test?

BuildTester1

I've looked at previous articles but they seem to be updated, and I've hit a brick wall. Can anyone help me find a solution?

I've got a directive that has some events bind. It's a credit card number filter, so it adds a space every 4 numbers to make it easier to read. I'm trying to test it in karma/jasmine by feeding it the number 5555555555554444, and expect response for $viewValue of 5555 5555 5555 4444, but can't find a way to trigger the event to fire.

I've tried $setViewValue, $render(), $digest(), and I've tried some of the solutions from StackOverflow (over a year old) but they don't seem to work.

Is the only solution to use jQuery? And if so, does that jQuery solution still work? I've been trying to do this without including jQuery into my unit tests.

A code sample of what I'm working with:

    elem.bind('keypress', parseKeyPress);
    elem.bind('keydown contextmenu', parseKeyDown);
    elem.bind('cut paste', parseCutPaste);

    var parseHandler= function (value) {
      return value ? returnNumbersOnly(value) : '';
    };
    ctrl.$parsers.unshift(parseHandler);
    ctrl.$formatters.unshift(parseHandler);

Thoughts?

Daniel A. White

You should use elem.trigger(...) to emit those events as needed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I trigger a keyup/keydown event in an angularjs unit test?

From Dev

how to fire a keydown or keypress event

From Dev

how to mock $window to unit test AngularJS service?

From Java

How to use a keypress event in AngularJS?

From Dev

How to use Keydown event in AngularJS

From Dev

How do I mock the result of a promise in an AngularJS unit test?

From Dev

How to mock $window.location.replace in AngularJS unit test?

From Dev

How do I mock the result of a promise in an AngularJS unit test?

From Dev

how to write a unit test for keydown bind directive?

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

AngularJS override (mock) services for unit test

From Dev

angularjs unit test karma with jasmine mock dilemma

From Dev

AngularJS override (mock) services for unit test

From Dev

How do I trigger a mousemove event in an angularjs unit test

From Dev

How to imitate keypress event for angularJS input?

From Dev

How to change model in AngularJS on keydown event

From Dev

PyQt QTableView - Capture KeyDown / KeyPress Event

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

How to android unit test and mock a static method

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to unit test this function without mock asserts?

From Dev

How to mock browserHistory in unit test environment?

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock an Akka Actor to Unit Test a class?

From Dev

How to mock a generic parameter for a unit test in Java?

From Dev

How to mock a FormBuilder for Angular Component Unit Test

Related Related

  1. 1

    How do I trigger a keyup/keydown event in an angularjs unit test?

  2. 2

    how to fire a keydown or keypress event

  3. 3

    how to mock $window to unit test AngularJS service?

  4. 4

    How to use a keypress event in AngularJS?

  5. 5

    How to use Keydown event in AngularJS

  6. 6

    How do I mock the result of a promise in an AngularJS unit test?

  7. 7

    How to mock $window.location.replace in AngularJS unit test?

  8. 8

    How do I mock the result of a promise in an AngularJS unit test?

  9. 9

    how to write a unit test for keydown bind directive?

  10. 10

    How to mock springSecurityService in an unit test

  11. 11

    How to mock this unit test in Python?

  12. 12

    Correct way to mock an AngularJS Service in a unit test?

  13. 13

    AngularJS override (mock) services for unit test

  14. 14

    angularjs unit test karma with jasmine mock dilemma

  15. 15

    AngularJS override (mock) services for unit test

  16. 16

    How do I trigger a mousemove event in an angularjs unit test

  17. 17

    How to imitate keypress event for angularJS input?

  18. 18

    How to change model in AngularJS on keydown event

  19. 19

    PyQt QTableView - Capture KeyDown / KeyPress Event

  20. 20

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  21. 21

    How to android unit test and mock a static method

  22. 22

    How to mock AngularFire 2 service in unit test?

  23. 23

    How to mock the UserAgent property for an HttpRequest in a unit test?

  24. 24

    How to unit test this function without mock asserts?

  25. 25

    How to mock browserHistory in unit test environment?

  26. 26

    How to mock multiple components in camel unit test?

  27. 27

    How to mock an Akka Actor to Unit Test a class?

  28. 28

    How to mock a generic parameter for a unit test in Java?

  29. 29

    How to mock a FormBuilder for Angular Component Unit Test

HotTag

Archive