Debian Codegraph @

extensions/Cite/tests/qunit/ve-cite/ve.dm.Transaction.test.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor DataModel Cite-specific Transaction tests.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 QUnit.module( 've.dm.Transaction (Cite)', ve.test.utils.newMwEnvironment() );
11
12 // FIXME: Duplicates test runner; should be using a data provider
13 QUnit.test( 'newFromDocumentInsertion with references', ( assert ) => {
14 const complexDoc = ve.dm.citeExample.createExampleDocument( 'complexInternalData' ),
15 withReference = [
16 { type: 'paragraph' },
17 'B', 'a', 'r',
18 { type: 'mwReference', attributes: {
19 mw: {},
20 about: '#mwt4',
21 listIndex: 0,
22 listGroup: 'mwReference/',
23 listKey: 'auto/0',
24 refGroup: '',
25 contentsUsed: true
26 } },
27 { type: '/mwReference' },
28 { type: '/paragraph' },
29 { type: 'internalList' },
30 { type: 'internalItem' },
31 { type: 'paragraph', internal: { generated: 'wrapper' } },
32 'B',
33 'a',
34 'z',
35 { type: '/paragraph' },
36 { type: '/internalItem' },
37 { type: '/internalList' }
38 ],
39 cases = [
40 {
41 msg: 'inserting a brand new document; internal lists are merged and items renumbered',
42 doc: 'complexInternalData',
43 offset: 7,
44 newDocData: withReference,
45 removalOps: [],
46 expectedOps: [
47 { type: 'retain', length: 7 },
48 {
49 type: 'replace',
50 remove: [],
51 insert: withReference.slice( 0, 4 )
52 // Renumber listIndex from 0 to 2
53 // Renumber listKey from auto/0 to auto/1
54 .concat( [
55 ve.extendObject( true, {}, withReference[ 4 ],
56 { attributes: { listIndex: 2, listKey: 'auto/1' } }
57 )
58 ] )
59 .concat( withReference.slice( 5, 7 ) )
60 },
61 { type: 'retain', length: 1 },
62 {
63 type: 'replace',
64 remove: complexDoc.getData( new ve.Range( 8, 32 ) ),
65 insert: complexDoc.getData( new ve.Range( 8, 32 ) )
66 .concat( withReference.slice( 8, 15 ) )
67 },
68 { type: 'retain', length: 1 }
69 ]
70 }
71 ];
72
73 cases.forEach( ( caseItem ) => {
74 const doc = ve.dm.citeExample.createExampleDocument( caseItem.doc );
75 let doc2, removalOps;
76 if ( caseItem.newDocData ) {
77 doc2 = new ve.dm.Document( caseItem.newDocData );
78 removalOps = [];
79 } else if ( caseItem.range ) {
80 doc2 = doc.cloneFromRange( caseItem.range );
81 caseItem.modify( doc2 );
82 const removalTx = ve.dm.TransactionBuilder.static
83 .newFromRemoval( doc, caseItem.range, true );
84 doc.commit( removalTx );
85 removalOps = removalTx.getOperations();
86 }
87
88 assert.deepEqualWithDomElements(
89 removalOps, caseItem.removalOps, caseItem.msg + ': removal'
90 );
91
92 const tx = ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
93 doc, caseItem.offset, doc2
94 );
95 assert.deepEqualWithDomElements(
96 tx.getOperations(), caseItem.expectedOps, caseItem.msg + ': transaction'
97 );
98
99 const expectedStoreItems = caseItem.expectedStoreItems || [];
100 const actualStoreItems = expectedStoreItems.map(
101 ( item ) => doc.store.value( OO.getHash( item ) )
102 );
103 assert.deepEqual( actualStoreItems, expectedStoreItems, caseItem.msg + ': store items' );
104 } );
105 } );