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

Commit 4ed9193

Browse files
committed
add test for hasEntity
1 parent dc5925d commit 4ed9193

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

packages/utils/src/__test__/index.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
11
import { expect } from 'chai'
2-
import { EditorState, ContentState } from 'draft-js'
3-
import { getCharCount, getWordCount, getLineCount } from '../index'
2+
import {
3+
EditorState,
4+
ContentState,
5+
convertFromRaw,
6+
SelectionState,
7+
} from 'draft-js'
8+
import { getCharCount, getWordCount, getLineCount, hasEntity } from '../index'
9+
10+
describe('Has Entity', () => {
11+
const createEditorStateFromRaw = raw => {
12+
const contentState = convertFromRaw(raw)
13+
return EditorState.createWithContent(contentState)
14+
}
15+
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',
39+
},
40+
mutability: 'IMMUTABLE',
41+
type: 'IMAGE',
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+
const result = hasEntity(updatedEditorState, 'IMAGE')
56+
expect(result).to.equal(true)
57+
})
58+
})
459

560
describe('Char Counter', () => {
661
const createEditorStateFromText = text => {

0 commit comments

Comments
 (0)