question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to enter numbers in an input using `trigger()`

See original GitHub issue

Version

1.0.0-beta.12

Reproduction link

https://codesandbox.io/s/n768kwm6wj note: the dummy VueInput component and the spec code is correct, but I can’t make vue-test-utils work there

Steps to reproduce

Following the keyboard example from the documentation, I’m trying to input arbitrary keys into an input.

  1. Have a component VueInput that generated a top level input DOM element <vue-input type='text'>
  2. With vue-test-utils, try to enter the keys home then 1 using:
describe('Dummy VueInput.vue', () => {
    it('should accept number keys', () => {
        const wrapper = mount(VueInput);
        const input = wrapper.find('input');
        expect(input.element.value).toEqual('');

        // Test inputting a number
        input.trigger('click'); //FIXME Is this really needed? Isn't the focus already on the input since it's the generated DOM element?
        input.trigger('keydown.home');
        input.trigger('keydown', { //FIXME Fails
            key: '1',
            keyCode: 49,
            which: 49,
        });
        expect(input.is('input')).toBe(true);
        expect(input.element.value).toEqual('1');
    });
});

What is expected?

The input element value should be 1.

What is actually happening?

The input element value is unchanged and still equal to "".

Bonus question:

Do we need to manually (and painfully) convert each key we want to enter to its keyCode (which value), or is there a quicker and better way to enter multiple keys one after another in one go (like 1, then 5, then 8, perhaps using something like wrapper.keys('158')? (see how it’s done in Selenium-based webdriver.io in that example)).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
pierremanceauxcommented, Apr 6, 2018

+1 @AlexandreBonneau , I’m currently facing exactly the same issue.

7reactions
AlexandreBonneaucommented, Apr 3, 2018

I’m not seeing how “You can test those things by changing the value” in a case where masking is involved.

For instance with the vue-autoNumeric component, setting the input.value directly (using input.value = 'foo') is managed differently than when changing the input value manually.

In the former case, a watch on the value is used, while on the latter the keydown, keypress, keyup events are closely monitored by the AutoNumeric library.

Moreover, setting the value directly cannot allows to test some keys that do not produce a change in the input value; for instance when you have the default settings on a vue-autonumeric component and the value 1,234,567.89 in it, if the caret is placed here 1,234,5|67.89 and you enter a single ArrowLeft key, then the result should be 1,234|,567.89 (not 1,234,|567.89).

I guess if vue-test-utils trigger() function does not simulate key* events, all this is untestable.

Could you please confirm?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger a button click with JavaScript on the Enter key in a text ...
And, I only want the Enter key to click this specific button if it is pressed from within this one text box, nothing...
Read more >
Solved: OnChange() of a textInput doesn't work when enter
Solved: Hi, I have a Text Input object. The "format" field is set to Number. If I am in the Text Input and...
Read more >
Why the number input is the worst input - Stack Overflow Blog
Chances are likely that you used the input type="number ” because you expected an integer to represent age or quantity. However, the number...
Read more >
Activate a Stateflow Chart by Sending Input Events - MathWorks
Activate a Stateflow Chart by Using Edge Triggers. An edge-triggered input event causes a Stateflow chart to execute during the current time step...
Read more >
25.3.1 Trigger Syntax and Examples - MySQL :: Developer Zone
Here is a simple example that associates a trigger with a table, to activate for INSERT operations. The trigger acts as an accumulator,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found