-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
embedding.spec.js
310 lines (270 loc) · 8.53 KB
/
embedding.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
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
/** @typedef {import('@playwright/test').Page} Page */
/** @typedef {import('@wordpress/e2e-test-utils-playwright').Editor} Editor */
const EMBED_URLS = [
'/oembed/1.0/proxy',
`rest_route=${ encodeURIComponent( '/oembed/1.0/proxy' ) }`,
];
const MOCK_EMBED_WORDPRESS_SUCCESS_RESPONSE = {
url: 'https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/',
html: '<div class="wp-embedded-content" data-secret="shhhh it is a secret"></div>',
type: 'rich',
provider_name: 'WordPress',
provider_url: 'https://wordpress.org',
version: '1.0',
};
const MOCK_EMBED_RICH_SUCCESS_RESPONSE = {
url: 'https://twitter.com/notnownikki',
html: '<p>Mock success response.</p>',
type: 'rich',
provider_name: 'Twitter',
provider_url: 'https://twitter.com',
version: '1.0',
};
const MOCK_EMBED_PHOTO_SUCCESS_RESPONSE = {
url: 'https://cloudup.com/cQFlxqtY4ob',
html: '<p>Mock success response.</p>',
type: 'photo',
provider_name: 'Cloudup',
provider_url: 'https://cloudup.com',
version: '1.0',
};
const MOCK_EMBED_VIDEO_SUCCESS_RESPONSE = {
url: 'https://www.youtube.com/watch?v=lXMskKTw3Bc',
html: '<iframe width="16" height="9"></iframe>',
type: 'video',
provider_name: 'YouTube',
provider_url: 'https://youtube.com',
version: '1.0',
};
const MOCK_BAD_EMBED_PROVIDER_RESPONSE = {
url: 'https://twitter.com/thatbunty',
html: false,
provider_name: 'Embed Provider',
version: '1.0',
};
const MOCK_CANT_EMBED_RESPONSE = {
provider_name: 'Embed Handler',
html: '<a href="https://twitter.com/wooyaygutenberg123454312">https://twitter.com/wooyaygutenberg123454312</a>',
};
const MOCK_BAD_WORDPRESS_RESPONSE = {
code: 'oembed_invalid_url',
message: 'Not Found',
data: {
status: 404,
},
html: false,
};
test.use( {
embedUtils: async ( { page, editor }, use ) => {
await use( new EmbedUtils( { page, editor } ) );
},
} );
test.describe( 'Embedding content', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test( 'should render embeds in the correct state', async ( {
editor,
embedUtils,
} ) => {
await embedUtils.interceptRequests( {
'https://twitter.com/notnownikki': MOCK_EMBED_RICH_SUCCESS_RESPONSE,
'https://twitter.com/wooyaygutenberg123454312':
MOCK_CANT_EMBED_RESPONSE,
'https://wordpress.org/gutenberg/handbook/':
MOCK_BAD_WORDPRESS_RESPONSE,
'https://twitter.com/thatbunty': MOCK_BAD_EMBED_PROVIDER_RESPONSE,
'https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/':
MOCK_EMBED_WORDPRESS_SUCCESS_RESPONSE,
'https://www.youtube.com/watch?v=lXMskKTw3Bc':
MOCK_EMBED_VIDEO_SUCCESS_RESPONSE,
'https://cloudup.com/cQFlxqtY4ob':
MOCK_EMBED_PHOTO_SUCCESS_RESPONSE,
} );
const currenEmbedBlock = editor.canvas
.getByRole( 'document', { name: 'Block' } )
.last();
await embedUtils.insertEmbed( 'https://twitter.com/notnownikki' );
await expect(
currenEmbedBlock.locator( 'iframe' ),
'Valid embed. Should render valid element.'
).toHaveAttribute( 'title', 'Embedded content from twitter' );
await embedUtils.insertEmbed(
'https://twitter.com/wooyaygutenberg123454312'
);
await expect(
currenEmbedBlock.getByRole( 'textbox', { name: 'Embed URL' } ),
'Valid provider; invalid content. Should render failed, edit state.'
).toHaveValue( 'https://twitter.com/wooyaygutenberg123454312' );
await embedUtils.insertEmbed(
'https://wordpress.org/gutenberg/handbook/'
);
await expect(
currenEmbedBlock.getByRole( 'textbox', { name: 'Embed URL' } ),
'WordPress invalid content. Should render failed, edit state.'
).toHaveValue( 'https://wordpress.org/gutenberg/handbook/' );
await embedUtils.insertEmbed( 'https://twitter.com/thatbunty' );
await expect(
currenEmbedBlock.getByRole( 'textbox', { name: 'Embed URL' } ),
'Provider whose oembed API has gone wrong. Should render failed, edit state.'
).toHaveValue( 'https://twitter.com/thatbunty' );
await embedUtils.insertEmbed(
'https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/'
);
await expect(
currenEmbedBlock.locator( 'figure' ),
'WordPress valid content. Should render valid figure element.'
).toHaveClass( 'wp-block-embed' );
await embedUtils.insertEmbed(
'https://www.youtube.com/watch?v=lXMskKTw3Bc'
);
await expect(
currenEmbedBlock.locator( 'figure' ),
'Video content. Should render valid figure element, and include the aspect ratio class.'
).toHaveClass( /wp-embed-aspect-16-9/ );
await embedUtils.insertEmbed( 'https://cloudup.com/cQFlxqtY4ob' );
await expect(
currenEmbedBlock.locator( 'iframe' ),
'Photo content. Should render valid iframe element.'
).toHaveAttribute( 'title', 'Embedded content from cloudup' );
} );
test( 'should allow the user to convert unembeddable URLs to a paragraph with a link in it', async ( {
editor,
embedUtils,
} ) => {
await embedUtils.interceptRequests( {
'https://twitter.com/wooyaygutenberg123454312':
MOCK_CANT_EMBED_RESPONSE,
} );
await embedUtils.insertEmbed(
'https://twitter.com/wooyaygutenberg123454312'
);
const embedBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Embed',
} );
await expect( embedBlock ).toContainText(
'Sorry, this content could not be embedded.'
);
await embedBlock
.getByRole( 'button', { name: 'Convert to link' } )
.click();
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content:
'<a href="https://twitter.com/wooyaygutenberg123454312">https://twitter.com/wooyaygutenberg123454312</a>',
},
},
] );
} );
// Reason: A possible regression of https://github.com/WordPress/gutenberg/pull/14705.
test.skip( 'should retry embeds that could not be embedded with trailing slashes, without the trailing slashes', async ( {
editor,
embedUtils,
} ) => {
await embedUtils.interceptRequests( {
'https://twitter.com/notnownikki/': MOCK_CANT_EMBED_RESPONSE,
'https://twitter.com/notnownikki': MOCK_EMBED_RICH_SUCCESS_RESPONSE,
} );
await embedUtils.insertEmbed( 'https://twitter.com/notnownikki/' );
await expect(
editor.canvas.getByRole( 'document', {
name: 'Block: Twitter',
} )
).toBeVisible();
} );
test( 'should allow the user to try embedding a failed URL again', async ( {
editor,
embedUtils,
} ) => {
await embedUtils.interceptRequests( {
'https://twitter.com/wooyaygutenberg123454312':
MOCK_CANT_EMBED_RESPONSE,
} );
await embedUtils.insertEmbed(
'https://twitter.com/wooyaygutenberg123454312'
);
const embedBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Embed',
} );
await expect( embedBlock ).toContainText(
'Sorry, this content could not be embedded.'
);
await embedUtils.interceptRequests( {
'https://twitter.com/wooyaygutenberg123454312':
MOCK_EMBED_RICH_SUCCESS_RESPONSE,
} );
await embedBlock.getByRole( 'button', { name: 'Try again' } ).click();
await expect(
editor.canvas.getByRole( 'document', {
name: 'Block: Twitter',
} )
).toBeVisible();
} );
test( 'should switch to the WordPress block correctly', async ( {
editor,
embedUtils,
requestUtils,
} ) => {
const post = await requestUtils.createPost( {
title: 'Local embed test',
content: 'Hello there!',
status: 'publish',
} );
await embedUtils.insertEmbed( post.link );
await expect(
editor.canvas.getByRole( 'document', { name: 'Block: Embed' } )
).toBeVisible();
} );
} );
class EmbedUtils {
/** @type {Page} */
#page;
/** @type {Editor} */
#editor;
constructor( { page, editor } ) {
this.#page = page;
this.#editor = editor;
}
/**
* @param {URL} url
*/
isRESTRoute( url ) {
return EMBED_URLS.some( ( route ) => {
return url.href.includes( route );
} );
}
async interceptRequests( responses ) {
await this.#page.route(
( url ) => this.isRESTRoute( url ),
async ( route, request ) => {
const embedUrl = new URL( request.url() ).searchParams.get(
'url'
);
const response = responses[ embedUrl ];
if ( response ) {
await route.fulfill( {
json: response,
} );
} else {
await route.continue();
}
}
);
}
async insertEmbed( url ) {
await test.step( `Inserting embed ${ url }`, async () => {
await this.#editor.insertBlock( { name: 'core/embed' } );
await this.#editor.canvas
.getByRole( 'textbox', { name: 'Embed URL' } )
.last()
.fill( url );
await this.#page.keyboard.press( 'Enter' );
} );
}
}