This repository was archived by the owner on Oct 11, 2022. It is now read-only.
forked from ngs/draft-js-markdown-shortcuts-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathchangeCurrentInlineStyle-test.js
More file actions
105 lines (104 loc) · 2.73 KB
/
changeCurrentInlineStyle-test.js
File metadata and controls
105 lines (104 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import Draft, { EditorState, SelectionState } from "draft-js";
import changeCurrentInlineStyle from "../changeCurrentInlineStyle";
describe("changeCurrentInlineStyle", () => {
const rawContentState = (text, inlineStyleRanges) => ({
entityMap: {},
blocks: [
{
key: "item1",
text,
type: "unstyled",
depth: 0,
inlineStyleRanges,
entityRanges: [],
data: {},
},
],
});
const selectionState = new SelectionState({
anchorKey: "item1",
anchorOffset: 5,
focusKey: "item1",
focusOffset: 5,
isBackward: false,
hasFocus: true,
});
const createEditorState = (...args) => {
const contentState = Draft.convertFromRaw(rawContentState(...args));
return EditorState.forceSelection(
EditorState.createWithContent(contentState),
selectionState
);
};
it("changes block type", () => {
const text = "foo `bar` baz";
const editorState = createEditorState(text, []);
const matchArr = ["`bar`", "bar"];
matchArr.index = 4;
matchArr.input = text;
const newEditorState = changeCurrentInlineStyle(
editorState,
matchArr,
"CODE"
);
expect(newEditorState).not.toEqual(editorState);
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
rawContentState("foo bar baz", [
{
length: 3,
offset: 4,
style: "CODE",
},
])
);
});
it("removes inline styles when applying code style", () => {
const text = "`some bold text`";
const editorState = createEditorState(text, [
{
length: 4,
offset: 6,
style: "BOLD",
},
]);
const matchArr = ["`some bold text`", "some bold text"];
matchArr.index = 0;
matchArr.input = text;
let newEditorState = changeCurrentInlineStyle(
editorState,
matchArr,
"CODE"
);
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
rawContentState("some bold text", [
{ length: 14, offset: 0, style: "CODE" },
])
);
});
it("handles a style terminator properly", () => {
const text = "foo **bar** baz";
const editorState = createEditorState(text, []);
const matchArr = ["**bar** ", "bar", " "];
matchArr.index = 4;
matchArr.input = text;
const newEditorState = changeCurrentInlineStyle(
editorState,
matchArr,
"BOLD"
);
expect(newEditorState).not.toEqual(editorState);
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
rawContentState(
"foo bar baz",
[
{
length: 3,
offset: 4,
style: "BOLD",
},
],
"BOLD"
)
);
});
});