Draft-js: 0.10.2 fails on a test: global.getSelection().anchorNode undefined

Created on 22 Sep 2017  路  7Comments  路  Source: facebook/draft-js

A test with these 3 lines with an error. Removing the last line make it to succeed.
It uses enzyme.

wrapper.find('.public-DraftEditor-content').simulate('beforeInput', { data: '@' });
wrapper.find('.public-DraftEditor-content').simulate('keydown', { which: KEY_ESC });
wrapper.find('.public-DraftEditor-content').simulate('beforeInput', { data: 't' });

So it seems this line anchorNode is undefined, not null.
https://github.com/facebook/draft-js/blob/master/src/component/handlers/edit/editOnBeforeInput.js#L159

TypeError: Cannot read property 'nodeType' of undefined

      at editOnBeforeInput (node_modules/draft-js/lib/editOnBeforeInput.js:112:74)
      at node_modules/draft-js/lib/DraftEditor.react.js:146:19
      at Object.invokeGuardedCallback [as invokeGuardedCallbackWithCatch] (node_modules/react-dom/lib/ReactErrorUtils.js:26:5)
      at executeDispatch (node_modules/react-dom/lib/EventPluginUtils.js:83:21)
      at Object.executeDispatchesInOrder (node_modules/react-dom/lib/EventPluginUtils.js:108:5)
      at executeDispatchesAndRelease (node_modules/react-dom/lib/EventPluginHub.js:43:22)
      at executeDispatchesAndReleaseSimulated (node_modules/react-dom/lib/EventPluginHub.js:51:10)
      at forEachAccumulated (node_modules/react-dom/lib/forEachAccumulated.js:26:8)
      at Object.processEventQueue (node_modules/react-dom/lib/EventPluginHub.js:252:7)
      at node_modules/react-dom/lib/ReactTestUtils.js:351:22
      at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-dom/lib/Transaction.js:143:20)
      at Object.batchedUpdates (node_modules/react-dom/lib/ReactDefaultBatchingStrategy.js:62:26)
      at Object.batchedUpdates (node_modules/react-dom/lib/ReactUpdates.js:97:27)
      at node_modules/react-dom/lib/ReactTestUtils.js:349:18
      at ReactWrapper.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:776:11)
      at ReactWrapper.single (node_modules/enzyme/build/ReactWrapper.js:1421:25)
      at ReactWrapper.simulate (node_modules/enzyme/build/ReactWrapper.js:769:14)
needs more info

Most helpful comment

I'm running into the same error as @eldh periodically. Still hunting for a solid repro case...

All 7 comments

Hi @tleunen - can you give me more context for this?

At Facebook, we don't usually use Enzyme to simulate events on Draft, so I'm not sure this is something we expect to work. Seeing the whole test would be useful, or knowing why you are writing this test.

Thanks!

There's not much more info to be honest. The test is actually testing something outside of draft, but I need to input data there to make sure the rest behaves properly.

The test is something like:

  • Press a letter, see that a suggestion box renders, like a mentions box.
  • Press escape, see the suggestion disappears
  • Press a new letter, see the suggestions is not rendered again

It worked properly with 0.10.1.. Is the new condition on the nodeType too strict? Because the reason is really that the anchor is undefined, not null.

Ah - I was focused on the test format itself, but I see now the main issue is that the check we added for null isn't catching the undefined value. That's what you meant by

anchorNode is undefined, not null.

I think it would be reasonable to change the line from

 nativeSelection.anchorNode !== null &&

to

// intentionally check for `null` and `undefined`
nativeSelection.anchorNode != null &&

Would you like to submit a PR?

I expect to get this out in a minor release in the next couple of weeks.

Np. Thank you @flarnie

I'm still seeing this (or a very similar error) in v0.10.4, coming from editOnInput. I'll make a PR cause I don't know if my fix is nice or not.

I'm running into the same error as @eldh periodically. Still hunting for a solid repro case...

Was this page helpful?
0 / 5 - 0 ratings