/
embedding.test.js
286 lines (257 loc) · 8.99 KB
/
embedding.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
/**
* WordPress dependencies
*/
import {
clickBlockAppender,
clickButton,
createEmbeddingMatcher,
createJSONResponse,
createNewPost,
createURLMatcher,
getEditedPostContent,
insertBlock,
publishPost,
setUpResponseMocking,
} from '@wordpress/e2e-test-utils';
const MOCK_EMBED_WORDPRESS_SUCCESS_RESPONSE = {
url: 'https://wordpress.org/gutenberg/handbook/block-api/attributes/',
html:
'<div class="wp-embedded-content" data-secret="shhhh it is a secret">WordPress embed</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_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_EMBED_AUDIO_SUCCESS_RESPONSE = {
url: 'https://soundcloud.com/a-boogie-wit-da-hoodie/swervin',
html: '<iframe width="16" height="9"></iframe>',
type: 'audio',
provider_name: 'SoundCloud',
provider_url: 'https://soundcloud.com',
version: '1.0',
};
const MOCK_EMBED_IMAGE_SUCCESS_RESPONSE = {
url: 'https://www.instagram.com/p/Bvl97o2AK6x/',
html: '<iframe width="16" height="9"></iframe>',
type: 'video',
provider_name: 'Instagram',
provider_url: 'https://www.instagram.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,
};
const MOCK_RESPONSES = [
{
match: createEmbeddingMatcher(
'https://wordpress.org/gutenberg/handbook'
),
onRequestMatch: createJSONResponse( MOCK_BAD_WORDPRESS_RESPONSE ),
},
{
match: createEmbeddingMatcher(
'https://wordpress.org/gutenberg/handbook/'
),
onRequestMatch: createJSONResponse( MOCK_BAD_WORDPRESS_RESPONSE ),
},
{
match: createEmbeddingMatcher(
'https://wordpress.org/gutenberg/handbook/block-api/attributes/'
),
onRequestMatch: createJSONResponse(
MOCK_EMBED_WORDPRESS_SUCCESS_RESPONSE
),
},
{
match: createEmbeddingMatcher(
'https://www.youtube.com/watch?v=lXMskKTw3Bc'
),
onRequestMatch: createJSONResponse( MOCK_EMBED_VIDEO_SUCCESS_RESPONSE ),
},
{
match: createEmbeddingMatcher(
'https://soundcloud.com/a-boogie-wit-da-hoodie/swervin'
),
onRequestMatch: createJSONResponse( MOCK_EMBED_AUDIO_SUCCESS_RESPONSE ),
},
{
match: createEmbeddingMatcher(
'https://www.instagram.com/p/Bvl97o2AK6x/'
),
onRequestMatch: createJSONResponse( MOCK_EMBED_IMAGE_SUCCESS_RESPONSE ),
},
{
match: createEmbeddingMatcher( 'https://cloudup.com/cQFlxqtY4ob' ),
onRequestMatch: createJSONResponse( MOCK_EMBED_RICH_SUCCESS_RESPONSE ),
},
{
match: createEmbeddingMatcher( 'https://twitter.com/notnownikki' ),
onRequestMatch: createJSONResponse( MOCK_EMBED_RICH_SUCCESS_RESPONSE ),
},
{
match: createEmbeddingMatcher( 'https://twitter.com/notnownikki/' ),
onRequestMatch: createJSONResponse( MOCK_CANT_EMBED_RESPONSE ),
},
{
match: createEmbeddingMatcher( 'https://twitter.com/thatbunty' ),
onRequestMatch: createJSONResponse( MOCK_BAD_EMBED_PROVIDER_RESPONSE ),
},
{
match: createEmbeddingMatcher(
'https://twitter.com/wooyaygutenberg123454312'
),
onRequestMatch: createJSONResponse( MOCK_CANT_EMBED_RESPONSE ),
},
// Respond to the instagram URL with a non-image response, doesn't matter what it is,
// just make sure the image errors.
{
match: createURLMatcher( 'https://www.instagram.com/p/Bvl97o2AK6x/' ),
onRequestMatch: createJSONResponse( MOCK_CANT_EMBED_RESPONSE ),
},
];
async function insertEmbed( URL ) {
await clickBlockAppender();
await page.keyboard.type( '/embed' );
await page.waitForXPath(
`//*[contains(@class, "components-autocomplete__result") and contains(@class, "is-selected") and contains(text(), 'Embed')]`
);
await page.keyboard.press( 'Enter' );
await page.keyboard.type( URL );
await page.keyboard.press( 'Enter' );
}
describe( 'Embedding content', () => {
beforeEach( async () => {
await setUpResponseMocking( MOCK_RESPONSES );
await createNewPost();
} );
it( 'should render embeds in the correct state', async () => {
// Valid embed. Should render valid figure element.
await insertEmbed( 'https://twitter.com/notnownikki' );
await page.waitForSelector( 'figure.wp-block-embed' );
// Valid provider; invalid content. Should render failed, edit state.
await insertEmbed( 'https://twitter.com/wooyaygutenberg123454312' );
await page.waitForSelector(
'input[value="https://twitter.com/wooyaygutenberg123454312"]'
);
// WordPress invalid content. Should render failed, edit state.
await insertEmbed( 'https://wordpress.org/gutenberg/handbook/' );
await page.waitForSelector(
'input[value="https://wordpress.org/gutenberg/handbook/"]'
);
// Provider whose oembed API has gone wrong. Should render failed, edit
// state.
await insertEmbed( 'https://twitter.com/thatbunty' );
await page.waitForSelector(
'input[value="https://twitter.com/thatbunty"]'
);
// WordPress content that can be embedded. Should render valid figure
// element.
await insertEmbed(
'https://wordpress.org/gutenberg/handbook/block-api/attributes/'
);
await page.waitForSelector( 'figure.wp-block-embed' );
// Video content. Should render valid figure element, and include the
// aspect ratio class.
await insertEmbed( 'https://www.youtube.com/watch?v=lXMskKTw3Bc' );
await page.waitForSelector(
'figure.wp-block-embed.is-type-video.wp-embed-aspect-16-9'
);
// Photo content. Should render valid figure element.
await insertEmbed( 'https://cloudup.com/cQFlxqtY4ob' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should allow the user to convert unembeddable URLs to a paragraph with a link in it', async () => {
// URL that can't be embedded.
await insertEmbed( 'https://twitter.com/wooyaygutenberg123454312' );
// Wait for the request to fail and present an error. Since placeholder
// has styles applied which depend on resize observer, wait for the
// expected size class to settle before clicking, since otherwise a race
// condition could occur on the placeholder layout vs. click intent.
await page.waitForSelector(
'.components-placeholder.is-large .components-placeholder__error'
);
await clickButton( 'Convert to link' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should retry embeds that could not be embedded with trailing slashes, without the trailing slashes', async () => {
await insertEmbed( 'https://twitter.com/notnownikki/' );
// The twitter block should appear correctly.
await page.waitForSelector( 'figure.wp-block-embed' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should allow the user to try embedding a failed URL again', async () => {
// URL that can't be embedded.
await insertEmbed( 'https://twitter.com/wooyaygutenberg123454312' );
// Wait for the request to fail and present an error. Since placeholder
// has styles applied which depend on resize observer, wait for the
// expected size class to settle before clicking, since otherwise a race
// condition could occur on the placeholder layout vs. click intent.
await page.waitForSelector(
'.components-placeholder.is-large .components-placeholder__error'
);
// Set up a different mock to make sure that try again actually does make the request again.
await setUpResponseMocking( [
{
match: createEmbeddingMatcher(
'https://twitter.com/wooyaygutenberg123454312'
),
onRequestMatch: createJSONResponse(
MOCK_EMBED_RICH_SUCCESS_RESPONSE
),
},
] );
await clickButton( 'Try again' );
await page.waitForSelector( 'figure.wp-block-embed' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'should switch to the WordPress block correctly', async () => {
// This test is to make sure that WordPress embeds are detected correctly,
// because the HTML can vary, and the block is detected by looking for
// classes in the HTML, so we need to flag up if the HTML changes.
// Publish a post to embed.
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'Hello there!' );
await publishPost();
const postUrl = await page.$eval(
'.editor-post-publish-panel [id^=inspector-text-control-]',
( el ) => el.value
);
// Start a new post, embed the previous post.
await createNewPost();
await insertEmbed( postUrl );
// Check the block has become a WordPress block.
await page.waitForSelector( 'figure.wp-block-embed' );
} );
} );