-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit.native.js
453 lines (397 loc) · 16.9 KB
/
edit.native.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* External dependencies
*/
import {
act,
fireEvent,
initializeEditor,
getEditorHtml,
render,
setupApiFetch,
} from 'test/helpers';
import { Image } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
/**
* WordPress dependencies
*/
import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks';
import {
requestMediaPicker,
setFeaturedImage,
sendMediaUpload,
subscribeMediaUpload,
} from '@wordpress/react-native-bridge';
import { select, dispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { registerCoreBlocks } from '../..';
import ImageEdit from '../edit';
let uploadCallBack;
subscribeMediaUpload.mockImplementation( ( callback ) => {
uploadCallBack = callback;
} );
sendMediaUpload.mockImplementation( ( payload ) => {
uploadCallBack( payload );
} );
function mockGetMedia( media ) {
jest.spyOn( select( coreStore ), 'getMedia' ).mockReturnValue( media );
}
const FETCH_MEDIA = {
request: {
path: `/wp/v2/media/1?context=edit`,
parse: false,
},
response: {
source_url: 'https://cldup.com/cXyG__fTLN.jpg',
id: 1,
},
};
const clipboardPromise = Promise.resolve( '' );
Clipboard.getString.mockImplementation( () => clipboardPromise );
beforeAll( () => {
registerCoreBlocks();
// Mock Image.getSize to avoid failed attempt to size non-existant image
const getSizeSpy = jest.spyOn( Image, 'getSize' );
getSizeSpy.mockImplementation( ( _url, callback ) => callback( 300, 200 ) );
} );
beforeEach( () => {
// Mock media fetch requests
setupApiFetch( [ FETCH_MEDIA ] );
// Invalidate `getMedia` resolutions to allow requesting to the API the same media id
dispatch( coreStore ).invalidateResolutionForStoreSelector( 'getMedia' );
} );
afterEach( () => {
apiFetch.mockReset();
} );
afterAll( () => {
getBlockTypes().forEach( ( { name } ) => {
unregisterBlockType( name );
} );
// Restore mocks.
Image.getSize.mockRestore();
} );
describe( 'Image Block', () => {
it( 'sets link to None', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"media","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<a href="https://cldup.com/cXyG__fTLN.jpg">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
</a>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () =>
fireEvent.press( screen.getByLabelText( 'Open Settings' ) )
);
fireEvent.press( screen.getByText( 'Media File' ) );
fireEvent.press( screen.getByText( 'None' ) );
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'sets link to Media File', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () =>
fireEvent.press( screen.getByLabelText( 'Open Settings' ) )
);
fireEvent.press( screen.getByText( 'None' ) );
fireEvent.press( screen.getByText( 'Media File' ) );
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"media","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="https://cldup.com/cXyG__fTLN.jpg"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'sets link to Custom URL', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () =>
fireEvent.press( screen.getByLabelText( 'Open Settings' ) )
);
fireEvent.press( screen.getByText( 'None' ) );
fireEvent.press( screen.getByText( 'Custom URL' ) );
// Await asynchronous fetch of clipboard
await act( () => clipboardPromise );
fireEvent.changeText(
screen.getByPlaceholderText( 'Search or type URL' ),
'wordpress.org'
);
fireEvent.press( screen.getByLabelText( 'Apply' ) );
await act(
() => new Promise( ( resolve ) => setTimeout( resolve, 100 ) )
);
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="http://wordpress.org"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'swaps the link between destinations', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
fireEvent.press( screen.getByLabelText( 'Open Settings' ) );
fireEvent.press( screen.getByText( 'None' ) );
fireEvent.press( screen.getByText( 'Custom URL' ) );
fireEvent.changeText(
screen.getByPlaceholderText( 'Search or type URL' ),
'wordpress.org'
);
fireEvent.press( screen.getByLabelText( 'Apply' ) );
fireEvent.press( await screen.findByText( 'Custom URL' ) );
fireEvent.press( screen.getByText( 'Media File' ) );
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"media","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="https://cldup.com/cXyG__fTLN.jpg"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'does not display the Link To URL within the Custom URL input when set to Media File and query parameters are present', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"media","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<a href="https://cldup.com/cXyG__fTLN.jpg">
<img src="https://cldup.com/cXyG__fTLN.jpg?w=683" alt="" class="wp-image-1"/>
</a>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is not fetched via `getMedia` due to the presence of query parameters in the URL.
expect( apiFetch ).not.toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () =>
fireEvent.press( screen.getByLabelText( 'Open Settings' ) )
);
fireEvent.press( screen.getByText( 'Media File' ) );
expect( screen.queryByLabelText( /https:\/\/cldup\.com/ ) ).toBeNull();
} );
it( 'sets link target', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<a href="https://wordpress.org">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
</a>
<figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
const settingsButton = screen.getByLabelText( 'Open Settings' );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () => fireEvent.press( settingsButton ) );
const linkTargetButton = screen.getByText( 'Open in new tab' );
fireEvent.press( linkTargetButton );
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="https://wordpress.org" target="_blank" rel="noreferrer noopener"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'unset link target', async () => {
const initialHtml = `
<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<a href="https://wordpress.org" target="_blank" rel="noreferrer noopener">
<img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/>
</a>
<figcaption class="wp-element-caption">Mountain</figcaption>
</figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
// Check that image is fetched via `getMedia`
expect( apiFetch ).toHaveBeenCalledWith( FETCH_MEDIA.request );
const [ imageBlock ] = screen.getAllByLabelText( /Image Block/ );
fireEvent.press( imageBlock );
const settingsButton = screen.getByLabelText( 'Open Settings' );
// Awaiting navigation event seemingly required due to React Navigation bug
// https://github.com/react-navigation/react-navigation/issues/9701
await act( () => fireEvent.press( settingsButton ) );
const linkTargetButton = screen.getByText( 'Open in new tab' );
fireEvent.press( linkTargetButton );
const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="https://wordpress.org"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
describe( "when replacing media for an image set as the post's featured image", () => {
function mockFeaturedMedia( featuredImageId ) {
jest.spyOn(
select( editorStore ),
'getEditedPostAttribute'
).mockImplementation( ( attributeName ) =>
attributeName === 'featured_media' ? featuredImageId : undefined
);
}
it( 'does not prompt to replace featured image during a new image upload', () => {
// Arrange
const INITIAL_IMAGE = { id: 1, url: 'mock-url-1' };
const NEW_IMAGE_PENDING = { id: 2, url: 'mock-url-2' };
mockFeaturedMedia( INITIAL_IMAGE.id );
const screen = render( <ImageEdit attributes={ INITIAL_IMAGE } /> );
// Act
screen.update( <ImageEdit attributes={ NEW_IMAGE_PENDING } /> );
const MEDIA_UPLOAD_STATE_UPLOADING = 1;
sendMediaUpload( {
state: MEDIA_UPLOAD_STATE_UPLOADING,
mediaId: NEW_IMAGE_PENDING.id,
progress: 0.1,
} );
// Assert
expect( setFeaturedImage ).not.toHaveBeenCalled();
} );
it( 'does not prompt to replace featured image after a new image upload fails', () => {
// Arrange
const INITIAL_IMAGE = { id: 1, url: 'mock-url-1' };
const NEW_IMAGE_PENDING = { id: 2, url: 'mock-url-2' };
mockFeaturedMedia( INITIAL_IMAGE.id );
const screen = render(
<ImageEdit
attributes={ INITIAL_IMAGE }
setAttributes={ () => {} }
/>
);
// Act
screen.update(
<ImageEdit
attributes={ NEW_IMAGE_PENDING }
setAttributes={ () => {} }
/>
);
const MEDIA_UPLOAD_STATE_UPLOADING = 1;
sendMediaUpload( {
state: MEDIA_UPLOAD_STATE_UPLOADING,
mediaId: NEW_IMAGE_PENDING.id,
progress: 0.1,
} );
const MEDIA_UPLOAD_STATE_FAILED = 3;
sendMediaUpload( {
state: MEDIA_UPLOAD_STATE_FAILED,
mediaId: NEW_IMAGE_PENDING.id,
} );
// Assert
expect( setFeaturedImage ).not.toHaveBeenCalled();
} );
it( 'prompts to replace featured image after a new image upload succeeds', () => {
// Arrange
const INITIAL_IMAGE = { id: 1, url: 'mock-url-1' };
const NEW_IMAGE_PENDING = { id: 2, url: 'mock-url-2' };
const NEW_IMAGE_RESOLVED = { id: 3, url: 'mock-url-2' };
mockFeaturedMedia( INITIAL_IMAGE.id );
const screen = render( <ImageEdit attributes={ INITIAL_IMAGE } /> );
// Act
screen.update( <ImageEdit attributes={ NEW_IMAGE_PENDING } /> );
mockGetMedia( { id: NEW_IMAGE_RESOLVED.id } );
screen.update(
<ImageEdit
attributes={ NEW_IMAGE_RESOLVED }
setAttributes={ () => {} }
/>
);
// Assert
expect( setFeaturedImage ).toHaveBeenCalledTimes( 1 );
expect( setFeaturedImage ).toHaveBeenCalledWith(
NEW_IMAGE_RESOLVED.id
);
} );
it( 'prompts to replace featured image for a cached image', () => {
// Arrange
const INITIAL_IMAGE = { id: 1, url: 'mock-url-1' };
const NEW_IMAGE_RESOLVED = { id: 3, url: 'mock-url-2' };
mockFeaturedMedia( INITIAL_IMAGE.id );
const screen = render(
<ImageEdit
attributes={ INITIAL_IMAGE }
setAttributes={ () => {} }
/>
);
// Act
mockGetMedia( { id: NEW_IMAGE_RESOLVED.id } );
screen.update(
<ImageEdit
attributes={ NEW_IMAGE_RESOLVED }
setAttributes={ () => {} }
/>
);
// Assert
expect( setFeaturedImage ).toHaveBeenCalledTimes( 1 );
expect( setFeaturedImage ).toHaveBeenCalledWith(
NEW_IMAGE_RESOLVED.id
);
} );
} );
it( 'sets src and alt attributes when selecting media', async () => {
const IMAGE = { id: 1, url: 'mock-image', alt: 'A beautiful mountain' };
requestMediaPicker.mockImplementationOnce(
( source, filter, multiple, callback ) => {
callback( {
id: IMAGE.id,
url: IMAGE.url,
alt: IMAGE.alt,
} );
}
);
mockGetMedia( {
id: IMAGE.id,
source_url: IMAGE.url,
} );
const initialHtml = `
<!-- wp:image -->
<figure class="wp-block-image">
<img alt="" />
</figure>
<!-- /wp:image -->`;
const screen = await initializeEditor( { initialHtml } );
fireEvent.press( screen.getByText( 'Add image' ) );
fireEvent.press( screen.getByText( 'WordPress Media Library' ) );
const expectedHtml = `<!-- wp:image {"id":${ IMAGE.id },"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="${ IMAGE.url }" alt="${ IMAGE.alt }" class="wp-image-${ IMAGE.id }"/></figure>
<!-- /wp:image -->`;
expect( getEditorHtml() ).toBe( expectedHtml );
} );
} );