-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.native.js
916 lines (754 loc) · 26.4 KB
/
index.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/**
* External dependencies
*/
import {
getEditorHtml,
initializeEditor,
fireEvent,
waitFor,
within,
} from 'test/helpers';
import { Clipboard, Platform } from 'react-native';
/**
* WordPress dependencies
*/
import {
getBlockTypes,
setDefaultBlockName,
unregisterBlockType,
} from '@wordpress/blocks';
import fetchRequest from '@wordpress/api-fetch';
import { store as coreStore } from '@wordpress/core-data';
import { dispatch } from '@wordpress/data';
import { requestPreview } from '@wordpress/react-native-bridge';
/**
* Internal dependencies
*/
import * as paragraph from '../../paragraph';
import * as embed from '..';
import { registerBlock } from '../..';
// Override modal mock to prevent unmounting it when is not visible.
// This is required to be able to trigger onClose and onDismiss events when
// the modal is dismissed.
jest.mock( 'react-native-modal', () => {
const mockComponent = require( 'react-native/jest/mockComponent' );
return mockComponent( 'react-native-modal' );
} );
const MODAL_DISMISS_EVENT = Platform.OS === 'ios' ? 'onDismiss' : 'onModalHide';
// oEmbed response mocks
const RICH_TEXT_EMBED_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 VIDEO_EMBED_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_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_BAD_WORDPRESS_RESPONSE = {
code: 'oembed_invalid_url',
message: 'Not Found',
data: {
status: 404,
},
html: false,
};
const EMBED_NULL_RESPONSE = null;
// Embed block HTML examples
const EMPTY_EMBED_HTML = '<!-- wp:embed /-->';
const RICH_TEXT_EMBED_HTML = `<!-- wp:embed {"url":"https://twitter.com/notnownikki","type":"rich","providerNameSlug":"twitter","responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/notnownikki
</div></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_ERROR_EMBED_HTML = `<!-- wp:embed {"url":"https://twitter.com/testing","type":"rich","providerNameSlug":"twitter","responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/testing
</div></figure>
<!-- /wp:embed -->`;
const PHOTO_EMBED_HTML = `<!-- wp:embed {"url":"https://cloudup.com/cQFlxqtY4ob","type":"photo","providerNameSlug":"cloudup","responsive":true} -->
<figure class="wp-block-embed is-type-photo is-provider-cloudup wp-block-embed-cloudup"><div class="wp-block-embed__wrapper">
https://cloudup.com/cQFlxqtY4ob
</div></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_EMBED_HTML_WITH_CAPTION = `<!-- wp:embed {"url":"https://twitter.com/notnownikki","type":"rich","providerNameSlug":"twitter","responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/notnownikki
</div><figcaption>Caption</figcaption></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_EMBED_HTML_WITH_ALLOW_RESPONSIVE_SET_TO_FALSE = `<!-- wp:embed {"url":"https://twitter.com/notnownikki","type":"rich","providerNameSlug":"twitter","allowResponsive":false,"responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/notnownikki
</div></figure>
<!-- /wp:embed -->`;
const EMPTY_RICH_TEXT_EMBED_HTML =
'<!-- wp:embed {"providerNameSlug":"twitter","responsive":true} /-->';
const EMPTY_PARAGRAPH_HTML =
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->';
const MOST_USED_PROVIDERS = embed.settings.variations.filter( ( { name } ) =>
[ 'youtube', 'twitter', 'wordpress', 'vimeo' ].includes( name )
);
// Return specified mocked responses for the oembed endpoint.
const mockEmbedResponses = ( mockedResponses ) => {
fetchRequest.mockImplementation( ( { path } ) => {
if ( path === '/wp/v2/themes?status=active' ) {
return Promise.resolve( [
{ theme_supports: { 'responsive-embeds': true } },
] );
}
const matchedEmbedResponse = mockedResponses.find(
( mockedResponse ) =>
path ===
`/oembed/1.0/proxy?url=${ encodeURIComponent(
mockedResponse.url
) }`
);
return Promise.resolve( matchedEmbedResponse || {} );
} );
};
const insertEmbedBlock = async ( blockTitle = 'Embed' ) => {
const editor = await initializeEditor( {
initialHtml: '',
} );
const { getByA11yLabel, getByText } = editor;
// Open inserter menu
fireEvent.press( await waitFor( () => getByA11yLabel( 'Add block' ) ) );
// Insert embed block
fireEvent.press( await waitFor( () => getByText( blockTitle ) ) );
// Return the embed block
const block = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);
return { ...editor, block };
};
const initializeWithEmbedBlock = async ( initialHtml, selectBlock = true ) => {
const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const editor = await initializeEditor(
{ initialHtml },
{ waitForElement }
);
const { element } = editor;
if ( selectBlock ) {
// Select block
fireEvent.press( element );
}
return { ...editor, block: element };
};
beforeAll( () => {
// Paragraph block needs to be registered because by default a paragraph
// block is added to empty posts.
registerBlock( paragraph );
registerBlock( embed );
setDefaultBlockName( paragraph.name );
} );
beforeEach( () => {
// Invalidate embed preview resolutions
dispatch( coreStore ).invalidateResolutionForStoreSelector(
'getEmbedPreview'
);
// Mock embed responses
mockEmbedResponses( [
RICH_TEXT_EMBED_SUCCESS_RESPONSE,
VIDEO_EMBED_SUCCESS_RESPONSE,
MOCK_EMBED_PHOTO_SUCCESS_RESPONSE,
] );
} );
afterAll( () => {
// Clean up registered blocks
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );
describe( 'Embed block', () => {
describe( 'insertion', () => {
it( 'inserts generic embed block', async () => {
const { block } = await insertEmbedBlock();
const blockName = within( block ).getByText( 'Embed' );
expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
MOST_USED_PROVIDERS.forEach( ( { title } ) =>
it( `inserts ${ title } embed block`, async () => {
const { block } = await insertEmbedBlock( title );
const blockName = within( block ).getByText( title );
expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} )
);
} );
describe( 'set URL upon block insertion', () => {
it( 'sets empty URL when dismissing edit URL modal', async () => {
const { getByTestId } = await insertEmbedBlock();
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'sets a valid URL when dismissing edit URL modal', async () => {
const expectedURL = 'https://twitter.com/notnownikki';
const {
getByA11yLabel,
getByPlaceholderText,
getByTestId,
} = await insertEmbedBlock();
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Set an URL
const linkTextInput = getByPlaceholderText( 'Add link' );
fireEvent( linkTextInput, 'focus' );
fireEvent.changeText( linkTextInput, expectedURL );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
// Wait for edit URL button to be present
const editURLButton = await waitFor( () =>
getByA11yLabel( 'Edit URL' )
);
expect( editURLButton ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'auto-pastes the URL from clipboard', async () => {
const clipboardURL = 'https://twitter.com/notnownikki';
// Mock clipboard
Clipboard.getString.mockResolvedValue( clipboardURL );
const {
getByA11yLabel,
getByTestId,
getByText,
} = await insertEmbedBlock();
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Get embed link
const embedLink = await waitFor( () => getByText( clipboardURL ) );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
// Wait for edit URL button to be present
const editURLButton = await waitFor( () =>
getByA11yLabel( 'Edit URL' )
);
expect( embedLink ).toBeDefined();
expect( editURLButton ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
Clipboard.getString.mockReset();
} );
} );
describe( 'set URL when empty block', () => {
it( 'sets empty URL when dismissing edit URL modal', async () => {
const { getByTestId, getByText } = await initializeWithEmbedBlock(
EMPTY_EMBED_HTML
);
// Edit URL
fireEvent.press( await waitFor( () => getByText( 'ADD LINK' ) ) );
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'sets a valid URL when dismissing edit URL modal', async () => {
const expectedURL = 'https://twitter.com/notnownikki';
const {
getByA11yLabel,
getByPlaceholderText,
getByTestId,
getByText,
} = await initializeWithEmbedBlock( EMPTY_EMBED_HTML );
// Edit URL
fireEvent.press( getByText( 'ADD LINK' ) );
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Set an URL
const linkTextInput = getByPlaceholderText( 'Add link' );
fireEvent( linkTextInput, 'focus' );
fireEvent.changeText( linkTextInput, expectedURL );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
// Wait for edit URL button to be present
const editURLButton = await waitFor( () =>
getByA11yLabel( 'Edit URL' )
);
expect( editURLButton ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'auto-pastes the URL from clipboard', async () => {
const clipboardURL = 'https://twitter.com/notnownikki';
// Mock clipboard
Clipboard.getString.mockResolvedValue( clipboardURL );
const {
getByA11yLabel,
getByTestId,
getByText,
} = await initializeWithEmbedBlock( EMPTY_EMBED_HTML );
// Edit URL
fireEvent.press( getByText( 'ADD LINK' ) );
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Get embed link
const embedLink = await waitFor( () => getByText( clipboardURL ) );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
// Wait for edit URL button to be present
const editURLButton = await waitFor( () =>
getByA11yLabel( 'Edit URL' )
);
expect( embedLink ).toBeDefined();
expect( editURLButton ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
Clipboard.getString.mockReset();
} );
} );
describe( 'edit URL', () => {
it( 'keeps the previous URL if no URL is set', async () => {
const {
getByA11yLabel,
getByTestId,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Edit URL
fireEvent.press(
await waitFor( () => getByA11yLabel( 'Edit URL' ) )
);
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'replaces URL', async () => {
const initialURL = 'https://twitter.com/notnownikki';
const expectedURL = 'https://www.youtube.com/watch?v=lXMskKTw3Bc';
const {
getByA11yLabel,
getByDisplayValue,
getByTestId,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Edit URL
fireEvent.press(
await waitFor( () => getByA11yLabel( 'Edit URL' ) )
);
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Start editing link
fireEvent.press(
getByA11yLabel( `Twitter link, ${ initialURL }` )
);
// Replace URL
const linkTextInput = getByDisplayValue( initialURL );
fireEvent( linkTextInput, 'focus' );
fireEvent.changeText( linkTextInput, expectedURL );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
// Get YouTube link field
const youtubeLinkField = await waitFor( () =>
getByA11yLabel( `YouTube link, ${ expectedURL }` )
);
expect( youtubeLinkField ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'keeps the previous URL if an invalid URL is set', async () => {
const previousURL = 'https://twitter.com/notnownikki';
const invalidURL = 'http://';
const {
getByA11yLabel,
getByDisplayValue,
getByTestId,
getByText,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Edit URL
fireEvent.press(
await waitFor( () => getByA11yLabel( 'Edit URL' ) )
);
// Wait for edit URL modal to be visible
const embedEditURLModal = getByTestId( 'embed-edit-url-modal' );
await waitFor( () => embedEditURLModal.props.isVisible );
// Start editing link
fireEvent.press(
getByA11yLabel( `Twitter link, ${ previousURL }` )
);
// Replace URL
const linkTextInput = getByDisplayValue( previousURL );
fireEvent( linkTextInput, 'focus' );
fireEvent.changeText( linkTextInput, invalidURL );
// Dismiss the edit URL modal
fireEvent( embedEditURLModal, 'backdropPress' );
fireEvent( embedEditURLModal, MODAL_DISMISS_EVENT );
const errorNotice = await waitFor( () =>
getByText( 'Invalid URL. Please enter a valid URL.' )
);
expect( errorNotice ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );
describe( 'alignment options', () => {
[
'Align left',
'Align center',
'Align right',
'Wide width',
'Full width',
].forEach( ( alignmentOption ) =>
it( `sets ${ alignmentOption } option`, async () => {
const {
getByA11yLabel,
getByText,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Open alignment options
fireEvent.press(
await waitFor( () => getByA11yLabel( 'Align' ) )
);
// Select alignment option
fireEvent.press(
await waitFor( () => getByText( alignmentOption ) )
);
expect( getEditorHtml() ).toMatchSnapshot();
} )
);
} );
describe( 'retry', () => {
it( 'retries loading the preview if initial request failed', async () => {
// Return bad response for the first request to oembed endpoint
// and success response for the rest of requests.
let isFirstEmbedRequest = true;
fetchRequest.mockImplementation( ( { path } ) => {
let response = {};
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
if ( isEmbedRequest ) {
if ( isFirstEmbedRequest ) {
isFirstEmbedRequest = false;
response = MOCK_BAD_WORDPRESS_RESPONSE;
} else {
response = RICH_TEXT_EMBED_SUCCESS_RESPONSE;
}
}
return Promise.resolve( response );
} );
const {
getByA11yLabel,
getByText,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Retry request
fireEvent.press( getByText( 'More options' ) );
fireEvent.press( getByText( 'Retry' ) );
// Wait for edit URL button to be present
const editURLButton = await waitFor( () =>
getByA11yLabel( 'Edit URL' )
);
expect( editURLButton ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'converts to link if preview request failed', async () => {
// Return bad response for requests to oembed endpoint.
fetchRequest.mockImplementation( ( { path } ) => {
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
return Promise.resolve(
isEmbedRequest ? MOCK_BAD_WORDPRESS_RESPONSE : {}
);
} );
const {
getByA11yLabel,
getByText,
} = await initializeWithEmbedBlock( RICH_TEXT_EMBED_HTML );
// Convert embed to link
fireEvent.press( getByText( 'More options' ) );
fireEvent.press( getByText( 'Convert to link' ) );
// Get paragraph block where the link is created
const paragraphBlock = await waitFor( () =>
getByA11yLabel( /Paragraph Block\. Row 1/ )
);
expect( paragraphBlock ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );
describe( 'preview coming soon', () => {
it( 'previews post for providers which embed preview is not available yet', async () => {
const { getByText, getByTestId } = await initializeWithEmbedBlock(
PHOTO_EMBED_HTML
);
// Try to preview the post
fireEvent.press( getByText( 'PREVIEW POST' ) );
// Wait for no preview modal to be visible
const noPreviewModal = getByTestId( 'embed-no-preview-modal' );
await waitFor( () => noPreviewModal.props.isVisible );
// Preview post
fireEvent.press( getByText( 'Preview post' ) );
// Dismiss the no preview modal
fireEvent( noPreviewModal, 'backdropPress' );
fireEvent( noPreviewModal, MODAL_DISMISS_EVENT );
expect( requestPreview ).toHaveBeenCalled();
} );
it( 'dismisses no preview modal', async () => {
const { getByText, getByTestId } = await initializeWithEmbedBlock(
PHOTO_EMBED_HTML
);
// Try to preview the post
fireEvent.press( getByText( 'PREVIEW POST' ) );
// Wait for no preview modal to be visible
const noPreviewModal = getByTestId( 'embed-no-preview-modal' );
await waitFor( () => noPreviewModal.props.isVisible );
// Dismiss modal
fireEvent.press( getByText( 'Dismiss' ) );
// Wait for no preview modal to be not visible
await waitFor( () => ! noPreviewModal.props.isVisible );
expect( requestPreview ).not.toHaveBeenCalled();
} );
} );
describe( 'create by pasting URL', () => {
it( 'creates embed block when pasting URL in paragraph block', async () => {
const expectedURL = 'https://www.youtube.com/watch?v=lXMskKTw3Bc';
const {
getByA11yLabel,
getByPlaceholderText,
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml: EMPTY_PARAGRAPH_HTML,
} );
// Paste URL in paragraph block
const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
fireEvent( paragraphText, 'paste', {
preventDefault: jest.fn(),
nativeEvent: {
eventCount: 1,
target: undefined,
files: [],
pastedHtml: expectedURL,
pastedText: expectedURL,
},
} );
// Wait for embed handler picker to be visible
await waitFor(
() => getByTestId( 'embed-handler-picker' ).props.isVisible
);
// Select create embed option
fireEvent.press( getByText( 'Create embed' ) );
// Get the created embed block
const embedBlock = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);
expect( embedBlock ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'creates link when pasting URL in paragraph block', async () => {
const expectedURL = 'https://www.youtube.com/watch?v=lXMskKTw3Bc';
const {
getByDisplayValue,
getByPlaceholderText,
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml: EMPTY_PARAGRAPH_HTML,
} );
// Paste URL in paragraph block
const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
fireEvent( paragraphText, 'paste', {
preventDefault: jest.fn(),
nativeEvent: {
eventCount: 1,
target: undefined,
files: [],
pastedHtml: expectedURL,
pastedText: expectedURL,
},
} );
// Wait for embed handler picker to be visible
await waitFor(
() => getByTestId( 'embed-handler-picker' ).props.isVisible
);
// Select create link option
fireEvent.press( getByText( 'Create link' ) );
// Get the link text
const linkText = await waitFor( () =>
getByDisplayValue(
`<p><a href="${ expectedURL }">${ expectedURL }</a></p>`
)
);
expect( linkText ).toBeDefined();
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );
describe( 'insert via slash inserter', () => {
it( 'insert generic embed block', async () => {
const expectedHtml = EMPTY_EMBED_HTML;
const embedBlockSlashInserter = '/Embed';
const {
getByPlaceholderText,
getByA11yLabel,
getByText,
} = await initializeEditor( { initialHtml: EMPTY_PARAGRAPH_HTML } );
const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
// Trigger onSelectionChange to update both the current text and text selection.
// This event is required by the autocompleter, as it only displays the slash inserter
// if the text selection is located at the end of the text, for this reason,
// the start and end arguments match the text length.
fireEvent(
paragraphText,
'onSelectionChange',
embedBlockSlashInserter.length,
embedBlockSlashInserter.length,
embedBlockSlashInserter,
{
nativeEvent: {
eventCount: 1,
target: undefined,
text: embedBlockSlashInserter,
},
}
);
fireEvent.press( await waitFor( () => getByText( 'Embed' ) ) );
const block = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);
const blockName = within( block ).getByText( 'Embed' );
expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'insert Twitter embed block', async () => {
const expectedHtml = EMPTY_RICH_TEXT_EMBED_HTML;
const embedBlockSlashInserter = '/Twitter';
const {
getByPlaceholderText,
getByA11yLabel,
getByText,
} = await initializeEditor( { initialHtml: EMPTY_PARAGRAPH_HTML } );
const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
// Trigger onSelectionChange to update both the current text and text selection.
// This event is required by the autocompleter, as it only displays the slash inserter
// if the text selection is located at the end of the text, for this reason,
// the start and end arguments match the text length.
fireEvent(
paragraphText,
'onSelectionChange',
embedBlockSlashInserter.length,
embedBlockSlashInserter.length,
embedBlockSlashInserter,
{
nativeEvent: {
eventCount: 1,
target: undefined,
text: embedBlockSlashInserter,
},
}
);
fireEvent.press( await waitFor( () => getByText( 'Twitter' ) ) );
const block = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);
const blockName = within( block ).getByText( 'Twitter' );
expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );
} );
it( 'sets an Embed block caption', async () => {
const initialHtml = RICH_TEXT_EMBED_HTML;
const expectedHtml = RICH_TEXT_EMBED_HTML_WITH_CAPTION;
const expectedCaption = 'Caption';
const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const {
element,
getByPlaceholderText,
getByDisplayValue,
} = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);
// Select block
fireEvent.press( element );
// Set a caption
const captionField = getByPlaceholderText( 'Add caption' );
fireEvent( captionField, 'focus' );
fireEvent( captionField, 'onChange', {
nativeEvent: {
eventCount: 1,
target: undefined,
text: expectedCaption,
},
} );
// Get current caption
const caption = await waitFor( () =>
getByDisplayValue( `<p>${ expectedCaption }</p>` )
);
expect( caption ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'Toggle resize for smaller devices media settings', async () => {
const initialHtml = RICH_TEXT_EMBED_HTML;
const expectedHtml = RICH_TEXT_EMBED_HTML_WITH_ALLOW_RESPONSIVE_SET_TO_FALSE;
const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const { element, getByA11yLabel } = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);
// Select block
fireEvent.press( element );
const settingsButton = await waitFor( () =>
getByA11yLabel( 'Open Settings' )
);
fireEvent.press( settingsButton );
const resizeToggleControl = await waitFor( () =>
getByA11yLabel( /Resize for smaller devices/ )
);
fireEvent.press( resizeToggleControl );
expect( getEditorHtml() ).toBe( expectedHtml );
} );
it( 'Cannot embed should be shown on the placeholder if EmbedPreview data is null', async () => {
// Return null response for requests to oembed endpoint.
fetchRequest.mockImplementation( ( { path } ) => {
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
return Promise.resolve( isEmbedRequest ? EMBED_NULL_RESPONSE : {} );
} );
const initialHtml = RICH_TEXT_ERROR_EMBED_HTML;
const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const { element, getByText } = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);
// Select block
fireEvent.press( element );
const cannotEmbedText = getByText( 'Unable to embed media' );
expect( cannotEmbedText ).toBeDefined();
} );
} );