/
MegadraftEditor_test.js
184 lines (151 loc) · 5.01 KB
/
MegadraftEditor_test.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) 2016, Globo.com (https://github.com/globocom)
*
* License: MIT
*/
import React from "react";
import ReactDOM from "react-dom";
import TestUtils from "react-addons-test-utils";
import chai from "chai";
import sinon from "sinon";
import {Editor} from "draft-js";
import MegadraftEditor from "../../src/components/MegadraftEditor";
import Media from "../../src/components/Media";
import {editorStateFromRaw} from "../../src/utils";
import DEFAULT_PLUGINS from "../../src/plugins/default";
let expect = chai.expect;
let kba = function keyBindingAction() {};
describe("MegadraftEditor Component", () => {
beforeEach(function() {
const INITIAL_CONTENT = {
"entityMap": {},
"blocks": [
{
"key": "ag6qs",
"text": "Hello World!",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [
{
"offset": 0,
"length": 12,
"style": "BOLD"
},
{
"offset": 6,
"length": 6,
"style": "ITALIC"
}
],
"entityRanges": []
},
{
"key": "9vgd",
"text": "🍺",
"type": "atomic",
"depth": 0,
"inlineStyleRanges": [],
"data": {
"type": "image",
"src": "images/media.jpg",
"caption": "Picture from StockSnap.io",
"rightsHolder": "By Tim Marshall"
},
"entityRanges": []
}
]
};
kba = sinon.spy();
const keyBindings = [
{name: "save", isKeyBound: (e) => {return e.keyCode === 83 && e.ctrlKey;}, action: kba}
];
this.onChange = sinon.spy();
this.editorState = editorStateFromRaw(INITIAL_CONTENT);
this.component = TestUtils.renderIntoDocument(
<MegadraftEditor
editorState={this.editorState}
onChange={this.onChange}
keyBindings={keyBindings}/>
);
});
afterEach(function() {
ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(this.component).parentNode);
});
it("renders without problems", function() {
expect(this.component).to.exist;
});
it("has the initial text", function() {
expect(this.component.refs.editor.textContent).to.have.string("Hello World!");
});
it("renders Media component", function() {
const items = TestUtils.scryRenderedComponentsWithType(
this.component, Media
);
expect(items).to.have.length(1);
});
it("runs mediaBlockRenderer with non-atomic block", function() {
const block = {getType: function() {return "metal";}};
const result = this.component.mediaBlockRenderer(block);
expect(result).to.be.null;
});
it("runs mediaBlockRenderer with atomic block", function() {
function atomic() {}
atomic.prototype.getType = function() {return "atomic";};
atomic.prototype.getData = function(position) {
return {
toObject: function(){
return {type: "image"};
}
};
};
const block = new atomic();
const result = this.component.mediaBlockRenderer(block);
const plugin = DEFAULT_PLUGINS[0];
expect(result).to.deep.equal({
"component": Media,
"editable": false,
"props": {
"plugin": plugin,
"onChange": this.component.onChange,
"editorState": this.editorState,
"setReadOnly": this.component.setReadOnly
}
});
});
it("starts with default readOnly status", function() {
const items = TestUtils.scryRenderedComponentsWithType(
this.component, Editor
);
expect(items[0].props.readOnly).to.be.false;
});
it("changes readOnly status", function() {
const items = TestUtils.scryRenderedComponentsWithType(
this.component, Editor
);
this.component.setReadOnly(true);
expect(items[0].props.readOnly).to.be.true;
});
it("is capable of inserting soft line breaks", function() {
this.component.handleReturn({shiftKey: true});
const content = this.onChange.args[0][0].getCurrentContent();
const text = content.getFirstBlock().getText();
expect(text).to.be.equal("\nHello World!");
});
it("recognizes external key binding", function() {
const defaultKeyBinding = {keyCode: 66, ctrlKey: true};
expect(this.component.externalKeyBindings(defaultKeyBinding)).to.equal("bold");
const unknownKeyBinding = {keyCode: 70, ctrlKey: true};
expect(this.component.externalKeyBindings(unknownKeyBinding)).to.not.exist;
const externalKeyBinding = {keyCode: 83, ctrlKey: true};
expect(this.component.externalKeyBindings(externalKeyBinding)).to.equal("save");
});
it("handles external commands", function() {
const defaultCommand = "bold";
expect(this.component.handleKeyCommand(defaultCommand)).to.be.true;
const unknownCommand = "foo";
expect(this.component.handleKeyCommand(unknownCommand)).to.be.false;
const externalCommand = "save";
expect(this.component.handleKeyCommand(externalCommand)).to.be.true;
expect(kba).to.have.been.called;
});
});