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.

Random behaviour when trying to simulate typing with enzyme

See original GitHub issue

Do you want to request a feature or report a bug?

I have a question that is too complicated for being asked in slack

What’s the current behavior?

I am new to automatic testing and trying to write tests for our custom slate editor, using karma (with google chrome) and enzyme. I thought it would be best to test by simulate typing on the editor and expecting a certain state change. After a while, I figured out I had to simulate ‘beforeInput’, ‘input’ and ‘keydown’ events on the Editors ‘Content’ Component, and I had to use the enzymes mount functions ‘attachTo’ parameter in order to do so. So I wrote a ‘simlateTyping’ function looking like the following:

export function simulateKeyPress(wrapper, key) {
  const events = [
    'keydown',
    'beforeinput',
    // 'input',
  ]
  events.forEach((event, i) => {
    const eventObj = {
      altKey: false,
      charCode: 0,
      ctrlKey: false,
      metaKey: false,
      shiftKey: false,
      which: keycode(key),
      keyCode: keycode(key),
      data: key,
      key,
    }
    wrapper.simulate(event, eventObj)
  })
}

export function simulateTyping(wrapper, str) {
  const chars = str.split('')
  chars.forEach(char => simulateKeyPress(wrapper, char))
}

I wrote a basic test for the editor:

describe('<SlateEditor />', () => {
  stateChangeExpectation('can type text', canTypeTextOutput, (wrapper, editorStore) => {
    editorStore.doUpdateState(
      editorStore.state.transform().selectAll().collapseToEnd().focus().apply(),
    )
    const contentWrapper = wrapper.find('Content')
    simulateTyping(contentWrapper, '12345678')
  })
})

The stateChangeExpectation function basically gives you an enzyme wrapper of the Editor comonent and the store in which the state is saved. After the method you pass is executed it expects the output specified as the second parameter. In my case the initial state was just a plain

Hello World!

and after the function executed a

Hello World!12345678

is expected, obviously ( I will list the input state and expected output below )

What’s the expected behavior?

However what I keep getting is this:

Hello World!2468|7531

(With the selection after the 8) Because apparently, the cursor doesn’t jump with the last typed character every second time. I’ve figured out that the reason for that lies somewhere in this code section https://github.com/ianstormtaylor/slate/blob/master/src/plugins/core.js#L114-L130 Where the condition is only fulfilled on every second character that is inserted. Does someone have any clue why this isn’t working like regular typing is? Would be really nice if someone could have a look at that.

I couldn’t create a JSFiddle because there is no enzyme CDN unfortunately. If you need it though I can create a little GitHub repo reproducing that issue.

Finally heres the initialState:

document:
  data: {}
  kind: document
  nodes:
    - data: {}
      kind: block
      isVoid: false
      type: paragraph
      nodes:
        - kind: text
          ranges:
            - kind: range
              text: 'Hello World!'
              marks: []
kind: state

And here the expected output

document:
  data: {}
  kind: document
  nodes:
    - data: {}
      kind: block
      isVoid: false
      type: paragraph
      nodes:
        - kind: text
          ranges:
            - kind: range
              text: 'Hello World!12345678'
              marks: []
kind: state

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ianstormtaylorcommented, Aug 1, 2017

Hey, thanks for using Slate! Unfortunately, we can’t offer support for usage questions in the issues here because it becomes overwhelming to maintain the project if the issues are filled with questions.

However, we do have a Slack channel and people are constantly asking and answering questions in there. So I’m going to close this issue, but I definitely recommend joining the Slack channel if you want to find people that might be able to help.

Thanks for understanding!

1reaction
wmhiltoncommented, Jul 13, 2018

I really dislike this policy, because it means - even if this was answered - the answer is gone now because Slack has a very short search history. @marschi Do you remember how you solved this? I’m investigating using enzyme to simulate typing in slate as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Investigating Enzyme's keypress simulation on input fields
The 'keypress' simulation is a bad idea for emulating typing in an input field with a onChange event handler, unless you have specifically ......
Read more >
Molecular dynamics simulation for all - PMC - NCBI - NIH
These simulations capture the behavior of proteins and other biomolecules in full atomic detail and at very fine temporal resolution. Major improvements in ......
Read more >
Jest / Enzyme not properly simulating my component method?
I have an input and when the "enter" key is pressed, using "onKeyPress" I want to set ...
Read more >
Handling Mouse Events in Your React Component Tests
Testing a React component will most often involve some type of user interaction happening before you validate the expected outcome.
Read more >
Experimental and Simulation Study of the Solvent Effects on ...
Spherical lignin nanoparticles (LNPs) fabricated via nanoprecipitation of dissolved lignin are among the most attractive biomass-derived ...
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