-
Notifications
You must be signed in to change notification settings - Fork 912
/
Copy pathreplaceTextTest.js
311 lines (252 loc) · 8.57 KB
/
replaceTextTest.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// External dependencies.
import { EditorState, ContentState, SelectionState } from "draft-js";
// Internal dependencies.
import {
getTrigger,
hasWhitespaceAt,
getCaretOffset,
getAnchorBlock,
insertText,
removeSelectedText,
moveCaret,
} from "../src/helpers/replaceText";
describe( "SnippetEditor text utilities", () => {
describe( "getTrigger", () => {
it( "returns a % without spaces", () => {
const actual = getTrigger( false, false );
const expected = "%";
expect( actual ).toBe( expected );
} );
it( "returns a % with a prepended space", () => {
const actual = getTrigger( true, false );
const expected = " %";
expect( actual ).toBe( expected );
} );
it( "returns a % with a appended space", () => {
const actual = getTrigger( false, true );
const expected = "% ";
expect( actual ).toBe( expected );
} );
it( "returns a % surrounded by spaces", () => {
const actual = getTrigger( true, true );
const expected = " % ";
expect( actual ).toBe( expected );
} );
} );
describe( "hasWhitespaceAt", () => {
const whitespaceText = "a \nb";
it( "identifies the letter a as non-whitespace", () => {
const actual = hasWhitespaceAt( whitespaceText, 0 );
const expected = false;
expect( actual ).toBe( expected );
} );
it( "identifies a space as whitespace", () => {
const actual = hasWhitespaceAt( whitespaceText, 1 );
const expected = true;
expect( actual ).toBe( expected );
} );
it( "identifies a newline as whitespace", () => {
const actual = hasWhitespaceAt( whitespaceText, 2 );
const expected = true;
expect( actual ).toBe( expected );
} );
it( "identifies a negative index as whitespace", () => {
const actual = hasWhitespaceAt( whitespaceText, -1 );
const expected = true;
expect( actual ).toBe( expected );
} );
it( "identifies an out of bounds index as whitespace", () => {
const actual = hasWhitespaceAt( whitespaceText, whitespaceText.length );
const expected = true;
expect( actual ).toBe( expected );
} );
} );
describe( "getCaretOffset", () => {
it( "returns the start offset when there is no collapse", () => {
const selection = new SelectionState( {
anchorKey: "foo",
anchorOffset: 1,
focusKey: "bar",
focusOffset: 1,
isBackward: false,
hasFocus: false,
} );
const actual = getCaretOffset( selection );
const expected = selection.getStartOffset();
expect( actual ).toBe( expected );
} );
it( "returns the start offset when there is a collapse", () => {
const selection = new SelectionState( {
anchorKey: "foo",
anchorOffset: 2,
focusKey: "bar",
focusOffset: 10,
isBackward: false,
hasFocus: false,
} );
const actual = getCaretOffset( selection );
const expected = selection.getStartOffset();
expect( actual ).toBe( expected );
} );
it( "returns the end offset when there is a collapse and it is backwards", () => {
const selection = new SelectionState( {
anchorKey: "foo",
anchorOffset: 10,
focusKey: "bar",
focusOffset: 2,
isBackward: true,
hasFocus: false,
} );
const actual = getCaretOffset( selection );
const expected = selection.getEndOffset();
expect( actual ).toBe( expected );
} );
} );
describe( "getAnchorBlock", () => {
it( "returns the text that it is created with", () => {
const expected = "The text of this block.";
const content = ContentState.createFromText( expected );
const editor = EditorState.createWithContent( content );
const block = getAnchorBlock( editor.getCurrentContent(), editor.getSelection() );
const actual = block.getText();
expect( actual ).toBe( expected );
} );
} );
describe( "insertText", () => {
it( "equals the inserted text", () => {
let editor = EditorState.createEmpty();
const expected = "The text to insert.";
editor = insertText( editor, expected );
const content = editor.getCurrentContent();
const actual = content.getPlainText();
expect( actual ).toBe( expected );
} );
it( "inserts the text at the caret", () => {
const expected = "This is a slightly more complicated text.\n" +
"With just another line.";
let content = ContentState.createFromText(
"This is a slightly more complicated text.\n" +
"With another line."
);
let editor = EditorState.createWithContent( content );
const block = content.getLastBlock();
const blockKey = block.getKey();
editor = moveCaret( editor, 5, blockKey );
editor = insertText( editor, "just " );
content = editor.getCurrentContent();
const actual = content.getPlainText();
expect( actual ).toBe( expected );
} );
it( "ignores a request when the selection is not collapsed", () => {
const expected = "This is the content.";
let content = ContentState.createFromText( expected );
let editor = EditorState.createWithContent( content );
const block = content.getFirstBlock();
const blockKey = block.getKey();
const selection = SelectionState
.createEmpty( blockKey )
.merge( {
anchorOffset: 8,
focusOffset: 19,
} );
editor = EditorState.acceptSelection( editor, selection );
editor = insertText( editor, "Text that will not get inserted" );
content = editor.getCurrentContent();
const actual = content.getPlainText();
expect( actual ).toBe( expected );
} );
} );
describe( "removeSelectedText", () => {
it( "removes the selected text", () => {
let content = ContentState.createFromText( "This is the content." );
let editor = EditorState.createWithContent( content );
const block = content.getFirstBlock();
const blockKey = block.getKey();
const selection = SelectionState
.createEmpty( blockKey )
.merge( {
anchorOffset: 8,
focusOffset: 19,
} );
editor = EditorState.acceptSelection( editor, selection );
editor = removeSelectedText( editor );
const expected = "This is .";
content = editor.getCurrentContent();
const actual = content.getPlainText();
expect( actual ).toBe( expected );
} );
it( "removes the selected text spanning over multiple blocks", () => {
let content = ContentState.createFromText(
"This is a longer text.\n" +
"With multiple lines.\n" +
"In which we will expand the selection.\n" +
"To just remove the selected text again."
);
let editor = EditorState.createWithContent( content );
const blocks = content.getBlockMap().toArray();
const anchorBlockKey = blocks[ 1 ].getKey();
const focusBlockKey = blocks[ 3 ].getKey();
const selection = SelectionState
.createEmpty( anchorBlockKey )
.merge( {
anchorOffset: 5,
focusKey: focusBlockKey,
focusOffset: 28,
} );
editor = EditorState.acceptSelection( editor, selection );
editor = removeSelectedText( editor );
const expected = "This is a longer text.\n" +
"With text again.";
content = editor.getCurrentContent();
const actual = content.getPlainText();
expect( actual ).toBe( expected );
} );
} );
describe( "moveCaret", () => {
it( "moves the caret to the index", () => {
const content = ContentState.createFromText(
"This is a longer text.\n" +
"With multiple lines.\n" +
"To move the caret in."
);
let editor = EditorState.createWithContent( content );
const blocks = content.getBlockMap().toArray();
const blockKey = blocks[ 1 ].getKey();
const selection = SelectionState.createEmpty( blockKey );
const index = 5;
editor = EditorState.acceptSelection( editor, selection );
editor = moveCaret( editor, index );
const expected = SelectionState
.createEmpty( blockKey )
.merge( {
anchorOffset: index,
focusOffset: index,
} );
const actual = editor.getSelection();
expect( actual ).toEqual( expected );
} );
it( "moves the caret to the index of another block", () => {
const content = ContentState.createFromText(
"This is a longer text.\n" +
"With multiple lines.\n" +
"To move the caret in."
);
let editor = EditorState.createWithContent( content );
const blocks = content.getBlockMap().toArray();
const originalBlockKey = blocks[ 1 ].getKey();
const selection = SelectionState.createEmpty( originalBlockKey );
const moveToBlockKey = blocks[ 2 ].getKey();
const index = 5;
editor = EditorState.acceptSelection( editor, selection );
editor = moveCaret( editor, index, moveToBlockKey );
const expected = SelectionState
.createEmpty( moveToBlockKey )
.merge( {
anchorOffset: index,
focusOffset: index,
} );
const actual = editor.getSelection();
expect( actual ).toEqual( expected );
} );
} );
} );