-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathadding-blocks.test.js
143 lines (120 loc) · 5.84 KB
/
adding-blocks.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
/**
* WordPress dependencies
*/
import {
createNewPost,
insertBlock,
getEditedPostContent,
pressKeyTimes,
switchEditorModeTo,
} from '@wordpress/e2e-test-utils';
describe( 'adding blocks', () => {
beforeEach( async () => {
await createNewPost();
} );
/**
* Given a Puppeteer ElementHandle, clicks below its bounding box.
*
* @param {Puppeteer.ElementHandle} elementHandle Element handle.
*
* @return {Promise} Promise resolving when click occurs.
*/
async function clickBelow( elementHandle ) {
const box = await elementHandle.boundingBox();
const x = box.x + ( box.width / 2 );
const y = box.y + box.height + 100;
return page.mouse.click( x, y );
}
it( 'Should insert content using the placeholder and the regular inserter', async () => {
// Click below editor to focus last field (block appender)
await clickBelow( await page.$( '.block-editor-default-block-appender' ) );
expect( await page.$( '[data-type="core/paragraph"]' ) ).not.toBeNull();
await page.keyboard.type( 'Paragraph block' );
// Using the slash command
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '/quote' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Quote block' );
// Arrow down into default appender.
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'ArrowDown' );
// Focus should be moved to block focus boundary on a block which does
// not have its own inputs (e.g. image). Proceeding to press enter will
// append the default block. Pressing backspace on the focused block
// will remove it.
await page.keyboard.type( '/image' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' );
expect( await getEditedPostContent() ).toMatchSnapshot();
// Using the regular inserter
await insertBlock( 'Preformatted' );
await page.keyboard.type( 'Pre block' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
// Verify vertical traversal at offset. This has been buggy in the past
// where verticality on a blank newline would skip into previous block.
await page.keyboard.type( 'Foo' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
await pressKeyTimes( 'Delete', 6 );
await page.keyboard.type( ' text' );
// Ensure newline preservation in shortcode block.
// See: https://github.com/WordPress/gutenberg/issues/4456
await insertBlock( 'Shortcode' );
await page.keyboard.type( '[myshortcode]With multiple' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'lines preserved[/myshortcode]' );
// Unselect blocks to avoid conflicts with the inbetween inserter
await page.click( '.editor-post-title__input' );
// Using the between inserter
const insertionPoint = await page.$( '[data-type="core/quote"] .block-editor-inserter__toggle' );
const rect = await insertionPoint.boundingBox();
await page.mouse.move( rect.x + ( rect.width / 2 ), rect.y + ( rect.height / 2 ), { steps: 10 } );
await page.waitForSelector( '[data-type="core/quote"] .block-editor-inserter__toggle' );
await page.click( '[data-type="core/quote"] .block-editor-inserter__toggle' );
// [TODO]: Search input should be focused immediately. It shouldn't be
// necessary to have `waitForFunction`.
await page.waitForFunction( () => (
document.activeElement &&
document.activeElement.classList.contains( 'block-editor-inserter__search' )
) );
await page.keyboard.type( 'para' );
await pressKeyTimes( 'Tab', 3 );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Second paragraph' );
await switchEditorModeTo( 'Code' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
// Check for regression of https://github.com/WordPress/gutenberg/issues/9583
it( 'should not allow transfer of focus outside of the block-insertion menu once open', async () => {
// Enter the default block and click the inserter toggle button to the left of it.
await page.keyboard.press( 'ArrowDown' );
await page.click( '.block-editor-block-list__empty-block-inserter .block-editor-inserter__toggle' );
// Expect the inserter search input to be the active element.
let activeElementClassList = await page.evaluate( () => document.activeElement.classList );
expect( Object.values( activeElementClassList ) ).toContain( 'block-editor-inserter__search' );
// Try using the up arrow key (vertical navigation triggers the issue described in #9583).
await page.keyboard.press( 'ArrowUp' );
// Expect the inserter search input to still be the active element.
activeElementClassList = await page.evaluate( () => document.activeElement.classList );
expect( Object.values( activeElementClassList ) ).toContain( 'block-editor-inserter__search' );
// Tab to the block search results
await page.keyboard.press( 'Tab' );
// Expect the search results to be the active element.
activeElementClassList = await page.evaluate( () => document.activeElement.classList );
expect( Object.values( activeElementClassList ) ).toContain( 'block-editor-inserter__results' );
// Try using the up arrow key
await page.keyboard.press( 'ArrowUp' );
// Expect the search results to still be the active element.
activeElementClassList = await page.evaluate( () => document.activeElement.classList );
expect( Object.values( activeElementClassList ) ).toContain( 'block-editor-inserter__results' );
// Press escape to close the block inserter.
await page.keyboard.press( 'Escape' );
// Expect focus to have transferred back to the inserter toggle button.
activeElementClassList = await page.evaluate( () => document.activeElement.classList );
expect( Object.values( activeElementClassList ) ).toContain( 'block-editor-inserter__toggle' );
} );
} );