-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathlinks.test.js
More file actions
944 lines (744 loc) · 28.6 KB
/
links.test.js
File metadata and controls
944 lines (744 loc) · 28.6 KB
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/**
* WordPress dependencies
*/
import {
clickBlockAppender,
clickBlockToolbarButton,
getEditedPostContent,
createNewPost,
pressKeyWithModifier,
showBlockToolbar,
} from '@wordpress/e2e-test-utils';
describe( 'Links', () => {
beforeEach( async () => {
await createNewPost();
} );
const waitForURLFieldAutoFocus = async () => {
await page.waitForFunction(
() => !! document.activeElement.closest( '.block-editor-url-input' )
);
};
it( 'will use Post title as link text if link to existing post is created without any text selected', async () => {
const titleText = 'Post to create a link to';
await createPostWithTitle( titleText );
await createNewPost();
await clickBlockAppender();
// Now in a new post and try to create a link from an autocomplete suggestion using the keyboard.
await page.keyboard.type( 'Here comes a link: ' );
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Trigger the autocomplete suggestion list and select the first suggestion.
await page.keyboard.type( titleText.substr( 0, titleText.length - 2 ) );
await page.waitForSelector( '.block-editor-link-control__search-item' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.press( 'Enter' );
const actualText = await page.evaluate(
() =>
document.querySelector( '.block-editor-rich-text__editable a' )
.textContent
);
expect( actualText ).toBe( titleText );
} );
it( 'can be created by selecting text and clicking Link', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Submit the link.
await page.keyboard.press( 'Enter' );
// The link should have been inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'will not automatically create a link if selected text is not a valid HTTP based URL', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This: is not a link' );
// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
const urlInputValue = await page.evaluate(
() => document.querySelector( '[aria-label="URL"]' ).value
);
expect( urlInputValue ).toBe( '' );
} );
it( 'can be created by selecting text and using keyboard shortcuts', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Navigate to and toggle the "Open in new tab" checkbox.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );
// Toggle should still have focus and be checked.
await page.waitForSelector(
':focus:checked.components-form-toggle__input'
);
// Ensure that the contents of the post have not been changed, since at
// this point the link is still not inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
// Tab back to the Submit and apply the link.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );
// The link should have been inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'can be created without any text selected', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg: ' );
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Press Enter to apply the link.
await page.keyboard.press( 'Enter' );
// A link with the URL as its text should have been inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'can be created instantly when a URL is selected', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type(
'This is Gutenberg: https://wordpress.org/gutenberg'
);
// Select the URL.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// A link with the selected URL as its href should have been inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'is not created when we click away from the link input', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Click somewhere else - it doesn't really matter where.
await page.click( '.editor-post-title' );
} );
const createAndReselectLink = async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Click on the Submit button.
await page.keyboard.press( 'Enter' );
// Reselect the link.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
};
it( 'can be edited', async () => {
await createAndReselectLink();
// Click on the Edit button.
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Change the URL.
await page.keyboard.type( '/handbook' );
// Submit the link.
await page.keyboard.press( 'Enter' );
// The link should have been updated.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'can be removed', async () => {
await createAndReselectLink();
// Click on the Unlink button
// await page.click( 'button[aria-label="Unlink"]' );
// Unlick via shortcut
// we do this to avoid an layout edge case whereby
// the rich link preview popover will obscure the block toolbar
// under very specific circumstances and screensizes.
await pressKeyWithModifier( 'primaryShift', 'K' );
// The link should have been removed.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
const toggleFixedToolbar = async ( isFixed ) => {
await page.evaluate( ( _isFixed ) => {
const { select, dispatch } = wp.data;
const isCurrentlyFixed =
select( 'core/edit-post' ).isFeatureActive( 'fixedToolbar' );
if ( isCurrentlyFixed !== _isFixed ) {
dispatch( 'core/edit-post' ).toggleFeature( 'fixedToolbar' );
}
}, isFixed );
};
it( 'allows Left to be pressed during creation when the toolbar is fixed to top', async () => {
await toggleFixedToolbar( true );
await clickBlockAppender();
await page.keyboard.type( 'Text' );
await page.click( 'button[aria-label="Link"]' );
// Typing "left" should not close the dialog.
await page.keyboard.press( 'ArrowLeft' );
let popover = await page.$(
'.components-popover__content .block-editor-link-control'
);
expect( popover ).not.toBeNull();
// Escape should close the dialog still.
await page.keyboard.press( 'Escape' );
popover = await page.$(
'.components-popover__content .block-editor-link-control'
);
expect( popover ).toBeNull();
} );
it( 'allows Left to be pressed during creation in "Docked Toolbar" mode', async () => {
await toggleFixedToolbar( false );
await clickBlockAppender();
await page.keyboard.type( 'Text' );
await clickBlockToolbarButton( 'Link' );
// Typing "left" should not close the dialog.
await page.keyboard.press( 'ArrowLeft' );
let popover = await page.$(
'.components-popover__content .block-editor-link-control'
);
expect( popover ).not.toBeNull();
// Escape should close the dialog still.
await page.keyboard.press( 'Escape' );
popover = await page.$(
'.components-popover__content .block-editor-link-control'
);
expect( popover ).toBeNull();
} );
it( 'can be edited with collapsed selection', async () => {
await createAndReselectLink();
// Make a collapsed selection inside the link
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowRight' );
await showBlockToolbar();
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
await waitForURLFieldAutoFocus();
await page.keyboard.type( '/handbook' );
await page.keyboard.press( 'Enter' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
const createPostWithTitle = async ( titleText ) => {
await createNewPost();
await page.type( '.editor-post-title__input', titleText );
await page.click( '.editor-post-publish-panel__toggle' );
// Disable reason: Wait for the animation to complete, since otherwise the
// click attempt may occur at the wrong point.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 100 );
// Publish the post.
await page.click( '.editor-post-publish-button' );
// Return the URL of the new post.
await page.waitForSelector(
'.post-publish-panel__postpublish-post-address input'
);
return page.evaluate(
() =>
document.querySelector(
'.post-publish-panel__postpublish-post-address input'
).value
);
};
it( 'allows use of escape key to dismiss the url popover', async () => {
const titleText = 'Test post escape';
await createPostWithTitle( titleText );
await createNewPost();
await clickBlockAppender();
// Now in a new post and try to create a link from an autocomplete suggestion using the keyboard.
await page.keyboard.type( 'This is Gutenberg' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Trigger the autocomplete suggestion list and select the first suggestion.
await page.keyboard.type( titleText );
await page.waitForSelector( '.block-editor-link-control__search-item' );
await page.keyboard.press( 'ArrowDown' );
// Expect the escape key to dismiss the popover when the autocomplete suggestion list is open.
await page.keyboard.press( 'Escape' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
// Confirm that selection is returned to where it was before launching
// the link editor, with "Gutenberg" as an uncollapsed selection.
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '.' );
expect( await getEditedPostContent() ).toMatchSnapshot();
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Expect the escape key to dismiss the popover normally.
await page.keyboard.press( 'Escape' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Tab to the "Open in new tab" toggle.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
// Expect the escape key to dismiss the popover normally.
await page.keyboard.press( 'Escape' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
} );
it( 'can be modified using the keyboard once a link has been set', async () => {
const URL = 'https://wordpress.org/gutenberg';
// Create a block with some text and format it as a link.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'primary', 'K' );
await waitForURLFieldAutoFocus();
await page.keyboard.type( URL );
await page.keyboard.press( 'Enter' );
// Deselect the link text by moving the caret to the end of the line
// and the link popover should not be displayed.
await page.keyboard.press( 'End' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
// Move the caret back into the link text and the link popover
// should be displayed.
await page.keyboard.press( 'ArrowLeft' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Press Cmd+K to edit the link and the url-input should become
// focused with the value previously inserted.
await pressKeyWithModifier( 'primary', 'K' );
await waitForURLFieldAutoFocus();
const isInURLInput = await page.evaluate(
() => !! document.activeElement.closest( '.block-editor-url-input' )
);
expect( isInURLInput ).toBe( true );
const activeElementValue = await page.evaluate(
() => document.activeElement.value
);
expect( activeElementValue ).toBe( URL );
// Confirm that submitting the input without any changes keeps the same
// value and moves focus back to the paragraph.
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '.' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
it( 'adds an assertive message for screenreader users when an invalid link is set', async () => {
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'primary', 'K' );
await waitForURLFieldAutoFocus();
await page.keyboard.type( 'http://#test.com' );
await page.keyboard.press( 'Enter' );
const assertiveContent = await page.evaluate(
() => document.querySelector( '#a11y-speak-assertive' ).textContent
);
expect( assertiveContent.trim() ).toBe(
'Warning: the link has been inserted but may have errors. Please test it.'
);
} );
it( 'should contain a label when it should open in a new tab', async () => {
await clickBlockAppender();
await page.keyboard.type( 'This is WordPress' );
// Select "WordPress".
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
await pressKeyWithModifier( 'primary', 'k' );
await waitForURLFieldAutoFocus();
await page.keyboard.type( 'w.org' );
// Navigate to and toggle the "Open in new tab" checkbox.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );
// Confirm that focus was not prematurely returned to the paragraph on
// a changing value of the setting.
await page.waitForSelector( ':focus.components-form-toggle__input' );
// Close dialog. Expect that "Open in new tab" would have been applied
// immediately.
await pressKeyWithModifier( 'shift', 'Tab' );
await page.keyboard.press( 'Enter' );
// Wait for Gutenberg to finish the job.
await page.waitForXPath(
'//a[contains(@href,"w.org") and @target="_blank"]'
);
expect( await getEditedPostContent() ).toMatchSnapshot();
// Regression Test: This verifies that the UI is updated according to
// the expected changed values, where previously the value could have
// fallen out of sync with how the UI is displayed (specifically for
// collapsed selections).
//
// See: https://github.com/WordPress/gutenberg/pull/15573
// Move caret back into the link.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
// Edit link.
await pressKeyWithModifier( 'primary', 'k' );
await waitForURLFieldAutoFocus();
await pressKeyWithModifier( 'primary', 'a' );
await page.keyboard.type( 'wordpress.org' );
// Update the link.
await page.keyboard.press( 'Enter' );
// Navigate back to the popover.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
// Navigate back to inputs to verify appears as changed.
await pressKeyWithModifier( 'primary', 'k' );
await waitForURLFieldAutoFocus();
// Navigate to the "Open in new tab" checkbox.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
// Uncheck the checkbox.
await page.keyboard.press( 'Space' );
// Wait for Gutenberg to finish the job.
await page.waitForXPath(
'//a[contains(@href,"wordpress.org") and not(@target)]'
);
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
describe( 'Editing link text', () => {
it( 'should not display text input when initially creating the link', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg: ' );
// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
const textInput = await page
.waitForXPath(
'//[contains(@class, "block-editor-link-control__search-input-wrapper")]//label[contains(text(), "Text")]',
{
timeout: 1000,
}
)
.catch( () => false );
expect( textInput ).toBeFalsy();
} );
it( 'should display text input when the link has a valid URL value', async () => {
await createAndReselectLink();
// Make a collapsed selection inside the link. This is used
// as a stress test to ensure we can find the link text from a
// collapsed RichTextValue that contains a link format.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowRight' );
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
await waitForURLFieldAutoFocus();
await pressKeyWithModifier( 'shift', 'Tab' );
// Tabbing back should land us in the text input.
const { isTextInput, textValue } = await page.evaluate( () => {
const el = document.activeElement;
return {
isTextInput: el.matches( 'input[type="text"]' ),
textValue: el.value,
};
} );
// Let's check we've focused a text input.
expect( isTextInput ).toBe( true );
// Link was created on text value "Gutenberg". We expect
// the text input to reflect that value.
expect( textValue ).toBe( 'Gutenberg' );
} );
it( 'should preserve trailing/leading whitespace from linked text in text input', async () => {
const textToSelect = ` spaces `;
const textWithWhitespace = `Text with leading and trailing${ textToSelect }`;
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( textWithWhitespace );
// Use arrow keys to select only the text with the leading
// and trailing whitespace.
for ( let index = 0; index < textToSelect.length; index++ ) {
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
}
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Click on the Submit button.
await page.keyboard.press( 'Enter' );
// Reselect the link.
await page.keyboard.press( 'ArrowLeft' );
await showBlockToolbar();
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
await waitForURLFieldAutoFocus();
await pressKeyWithModifier( 'shift', 'Tab' );
// Tabbing back should land us in the text input.
const textInputValue = await page.evaluate(
() => document.activeElement.value
);
expect( textInputValue ).toBe( textToSelect );
} );
it( 'should allow for modification of link text via Link UI', async () => {
const originalLinkText = 'Gutenberg';
const changedLinkText =
' link text that was modified via the Link UI to include spaces ';
await createAndReselectLink();
// Make a collapsed selection inside the link. This is used
// as a stress test to ensure we can find the link text from a
// collapsed RichTextValue that contains a link format.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowRight' );
await showBlockToolbar();
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
await waitForURLFieldAutoFocus();
await pressKeyWithModifier( 'shift', 'Tab' );
// Tabbing back should land us in the text input.
const textInputValue = await page.evaluate(
() => document.activeElement.value
);
// At this point, we still expect the text input
// to reflect the original value with no modifications.
expect( textInputValue ).toBe( originalLinkText );
// Select all the link text in the input.
await pressKeyWithModifier( 'primary', 'a' );
// Modify the link text value.
await page.keyboard.type( changedLinkText );
// Submit the change.
await page.keyboard.press( 'Enter' );
// Check the created link reflects the link text.
const actualLinkText = await page.evaluate(
() =>
document.querySelector(
'.block-editor-rich-text__editable a'
).textContent
);
expect( actualLinkText ).toBe( changedLinkText );
} );
it( 'should display (capture the) text from the currently active link even if there is a rich text selection', async () => {
const originalLinkText = 'Gutenberg';
await createAndReselectLink();
// Make a collapsed selection inside the link in order
// to activate the Link UI.
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowRight' );
const [ editButton ] = await page.$x(
'//button[contains(@aria-label, "Edit")]'
);
await editButton.click();
await waitForURLFieldAutoFocus();
// Move focus back to RichText for the underlying link.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
// Make a selection within the RichText.
await pressKeyWithModifier( 'shift', 'ArrowRight' );
await pressKeyWithModifier( 'shift', 'ArrowRight' );
await pressKeyWithModifier( 'shift', 'ArrowRight' );
// Move back to the text input.
await page.keyboard.press( 'Tab' );
// Tabbing back should land us in the text input.
const textInputValue = await page.evaluate(
() => document.activeElement.value
);
// Making a selection within the link text whilst the Link UI
// is open should not alter the value in the Link UI's text
// input. It should remain as the full text of the currently
// focused link format.
expect( textInputValue ).toBe( originalLinkText );
} );
} );
describe( 'Disabling Link UI active state', () => {
it( 'should not show the Link UI when selection extends beyond link boundary', async () => {
const linkedText = `Gutenberg`;
const textBeyondLinkedText = ` and more text.`;
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type(
`This is ${ linkedText }${ textBeyondLinkedText }`
);
// Move cursor next to end of `linkedText`
for (
let index = 0;
index < textBeyondLinkedText.length;
index++
) {
await page.keyboard.press( 'ArrowLeft' );
}
// Select the linkedText.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );
// Update the link.
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Make selection starting within the link and moving beyond boundary to the left.
for ( let index = 0; index < linkedText.length; index++ ) {
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
}
// The Link UI should have disappeared (i.e. be inactive).
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
// Cancel selection and move back within the Link.
await page.keyboard.press( 'ArrowRight' );
// We should see the Link UI displayed again.
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Make selection starting within the link and moving beyond boundary to the right.
await pressKeyWithModifier( 'shift', 'ArrowRight' );
await pressKeyWithModifier( 'shift', 'ArrowRight' );
await pressKeyWithModifier( 'shift', 'ArrowRight' );
// The Link UI should have disappeared (i.e. be inactive).
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
} );
it( 'should not show the Link UI when selection extends into another link', async () => {
const linkedTextOne = `Gutenberg`;
const linkedTextTwo = `Block Editor`;
const linkOneURL = 'https://wordpress.org';
const linkTwoURL = 'https://wordpress.org/gutenberg';
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type(
`This is the ${ linkedTextOne }${ linkedTextTwo }`
);
// Select the linkedTextTwo.
for ( let index = 0; index < linkedTextTwo.length; index++ ) {
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
}
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( linkTwoURL );
// Update the link.
await page.keyboard.press( 'Enter' );
// Move cursor next to the **end** of `linkTextOne`
for ( let index = 0; index < linkedTextTwo.length + 2; index++ ) {
await page.keyboard.press( 'ArrowLeft' );
}
// Select `linkTextOne`
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );
// Click on the Link button.
await page.click( 'button[aria-label="Link"]' );
// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();
// Type a URL.
await page.keyboard.type( linkOneURL );
// Update the link.
await page.keyboard.press( 'Enter' );
// Move cursor within `linkTextOne`
for ( let index = 0; index < 3; index++ ) {
await page.keyboard.press( 'ArrowLeft' );
}
// Link UI should activate for `linkTextOne`
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();
// Expand selection so that it overlaps with `linkTextTwo`
for ( let index = 0; index < 3; index++ ) {
await pressKeyWithModifier( 'shift', 'ArrowRight' );
}
// Link UI should be inactive.
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).toBeNull();
} );
} );
} );