1

I have a jQuery plugin that limits character entry with an input element. This plugin (similar to numeric) hooks into keydown and keyup to allow certain characters prevent the event under certain conditions:

jqueryInputElement.on('keydown', function(event){
    if (wrongThingEntered(event))
        event.stopPropagation();
        event.preventDefault();
});

wrongThingEntered does funky things with the character code and modifier keys in the event and the current inputted value (missing from this example). For this reason I need to simulate character entry from Karma for different devices so that I can test the value after keydowns and keyups etc.

I understand that I can simulate using jQuery trigger or similar, but after going down the rabbit-hole of what keys map to what character codes on different devices, I came to the conclusion that triggering these low-level events did not solve my problem, and the tests weren't really testing anything.

I'm really after something like this:

describe 'hello world', ->
    element = undefined
    beforeEach ->
        element = $('<input>')
        element.customPlugin()
        element.focus()
    afterEach ->
        element.remove()

    describe 'keyboard events work', ->
        it 'receives some input', ->
            karma.user.presses '4' # not sure about this part
            expect element.val()
                .toBe '4'
        it 'respects paste commands', ->
            karma.system.pasteBuffer = "some text to paste" # and also this part
            karma.user.pastes() # ... and this part
            expect element.val()
                .toBe "some text to paste"

Is this possible with Karma (in my case running phantomJS)?

sennett
  • 8,014
  • 9
  • 46
  • 69
  • I think that Karma might not be the tool for this type of testing. I've seen some people talk about String.charCodeAt (http://stackoverflow.com/a/596580/614523), so I'll try this, but perhaps I should look at another tool to run these types of tests. – sennett Feb 21 '14 at 14:31
  • Just wondering, have you tried https://github.com/tmcw/happen to trigger a Keyboard event? – MarcoL Feb 24 '14 at 09:19

0 Answers0