Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/tests/ui/actions/ve.ui.MWLinkAction.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Actions MWLinkAction tests.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 QUnit.module( 've.ui.MWLinkAction' );
9
10 /* Tests */
11
12 QUnit.test( 'MW autolink', ( assert ) => {
13 const cases = [
14 {
15 msg: 'Strip trailing punctuation (but not matched parens)',
16 html: '<p><b>https://en.wikipedia.org/wiki/Red_(disambiguation) xyz</b></p>',
17 rangeOrSelection: new ve.Range( 1, 51 ),
18 method: 'autolinkUrl',
19 expectedRangeOrSelection: new ve.Range( 51 ),
20 expectedData: ( data, action ) => {
21 const a = action.getLinkAnnotation( 'https://en.wikipedia.org/wiki/Red_(disambiguation)' );
22 for ( let i = 1; i < 51; i++ ) {
23 data[ i ][ 1 ].push( a.element );
24 }
25 }
26 },
27 {
28 msg: 'Autolink valid RFC',
29 html: '<p><b>RFC 1234 xyz</b></p>',
30 rangeOrSelection: new ve.Range( 1, 9 ),
31 method: 'autolinkMagicLink',
32 expectedRangeOrSelection: new ve.Range( 3 ),
33 expectedOriginalRangeOrSelection: new ve.Range( 9 ),
34 expectedData: ( data ) => {
35 data.splice( 1, 8, {
36 type: 'link/mwMagic',
37 attributes: {
38 content: 'RFC 1234'
39 },
40 annotations: data[ 1 ][ 1 ]
41 }, {
42 type: '/link/mwMagic'
43 } );
44 },
45 undo: true
46 },
47 {
48 msg: 'Don\'t autolink invalid RFC',
49 html: '<p><b>RFC 123x xyz</b></p>',
50 rangeOrSelection: new ve.Range( 1, 9 ),
51 method: 'autolinkMagicLink',
52 expectedRangeOrSelection: new ve.Range( 1, 9 ),
53 expectedData: () => {
54 /* no change, no link */
55 }
56 },
57 {
58 msg: 'Autolink valid PMID',
59 html: '<p><b>PMID 1234 xyz</b></p>',
60 rangeOrSelection: new ve.Range( 1, 10 ),
61 method: 'autolinkMagicLink',
62 expectedRangeOrSelection: new ve.Range( 3 ),
63 expectedOriginalRangeOrSelection: new ve.Range( 10 ),
64 expectedData: ( data ) => {
65 data.splice( 1, 9, {
66 type: 'link/mwMagic',
67 attributes: {
68 content: 'PMID 1234'
69 },
70 annotations: data[ 1 ][ 1 ]
71 }, {
72 type: '/link/mwMagic'
73 } );
74 },
75 undo: true
76 },
77 {
78 msg: 'Don\'t autolink invalid PMID',
79 html: '<p><b>PMID 123x xyz</b></p>',
80 rangeOrSelection: new ve.Range( 1, 10 ),
81 method: 'autolinkMagicLink',
82 expectedRangeOrSelection: new ve.Range( 1, 10 ),
83 expectedData: () => {
84 /* no change, no link */
85 }
86 },
87 {
88 msg: 'Autolink valid ISBN',
89 html: '<p><b>ISBN 978-0596517748 xyz</b></p>',
90 rangeOrSelection: new ve.Range( 1, 20 ),
91 method: 'autolinkMagicLink',
92 expectedRangeOrSelection: new ve.Range( 3 ),
93 expectedOriginalRangeOrSelection: new ve.Range( 20 ),
94 expectedData: ( data ) => {
95 data.splice( 1, 19, {
96 type: 'link/mwMagic',
97 attributes: {
98 content: 'ISBN 978-0596517748'
99 },
100 annotations: data[ 1 ][ 1 ]
101 }, {
102 type: '/link/mwMagic'
103 } );
104 },
105 undo: true
106 },
107 {
108 msg: 'Don\'t autolink invalid ISBN',
109 html: '<p><b>ISBN 978-059651774 xyz</b></p>',
110 rangeOrSelection: new ve.Range( 1, 19 ),
111 method: 'autolinkMagicLink',
112 expectedRangeOrSelection: new ve.Range( 1, 19 ),
113 expectedData: () => {
114 /* no change, no link */
115 }
116 }
117 ];
118
119 cases.forEach( ( caseItem ) => {
120 ve.test.utils.runActionTest(
121 'link', assert, caseItem.html, false, caseItem.method, [], caseItem.rangeOrSelection, caseItem.msg,
122 {
123 expectedData: caseItem.expectedData,
124 expectedRangeOrSelection: caseItem.expectedRangeOrSelection,
125 expectedOriginalRangeOrSelection: caseItem.expectedOriginalRangeOrSelection,
126 undo: caseItem.undo
127 }
128 );
129 } );
130 } );