Draft-js: How to mock text selection in test environment

Created on 28 Feb 2017  路  3Comments  路  Source: facebook/draft-js

Is there a way to mock selecting of an editor's existing text within a test environment?

For example, I'd like select any text, then click a button that applies a link, in order to determine if the correct block type was applied.

I attempted manipulating the selectionState using Immutable's toJS and fromJS, but then lost functions available to selectionState.

Are there other methods by which I could manually set my selection?

Most helpful comment

Yes, this seems like it will do it - thanks!

All 3 comments

I like to use Immutable's merge to update selection in tests. For example:

const selection = editorState.getSelection();

const newSelection = selection.merge({
  anchorKey: someBlockKey,
  anchorOffset: offsetInSomeBlock,
  focusKey: someOtherBlockKey,
  focusOffset: offsetInSomeOtherBlock
});

const newEditorState = EditorState.forceSelection(editorState, newSelection);

Does that seem like it might work better for you? Apologies if I misunderstood the question!

Yes, this seems like it will do it - thanks!

馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ufo22940268 picture ufo22940268  路  3Comments

sandorvasas picture sandorvasas  路  3Comments

vierno picture vierno  路  3Comments

mvnnn picture mvnnn  路  3Comments

jackmatrix picture jackmatrix  路  3Comments