Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 355c789

Browse files
authored
Merge pull request #86 from withspectrum/new-test-suite
Add new plugin test suite
2 parents 23e8a54 + 3dbce9a commit 355c789

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`markdown should convert asteriks to bold text 1`] = `
4+
Object {
5+
"blocks": Array [
6+
Object {
7+
"data": Object {},
8+
"depth": 0,
9+
"entityRanges": Array [],
10+
"inlineStyleRanges": Array [
11+
Object {
12+
"length": 4,
13+
"offset": 5,
14+
"style": "BOLD",
15+
},
16+
],
17+
"key": "item1",
18+
"text": "Some text",
19+
"type": "unstyled",
20+
},
21+
],
22+
"entityMap": Object {},
23+
}
24+
`;
25+
26+
exports[`markdown should not have sticky inline styles 1`] = `
27+
Object {
28+
"blocks": Array [
29+
Object {
30+
"data": Object {},
31+
"depth": 0,
32+
"entityRanges": Array [],
33+
"inlineStyleRanges": Array [
34+
Object {
35+
"length": 4,
36+
"offset": 5,
37+
"style": "BOLD",
38+
},
39+
],
40+
"key": "item1",
41+
"text": "Some texta",
42+
"type": "unstyled",
43+
},
44+
],
45+
"entityMap": Object {},
46+
}
47+
`;

src/__test__/plugin-new.test.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import Draft, {
2+
EditorState,
3+
SelectionState,
4+
ContentBlock,
5+
convertToRaw,
6+
} from "draft-js";
7+
import createMarkdownPlugin from "../";
8+
9+
describe("markdown", () => {
10+
it("should convert asteriks to bold text", () => {
11+
const { handleBeforeInput } = createMarkdownPlugin();
12+
const setEditorState = jest.fn();
13+
const before = EditorState.moveSelectionToEnd(
14+
EditorState.createWithContent(
15+
Draft.convertFromRaw({
16+
entityMap: {},
17+
blocks: [
18+
{
19+
key: "item1",
20+
text: "Some *text",
21+
type: "unstyled",
22+
depth: 0,
23+
inlineStyleRanges: [],
24+
entityRanges: [],
25+
data: {},
26+
},
27+
],
28+
})
29+
)
30+
);
31+
expect(handleBeforeInput("*", before, { setEditorState })).toEqual(
32+
"handled"
33+
);
34+
const raw = convertToRaw(
35+
setEditorState.mock.calls[0][0].getCurrentContent()
36+
);
37+
expect(raw).toMatchSnapshot();
38+
});
39+
40+
it("should not do anything to existing inline styles when within them", () => {
41+
const { handleBeforeInput } = createMarkdownPlugin();
42+
const setEditorState = jest.fn();
43+
const boldInlineStyleRange = {
44+
length: 4,
45+
offset: 5,
46+
style: "BOLD",
47+
};
48+
const before = EditorState.forceSelection(
49+
EditorState.createWithContent(
50+
Draft.convertFromRaw({
51+
entityMap: {},
52+
blocks: [
53+
{
54+
key: "item1",
55+
text: "Some text",
56+
type: "unstyled",
57+
depth: 0,
58+
inlineStyleRanges: [boldInlineStyleRange],
59+
entityRanges: [],
60+
data: {},
61+
},
62+
],
63+
})
64+
),
65+
new SelectionState({
66+
anchorKey: "item1",
67+
anchorOffset: 6,
68+
focusKey: "item1",
69+
focusOffset: 6,
70+
isBackward: false,
71+
hasFocus: true,
72+
})
73+
);
74+
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
75+
"not-handled"
76+
);
77+
});
78+
79+
it("should not have sticky inline styles", () => {
80+
const { handleBeforeInput } = createMarkdownPlugin();
81+
const setEditorState = jest.fn();
82+
const boldInlineStyleRange = {
83+
length: 4,
84+
offset: 5,
85+
style: "BOLD",
86+
};
87+
const before = EditorState.moveSelectionToEnd(
88+
EditorState.createWithContent(
89+
Draft.convertFromRaw({
90+
entityMap: {},
91+
blocks: [
92+
{
93+
key: "item1",
94+
text: "Some text",
95+
type: "unstyled",
96+
depth: 0,
97+
inlineStyleRanges: [boldInlineStyleRange],
98+
entityRanges: [],
99+
data: {},
100+
},
101+
],
102+
})
103+
)
104+
);
105+
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
106+
"handled"
107+
);
108+
const raw = convertToRaw(
109+
setEditorState.mock.calls[0][0].getCurrentContent()
110+
);
111+
expect(raw.blocks[0].inlineStyleRanges[0]).toEqual(boldInlineStyleRange);
112+
expect(raw).toMatchSnapshot();
113+
});
114+
});

0 commit comments

Comments
 (0)