-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathinserting-blocks.spec.js
375 lines (311 loc) · 11.2 KB
/
inserting-blocks.spec.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
/**
* External dependencies
*/
const path = require( 'path' );
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.use( {
insertingBlocksUtils: async ( { page, editor }, use ) => {
await use( new InsertingBlocksUtils( { page, editor } ) );
},
} );
test.describe( 'Inserting blocks (@firefox, @webkit)', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deleteAllPosts();
} );
test( 'inserts blocks by dragging and dropping from the global inserter', async ( {
page,
editor,
insertingBlocksUtils,
}, testInfo ) => {
testInfo.fixme(
testInfo.project.name === 'firefox',
'The clientX value is always 0 in firefox, see https://github.com/microsoft/playwright/issues/17761 for more info.'
);
// We need a dummy block in place to display the drop indicator due to a bug.
// @see https://github.com/WordPress/gutenberg/issues/44064
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'Dummy text' },
} );
const paragraphBlock = page.locator(
'[data-type="core/paragraph"] >> text=Dummy text'
);
await page.click(
'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]'
);
await page.fill(
'role=region[name="Block Library"i] >> role=searchbox[name="Search for blocks and patterns"i]',
'Heading'
);
await page.hover(
'role=listbox[name="Blocks"i] >> role=option[name="Heading"i]'
);
const paragraphBoundingBox = await paragraphBlock.boundingBox();
await expect( insertingBlocksUtils.indicator ).toBeVisible();
// Expect the indicator to be below the paragraph block.
await expect
.poll( () =>
insertingBlocksUtils.indicator
.boundingBox()
.then( ( { y } ) => y )
)
.toBeGreaterThan( paragraphBoundingBox.y );
await page.mouse.down();
// Call the move function twice to make sure the `dragOver` event is sent.
// @see https://github.com/microsoft/playwright/issues/17153
for ( let i = 0; i < 2; i += 1 ) {
await page.mouse.move(
// Hover on the right side of the block to avoid collapsing with the preview.
paragraphBoundingBox.x + paragraphBoundingBox.width - 1,
// Hover on the bottom of the paragraph block.
paragraphBoundingBox.y + paragraphBoundingBox.height - 1
);
}
// Expect the indicator to be below the paragraph block.
await expect
.poll( () =>
insertingBlocksUtils.indicator
.boundingBox()
.then( ( { y } ) => y )
)
.toBeGreaterThan( paragraphBoundingBox.y );
// Expect the draggable-chip to appear.
await expect( insertingBlocksUtils.draggableChip ).toBeVisible();
await page.mouse.up();
await expect.poll( editor.getEditedPostContent )
.toBe( `<!-- wp:paragraph -->
<p>Dummy text</p>
<!-- /wp:paragraph -->
<!-- wp:heading -->
<h2 class="wp-block-heading"></h2>
<!-- /wp:heading -->` );
} );
test( 'cancels dragging blocks from the global inserter by pressing Escape', async ( {
page,
editor,
insertingBlocksUtils,
} ) => {
// We need a dummy block in place to display the drop indicator due to a bug.
// @see https://github.com/WordPress/gutenberg/issues/44064
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'Dummy text' },
} );
const beforeContent = await editor.getEditedPostContent();
const paragraphBlock = page.locator(
'[data-type="core/paragraph"] >> text=Dummy text'
);
await page.click(
'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]'
);
await page.fill(
'role=region[name="Block Library"i] >> role=searchbox[name="Search for blocks and patterns"i]',
'Heading'
);
await page.hover(
'role=listbox[name="Blocks"i] >> role=option[name="Heading"i]'
);
const paragraphBoundingBox = await paragraphBlock.boundingBox();
await page.mouse.down();
// Call the move function twice to make sure the `dragOver` event is sent.
// @see https://github.com/microsoft/playwright/issues/17153
for ( let i = 0; i < 2; i += 1 ) {
await page.mouse.move(
// Hover on the right side of the block to avoid collapsing with the preview.
paragraphBoundingBox.x + paragraphBoundingBox.width - 1,
// Hover on the bottom of the paragraph block.
paragraphBoundingBox.y + paragraphBoundingBox.height - 1
);
}
await expect( insertingBlocksUtils.indicator ).toBeVisible();
await expect( insertingBlocksUtils.draggableChip ).toBeVisible();
await page.keyboard.press( 'Escape' );
await expect( insertingBlocksUtils.indicator ).not.toBeVisible();
await expect( insertingBlocksUtils.draggableChip ).not.toBeVisible();
await page.mouse.up();
await expect.poll( editor.getEditedPostContent ).toBe( beforeContent );
} );
test( 'inserts patterns by dragging and dropping from the global inserter', async ( {
page,
editor,
insertingBlocksUtils,
}, testInfo ) => {
testInfo.fixme(
testInfo.project.name === 'firefox',
'The clientX value is always 0 in firefox, see https://github.com/microsoft/playwright/issues/17761 for more info.'
);
// We need a dummy block in place to display the drop indicator due to a bug.
// @see https://github.com/WordPress/gutenberg/issues/44064
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'Dummy text' },
} );
const paragraphBlock = page.locator(
'[data-type="core/paragraph"] >> text=Dummy text'
);
await page.click(
'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]'
);
const PATTERN_NAME = 'Social links with a shared background color';
await page.fill(
'role=region[name="Block Library"i] >> role=searchbox[name="Search for blocks and patterns"i]',
PATTERN_NAME
);
await page.hover(
`role=listbox[name="Block Patterns"i] >> role=option[name="${ PATTERN_NAME }"i]`
);
// FIXME: I think we should show the indicator when hovering on patterns as well?
// @see https://github.com/WordPress/gutenberg/issues/45183
// await expect( insertingBlocksUtils.indicator ).toBeVisible();
const paragraphBoundingBox = await paragraphBlock.boundingBox();
await page.mouse.down();
// Call the move function twice to make sure the `dragOver` event is sent.
// @see https://github.com/microsoft/playwright/issues/17153
for ( let i = 0; i < 2; i += 1 ) {
await page.mouse.move(
// Hover on the right side of the block to avoid collapsing with the preview.
paragraphBoundingBox.x + paragraphBoundingBox.width - 1,
// Hover on the bottom of the paragraph block.
paragraphBoundingBox.y + paragraphBoundingBox.height - 1
);
}
await expect( insertingBlocksUtils.indicator ).toBeVisible();
// Expect the indicator to be below the paragraph block.
await expect
.poll( () =>
insertingBlocksUtils.indicator
.boundingBox()
.then( ( { y } ) => y )
)
.toBeGreaterThan( paragraphBoundingBox.y );
await expect( insertingBlocksUtils.draggableChip ).toBeVisible();
await page.mouse.up();
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );
test( 'cancels dragging patterns from the global inserter by pressing Escape', async ( {
page,
editor,
insertingBlocksUtils,
} ) => {
// We need a dummy block in place to display the drop indicator due to a bug.
// @see https://github.com/WordPress/gutenberg/issues/44064
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'Dummy text' },
} );
const beforeContent = await editor.getEditedPostContent();
const paragraphBlock = page.locator(
'[data-type="core/paragraph"] >> text=Dummy text'
);
await page.click(
'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]'
);
const PATTERN_NAME = 'Social links with a shared background color';
await page.fill(
'role=region[name="Block Library"i] >> role=searchbox[name="Search for blocks and patterns"i]',
PATTERN_NAME
);
await page.hover(
`role=listbox[name="Block Patterns"i] >> role=option[name="${ PATTERN_NAME }"i]`
);
const paragraphBoundingBox = await paragraphBlock.boundingBox();
await page.mouse.down();
// Call the move function twice to make sure the `dragOver` event is sent.
// @see https://github.com/microsoft/playwright/issues/17153
for ( let i = 0; i < 2; i += 1 ) {
await page.mouse.move(
// Hover on the right side of the block to avoid collapsing with the preview.
paragraphBoundingBox.x + paragraphBoundingBox.width - 1,
// Hover on the bottom of the paragraph block.
paragraphBoundingBox.y + paragraphBoundingBox.height - 1
);
}
await expect( insertingBlocksUtils.indicator ).toBeVisible();
await expect( insertingBlocksUtils.draggableChip ).toBeVisible();
await page.keyboard.press( 'Escape' );
await expect( insertingBlocksUtils.indicator ).not.toBeVisible();
await expect( insertingBlocksUtils.draggableChip ).not.toBeVisible();
await page.mouse.up();
await expect.poll( editor.getEditedPostContent ).toBe( beforeContent );
} );
// A test for https://github.com/WordPress/gutenberg/issues/43090.
test( 'should close the inserter when clicking on the toggle button', async ( {
page,
editor,
} ) => {
const inserterButton = page.getByRole( 'button', {
name: 'Toggle block inserter',
} );
const blockLibrary = page.getByRole( 'region', {
name: 'Block Library',
} );
await inserterButton.click();
await blockLibrary.getByRole( 'option', { name: 'Buttons' } ).click();
await expect
.poll( editor.getBlocks )
.toMatchObject( [ { name: 'core/buttons' } ] );
await inserterButton.click();
await expect( blockLibrary ).toBeHidden();
} );
} );
test.describe( 'insert media from inserter', () => {
let uploadedMedia;
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.deleteAllMedia();
uploadedMedia = await requestUtils.uploadMedia(
path.resolve(
process.cwd(),
'test/e2e/assets/10x10_e2e_test_image_z9T8jK.png'
)
);
} );
test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.deleteAllMedia(),
requestUtils.deleteAllPosts(),
] );
} );
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test( 'insert media from the global inserter', async ( {
page,
editor,
} ) => {
await page.click(
'role=region[name="Editor top bar"i] >> role=button[name="Toggle block inserter"i]'
);
await page.click(
'role=region[name="Block Library"i] >> role=tab[name="Media"i]'
);
await page.click(
'[aria-label="Media categories"i] >> role=button[name="Images"i]'
);
await page.click(
`role=listbox[name="Media List"i] >> role=option[name="${ uploadedMedia.title.raw }"]`
);
await expect.poll( editor.getEditedPostContent ).toBe(
`<!-- wp:image {"id":${ uploadedMedia.id }} -->
<figure class="wp-block-image"><img src="${ uploadedMedia.source_url }" alt="${ uploadedMedia.alt_text }" class="wp-image-${ uploadedMedia.id }"/></figure>
<!-- /wp:image -->`
);
} );
} );
class InsertingBlocksUtils {
constructor( { page, editor } ) {
this.page = page;
this.editor = editor;
this.indicator = this.page.locator(
'data-testid=block-list-insertion-point-indicator'
);
this.draggableChip = this.page.locator(
'data-testid=block-draggable-chip >> visible=true'
);
}
}