-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
writing-flow.test.js
425 lines (357 loc) · 15.4 KB
/
writing-flow.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* WordPress dependencies
*/
import {
clickBlockAppender,
getEditedPostContent,
createNewPost,
pressKeyTimes,
pressKeyWithModifier,
insertBlock,
} from '@wordpress/e2e-test-utils';
describe( 'Writing Flow', () => {
beforeEach( async () => {
await createNewPost();
} );
it( 'Should navigate inner blocks with arrow keys', async () => {
// TODO: The `waitForSelector` calls in this function should ultimately
// not be necessary for interactions, and exist as a stop-gap solution
// where rendering delays in slower CPU can cause intermittent failure.
let activeElementText;
// Add demo content
await clickBlockAppender();
await page.keyboard.type( 'First paragraph' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '/columns' );
await page.keyboard.press( 'Enter' );
await page.click( ':focus [aria-label="Two columns; equal split"]' );
await page.click( ':focus .block-editor-button-block-appender' );
await page.waitForSelector( ':focus.block-editor-inserter__search' );
await page.keyboard.type( 'Paragraph' );
await pressKeyTimes( 'Tab', 3 ); // Tab to paragraph result.
await page.keyboard.press( 'Enter' ); // Insert paragraph.
await page.keyboard.type( '1st col' ); // If this text is too long, it may wrap to a new line and cause test failure. That's why we're using "1st" instead of "First" here.
// TODO: ArrowDown should traverse into the second column. In slower
// CPUs, it can sometimes remain in the first column paragraph. This
// is a temporary solution.
await page.focus( '.wp-block[data-type="core/column"]:nth-child(2)' );
await page.click( ':focus .block-editor-button-block-appender' );
await page.waitForSelector( ':focus.block-editor-inserter__search' );
await page.keyboard.type( 'Paragraph' );
await pressKeyTimes( 'Tab', 3 ); // Tab to paragraph result.
await page.keyboard.press( 'Enter' ); // Insert paragraph.
await page.keyboard.type( '2nd col' ); // If this text is too long, it may wrap to a new line and cause test failure. That's why we're using "2nd" instead of "Second" here.
// Arrow down from last of layouts exits nested context to default
// appender of root level.
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.type( 'Second paragraph' );
// Arrow up into nested context focuses last text input
await page.keyboard.press( 'ArrowUp' );
activeElementText = await page.evaluate( () => document.activeElement.textContent );
expect( activeElementText ).toBe( '2nd col' );
// Arrow up in inner blocks should navigate through (1) column wrapper,
// (2) text fields.
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
activeElementText = await page.evaluate( () => document.activeElement.textContent );
expect( activeElementText ).toBe( '1st col' );
// Arrow up from first text field in nested context focuses column and
// columns wrappers before escaping out.
let activeElementBlockType;
await page.keyboard.press( 'ArrowUp' );
activeElementBlockType = await page.evaluate( () => (
document.activeElement.getAttribute( 'data-type' )
) );
expect( activeElementBlockType ).toBe( 'core/column' );
await page.keyboard.press( 'ArrowUp' );
activeElementBlockType = await page.evaluate( () => (
document.activeElement.getAttribute( 'data-type' )
) );
expect( activeElementBlockType ).toBe( 'core/columns' );
// Arrow up from focused (columns) block wrapper exits nested context
// to prior text input.
await page.keyboard.press( 'ArrowUp' );
activeElementText = await page.evaluate( () => document.activeElement.textContent );
expect( activeElementText ).toBe( 'First paragraph' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate around inline boundaries', async () => {
// Add demo content
await clickBlockAppender();
await page.keyboard.type( 'First' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Second' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Third' );
// Navigate to second paragraph
await pressKeyTimes( 'ArrowLeft', 6 );
// Bold second paragraph text
await page.keyboard.down( 'Shift' );
await pressKeyTimes( 'ArrowLeft', 6 );
await page.keyboard.up( 'Shift' );
await pressKeyWithModifier( 'primary', 'b' );
// Arrow left from selected bold should collapse to before the inline
// boundary. Arrow once more to traverse into first paragraph.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.type( 'After' );
// Arrow right from end of first should traverse to second, *BEFORE*
// the bolded text. Another press should move within inline boundary.
await pressKeyTimes( 'ArrowRight', 2 );
await page.keyboard.type( 'Inside' );
// Arrow left from end of beginning of inline boundary should move to
// the outside of the inline boundary.
await pressKeyTimes( 'ArrowLeft', 6 );
await page.keyboard.press( 'ArrowLeft' ); // Separate for emphasis.
await page.keyboard.type( 'Before' );
// Likewise, test at the end of the inline boundary for same effect.
await page.keyboard.press( 'ArrowRight' ); // Move inside
await pressKeyTimes( 'ArrowRight', 12 );
await page.keyboard.type( 'Inside' );
await page.keyboard.press( 'ArrowRight' );
// Edge case: Verify that workaround to test for ZWSP at beginning of
// focus node does not take effect when on the right edge of inline
// boundary (thus preventing traversing to the next block by arrow).
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'ArrowLeft' );
// Should be after the inline boundary again.
await page.keyboard.type( 'After' );
// Finally, ensure that ArrowRight from end of unbolded text moves to
// the last paragraph
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'Before' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate around nested inline boundaries', async () => {
await clickBlockAppender();
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '1 2' );
await page.keyboard.down( 'Shift' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.up( 'Shift' );
await pressKeyWithModifier( 'primary', 'i' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.down( 'Shift' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.up( 'Shift' );
await pressKeyWithModifier( 'primary', 'i' );
await page.keyboard.press( 'ArrowLeft' );
expect( await getEditedPostContent() ).toMatchSnapshot();
await page.keyboard.type( 'a' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'b' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'c' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'd' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'e' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'f' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'g' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'h' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'i' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( 'j' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should insert line break at end', async () => {
await clickBlockAppender();
await page.keyboard.type( 'a' );
await pressKeyWithModifier( 'shift', 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should insert line break at end and continue writing', async () => {
await clickBlockAppender();
await page.keyboard.type( 'a' );
await pressKeyWithModifier( 'shift', 'Enter' );
await page.keyboard.type( 'b' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should insert line break mid text', async () => {
await clickBlockAppender();
await page.keyboard.type( 'ab' );
await page.keyboard.press( 'ArrowLeft' );
await pressKeyWithModifier( 'shift', 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should insert line break at start', async () => {
await clickBlockAppender();
await page.keyboard.type( 'a' );
await page.keyboard.press( 'ArrowLeft' );
await pressKeyWithModifier( 'shift', 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should insert line break in empty container', async () => {
await clickBlockAppender();
await pressKeyWithModifier( 'shift', 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should not create extra line breaks in multiline value', async () => {
await insertBlock( 'Quote' );
await page.keyboard.type( 'a' );
await page.keyboard.press( 'Backspace' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate native inputs vertically, not horizontally', async () => {
// See: https://github.com/WordPress/gutenberg/issues/9626
// Title is within the editor's writing flow, and is a <textarea>
await page.click( '.editor-post-title' );
// Should remain in title upon ArrowRight:
await page.keyboard.press( 'ArrowRight' );
let isInTitle = await page.evaluate( () => (
!! document.activeElement.closest( '.editor-post-title' )
) );
expect( isInTitle ).toBe( true );
// Should remain in title upon modifier + ArrowDown:
await pressKeyWithModifier( 'primary', 'ArrowDown' );
isInTitle = await page.evaluate( () => (
!! document.activeElement.closest( '.editor-post-title' )
) );
expect( isInTitle ).toBe( true );
// Should navigate into blocks list upon ArrowDown:
await page.keyboard.press( 'ArrowDown' );
const isInBlock = await page.evaluate( () => (
!! document.activeElement.closest( '[data-type]' )
) );
expect( isInBlock ).toBe( true );
} );
it( 'should not delete surrounding space when deleting a word with Backspace', async () => {
await clickBlockAppender();
await page.keyboard.type( '1 2 3' );
await pressKeyTimes( 'ArrowLeft', ' 3'.length );
await page.keyboard.press( 'Backspace' );
expect( await getEditedPostContent() ).toMatchSnapshot();
await page.keyboard.type( '2' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should not delete surrounding space when deleting a word with Alt+Backspace', async () => {
await clickBlockAppender();
await page.keyboard.type( 'alpha beta gamma' );
await pressKeyTimes( 'ArrowLeft', ' gamma'.length );
if ( process.platform === 'darwin' ) {
await pressKeyWithModifier( 'alt', 'Backspace' );
} else {
await pressKeyWithModifier( 'primary', 'Backspace' );
}
expect( await getEditedPostContent() ).toMatchSnapshot();
await page.keyboard.type( 'beta' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should not delete surrounding space when deleting a selected word', async () => {
await clickBlockAppender();
await page.keyboard.type( 'alpha beta gamma' );
await pressKeyTimes( 'ArrowLeft', ' gamma'.length );
await page.keyboard.down( 'Shift' );
await pressKeyTimes( 'ArrowLeft', 'beta'.length );
await page.keyboard.up( 'Shift' );
await page.keyboard.press( 'Backspace' );
expect( await getEditedPostContent() ).toMatchSnapshot();
await page.keyboard.type( 'beta' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should create valid paragraph blocks when rapidly pressing Enter', async () => {
await clickBlockAppender();
await pressKeyTimes( 'Enter', 10 );
// Check that none of the paragraph blocks have <br> in them.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate empty paragraph', async () => {
await clickBlockAppender();
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '2' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate contenteditable with padding', async () => {
await clickBlockAppender();
await page.keyboard.press( 'Enter' );
await page.evaluate( () => {
document.activeElement.style.paddingTop = '100px';
} );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.type( '1' );
await page.evaluate( () => {
document.activeElement.style.paddingBottom = '100px';
} );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.type( '2' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should not prematurely multi-select', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '><<' );
await pressKeyWithModifier( 'shift', 'Enter' );
await page.keyboard.type( '<<<' );
await page.keyboard.down( 'Shift' );
await pressKeyTimes( 'ArrowLeft', '<<\n<<<'.length );
await page.keyboard.up( 'Shift' );
await page.keyboard.press( 'Backspace' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should merge forwards', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '3' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'Delete' );
await page.keyboard.type( '2' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should preserve horizontal position when navigating vertically between blocks', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'abc' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '23' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.type( '1' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should remember initial vertical position', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await pressKeyWithModifier( 'shift', 'Enter' );
await page.keyboard.type( '2' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.type( 'x' ); // Should be right after "1".
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate contenteditable with side padding', async () => {
await clickBlockAppender();
await page.keyboard.press( 'Enter' );
await page.evaluate( () => {
document.activeElement.style.paddingLeft = '100px';
} );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.type( '1' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should navigate empty paragraphs', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '3' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );