Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 1f08c20

Browse files
committed
add test for getCurrentEntityKey
1 parent 4ed9193 commit 1f08c20

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

packages/utils/src/__test__/index.js

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,67 @@ import {
55
convertFromRaw,
66
SelectionState,
77
} from 'draft-js'
8-
import { getCharCount, getWordCount, getLineCount, hasEntity } from '../index'
8+
import { getCharCount, getWordCount, getLineCount, hasEntity, getCurrentEntityKey } from '../index'
99

10-
describe('Has Entity', () => {
10+
describe('Entity', () => {
1111
const createEditorStateFromRaw = raw => {
1212
const contentState = convertFromRaw(raw)
1313
return EditorState.createWithContent(contentState)
1414
}
1515

16-
it('create editorState and check if has Entity', () => {
17-
const rawContent = {
18-
blocks: [
19-
{
20-
text: 'Hey there duder',
21-
},
22-
{
23-
type: 'atomic',
24-
text: ' ',
25-
entityRanges: [
26-
{
27-
key: 0,
28-
length: 1,
29-
offset: 0,
30-
},
31-
],
32-
},
33-
],
34-
entityMap: {
35-
0: {
36-
data: {
37-
title: 'Kitten',
38-
src: 'https://placekitten.com/200/200',
16+
const rawContent = {
17+
blocks: [
18+
{
19+
text: 'Hey there duder',
20+
},
21+
{
22+
type: 'atomic',
23+
text: ' ',
24+
entityRanges: [
25+
{
26+
key: 0,
27+
length: 1,
28+
offset: 0,
3929
},
40-
mutability: 'IMMUTABLE',
41-
type: 'IMAGE',
30+
],
31+
},
32+
],
33+
entityMap: {
34+
0: {
35+
data: {
36+
title: 'Kitten',
37+
src: 'https://placekitten.com/200/200',
4238
},
39+
mutability: 'IMMUTABLE',
40+
type: 'IMAGE',
4341
},
44-
}
45-
const editorState = createEditorStateFromRaw(rawContent)
46-
const blockKey = editorState
47-
.getCurrentContent()
48-
.getLastBlock()
49-
.getKey()
50-
const selectionState = SelectionState.createEmpty(blockKey)
51-
const updatedEditorState = EditorState.forceSelection(
52-
editorState,
53-
selectionState
54-
)
42+
},
43+
}
44+
45+
const editorState = createEditorStateFromRaw(rawContent)
46+
const blockKey = editorState
47+
.getCurrentContent()
48+
.getLastBlock()
49+
.getKey()
50+
const selectionState = SelectionState.createEmpty(blockKey)
51+
const updatedEditorState = EditorState.forceSelection(
52+
editorState,
53+
selectionState
54+
)
55+
56+
it('create editorState and check if the selection has Entity', () => {
5557
const result = hasEntity(updatedEditorState, 'IMAGE')
5658
expect(result).to.equal(true)
5759
})
60+
61+
it('create editorState and get entity key at selection', () => {
62+
const result = getCurrentEntityKey(updatedEditorState)
63+
const entityKey = updatedEditorState
64+
.getCurrentContent()
65+
.getBlockForKey(blockKey)
66+
.getEntityAt(0)
67+
expect(result).to.equal(entityKey)
68+
})
5869
})
5970

6071
describe('Char Counter', () => {

0 commit comments

Comments
 (0)