Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ui/inspectors/ve.ui.FragmentInspector.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface FragmentInspector tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ui.FragmentInspector' );
8
9 /* Tests */
10
11 ve.test.utils.runFragmentInspectorTests = function ( surface, assert, cases ) {
12 let promise = Promise.resolve();
13
14 surface.getView().showSelectionState = function () {};
15
16 cases.forEach( ( caseItem ) => {
17 promise = promise.then( () => surface.context.inspectors.getWindow( caseItem.name ).then( ( inspector ) => {
18 const surfaceModel = surface.getModel(),
19 linearData = ve.copy( surfaceModel.getDocument().getFullData() );
20
21 surfaceModel.setLinearSelection( caseItem.range );
22 const setupData = ve.extendObject( { surface: surface, fragment: surfaceModel.getFragment() }, caseItem.setupData );
23 return inspector.setup( setupData ).then( () => inspector.ready( setupData ).then( () => {
24 if ( caseItem.input ) {
25 caseItem.input.call( inspector );
26 }
27 // TODO: Skips ActionProcess
28 return inspector.teardown( caseItem.actionData || { action: 'done' } ).then( () => {
29 assert.equalRange( surfaceModel.getSelection().getRange(), caseItem.expectedRange, caseItem.msg + ': range' );
30 if ( caseItem.expectedData ) {
31 caseItem.expectedData( linearData );
32 assert.equalLinearData(
33 surfaceModel.getDocument().getFullData(),
34 linearData,
35 caseItem.msg + ': data'
36 );
37 }
38 if ( caseItem.expectedInsertionAnnotations ) {
39 assert.deepEqual(
40 surfaceModel.getInsertionAnnotations().getHashes(),
41 caseItem.expectedInsertionAnnotations,
42 caseItem.msg + ': insertion annotations'
43 );
44 }
45 while ( surfaceModel.canUndo() ) {
46 surfaceModel.undo();
47 }
48 // Insertion annotations are not cleared by undo
49 surfaceModel.setInsertionAnnotations( null );
50 } );
51 } ) );
52 } ) );
53 } );
54 return promise;
55 };
56
57 QUnit.test( 'Different selections and inputs', ( assert ) => {
58 const done = assert.async(),
59 surface = ve.test.utils.createSurfaceFromHtml( ve.dm.example.singleLine`
60 <p>Foo <a href="bar">bar</a> baz x</p>
61 <p><!-- comment --> comment</p>
62 <p>Fo<a href="bar">o bar</a></p>
63 ` ),
64 fooHash = 'hd5a13e54366d44db',
65 barHash = 'h071cb84c069d07a4',
66 quuxHash = 'hb085ebec56a162a4',
67 cases = [
68 {
69 msg: 'Collapsed selection expands to word',
70 name: 'link',
71 range: new ve.Range( 2 ),
72 expectedRange: new ve.Range( 1, 4 ),
73 expectedData: function ( data ) {
74 data.splice(
75 1, 3,
76 [ 'F', [ fooHash ] ],
77 [ 'o', [ fooHash ] ],
78 [ 'o', [ fooHash ] ]
79 );
80 }
81 },
82 {
83 msg: 'Collapsed selection in word (noExpand)',
84 name: 'link',
85 range: new ve.Range( 2 ),
86 setupData: { noExpand: true },
87 expectedRange: new ve.Range( 2 ),
88 expectedData: function () {}
89 },
90 {
91 msg: 'Cancel restores original data & selection',
92 name: 'link',
93 range: new ve.Range( 2 ),
94 expectedRange: new ve.Range( 2 ),
95 expectedData: function () {},
96 actionData: {}
97 },
98 {
99 msg: 'Collapsed selection inside existing link',
100 name: 'link',
101 range: new ve.Range( 6 ),
102 expectedRange: new ve.Range( 5, 8 ),
103 expectedData: function () {}
104 },
105 {
106 msg: 'Selection inside existing link',
107 name: 'link',
108 range: new ve.Range( 6, 7 ),
109 expectedRange: new ve.Range( 5, 8 ),
110 expectedData: function () {}
111 },
112 {
113 msg: 'Selection spanning existing link',
114 name: 'link',
115 range: new ve.Range( 6, 10 ),
116 expectedRange: new ve.Range( 5, 10 ),
117 expectedData: function ( data ) {
118 data.splice(
119 8, 2,
120 [ ' ', [ barHash ] ],
121 [ 'b', [ barHash ] ]
122 );
123 }
124 },
125 {
126 msg: 'Collapsed selection inside a word that is partially linked',
127 name: 'link',
128 range: new ve.Range( 30 ),
129 expectedRange: new ve.Range( 29, 36 ),
130 expectedData: function ( data ) {
131 data.splice(
132 29, 2,
133 [ 'F', [ barHash ] ],
134 [ 'o', [ barHash ] ]
135 );
136 }
137 },
138 {
139 msg: 'Selection with whitespace is trimmed',
140 name: 'link',
141 range: new ve.Range( 1, 5 ),
142 expectedRange: new ve.Range( 1, 4 )
143 },
144 {
145 msg: 'Link insertion',
146 name: 'link',
147 range: new ve.Range( 13 ),
148 input: function () {
149 this.annotationInput.getTextInputWidget().setValue( 'quux' );
150 },
151 expectedRange: new ve.Range( 17 ),
152 expectedData: function ( data ) {
153 data.splice(
154 13, 0,
155 [ 'q', [ quuxHash ] ],
156 [ 'u', [ quuxHash ] ],
157 [ 'u', [ quuxHash ] ],
158 [ 'x', [ quuxHash ] ]
159 );
160 }
161 },
162 {
163 msg: 'Link insertion with no input is no-op',
164 name: 'link',
165 range: new ve.Range( 13 ),
166 expectedRange: new ve.Range( 13 ),
167 expectedData: function () {}
168 },
169 {
170 msg: 'Link modified',
171 name: 'link',
172 range: new ve.Range( 5, 8 ),
173 input: function () {
174 this.annotationInput.getTextInputWidget().setValue( 'quux' );
175 },
176 expectedRange: new ve.Range( 5, 8 ),
177 expectedData: function ( data ) {
178 data.splice(
179 5, 3,
180 [ 'b', [ quuxHash ] ],
181 [ 'a', [ quuxHash ] ],
182 [ 'r', [ quuxHash ] ]
183 );
184 }
185 },
186 {
187 msg: 'Link removed (clear input)',
188 name: 'link',
189 range: new ve.Range( 5, 8 ),
190 input: function () {
191 this.annotationInput.getTextInputWidget().setValue( '' );
192 },
193 expectedRange: new ve.Range( 5, 8 ),
194 expectedData: function ( data ) {
195 data.splice(
196 5, 3,
197 ...'bar'
198 );
199 }
200 },
201 {
202 msg: 'Clear input & cancel is still a no-op',
203 name: 'link',
204 range: new ve.Range( 5, 8 ),
205 input: function () {
206 this.annotationInput.getTextInputWidget().setValue( '' );
207 },
208 expectedRange: new ve.Range( 5, 8 ),
209 expectedData: function () {},
210 actionData: {}
211 },
212 {
213 msg: 'Comment change',
214 name: 'comment',
215 range: new ve.Range( 17, 19 ),
216 input: function () {
217 this.textWidget.setValue( 'new' );
218 },
219 expectedRange: new ve.Range( 17, 19 ),
220 expectedData: function ( data ) {
221 data.splice(
222 17, 2,
223 {
224 type: 'comment',
225 attributes: { text: ' new ' }
226 },
227 { type: '/comment' }
228 );
229 }
230 },
231 {
232 msg: 'Comment cancel',
233 name: 'comment',
234 range: new ve.Range( 17, 19 ),
235 input: function () {
236 this.textWidget.setValue( 'new' );
237 },
238 actionData: {},
239 expectedRange: new ve.Range( 17, 19 ),
240 expectedData: function () {}
241 },
242 {
243 msg: 'Comment clear (empty input)',
244 name: 'comment',
245 range: new ve.Range( 17, 19 ),
246 input: function () {
247 this.textWidget.setValue( '' );
248 },
249 expectedRange: new ve.Range( 17 ),
250 expectedData: function ( data ) {
251 data.splice( 17, 2 );
252 }
253 },
254 {
255 msg: 'Comment delete (action button)',
256 name: 'comment',
257 range: new ve.Range( 17, 19 ),
258 actionData: { action: 'remove' },
259 expectedRange: new ve.Range( 17 ),
260 expectedData: function ( data ) {
261 data.splice( 17, 2 );
262 }
263 },
264 {
265 msg: 'Language annotation doesn\'t expand',
266 name: 'language',
267 range: new ve.Range( 2, 3 ),
268 expectedRange: new ve.Range( 2, 3 ),
269 expectedData: function ( data ) {
270 data.splice(
271 2, 1,
272 [ 'o', [ 'h785e2045ecc398c1' ] ]
273 );
274 }
275 },
276 {
277 msg: 'Collapsed language annotation becomes insertion annotation',
278 name: 'language',
279 range: new ve.Range( 13 ),
280 expectedRange: new ve.Range( 13 ),
281 expectedData: function () {},
282 expectedInsertionAnnotations: [ 'h785e2045ecc398c1' ]
283 }
284 ];
285
286 ve.test.utils.runFragmentInspectorTests( surface, assert, cases ).finally( () => done() );
287 } );