2016-02-05 20:54:55 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel Cite-specific Transaction tests.
|
|
|
|
*
|
2018-01-03 01:05:45 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
2017-12-29 12:12:35 +00:00
|
|
|
* @license MIT
|
2016-02-05 20:54:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QUnit.module( 've.dm.Transaction (Cite)', ve.test.utils.mwEnvironment );
|
|
|
|
|
|
|
|
// FIXME: Duplicates test runner; should be using a data provider
|
|
|
|
QUnit.test( 'newFromDocumentInsertion with references', function ( assert ) {
|
|
|
|
var i, j, doc2, tx, actualStoreItems, expectedStoreItems, removalOps, doc,
|
|
|
|
complexDoc = ve.dm.citeExample.createExampleDocument( 'complexInternalData' ),
|
|
|
|
withReference = [
|
|
|
|
{ type: 'paragraph' },
|
|
|
|
'B', 'a', 'r',
|
|
|
|
{ type: 'mwReference', attributes: {
|
|
|
|
mw: {},
|
|
|
|
about: '#mwt4',
|
|
|
|
listIndex: 0,
|
|
|
|
listGroup: 'mwReference/',
|
|
|
|
listKey: 'auto/0',
|
|
|
|
refGroup: '',
|
|
|
|
contentsUsed: true
|
|
|
|
} },
|
|
|
|
{ type: '/mwReference' },
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: 'internalItem' },
|
|
|
|
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
|
|
|
'B',
|
|
|
|
'a',
|
|
|
|
'z',
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: '/internalItem' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
],
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
msg: 'inserting a brand new document; internal lists are merged and items renumbered',
|
|
|
|
doc: 'complexInternalData',
|
|
|
|
offset: 7,
|
|
|
|
newDocData: withReference,
|
|
|
|
removalOps: [],
|
|
|
|
expectedOps: [
|
|
|
|
{ type: 'retain', length: 7 },
|
|
|
|
{
|
|
|
|
type: 'replace',
|
|
|
|
remove: [],
|
|
|
|
insert: withReference.slice( 0, 4 )
|
|
|
|
// Renumber listIndex from 0 to 2
|
|
|
|
// Renumber listKey from auto/0 to auto/1
|
|
|
|
.concat( [
|
|
|
|
ve.extendObject( true, {}, withReference[ 4 ],
|
|
|
|
{ attributes: { listIndex: 2, listKey: 'auto/1' } }
|
|
|
|
)
|
|
|
|
] )
|
|
|
|
.concat( withReference.slice( 5, 7 ) )
|
|
|
|
},
|
|
|
|
{ type: 'retain', length: 1 },
|
|
|
|
{
|
|
|
|
type: 'replace',
|
|
|
|
remove: complexDoc.getData( new ve.Range( 8, 32 ) ),
|
|
|
|
insert: complexDoc.getData( new ve.Range( 8, 32 ) )
|
2018-01-22 22:31:44 +00:00
|
|
|
.concat( withReference.slice( 8, 15 ) )
|
2016-02-05 20:54:55 +00:00
|
|
|
},
|
|
|
|
{ type: 'retain', length: 1 }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
2017-04-28 15:49:43 +00:00
|
|
|
|
2016-02-05 20:54:55 +00:00
|
|
|
for ( i = 0; i < cases.length; i++ ) {
|
|
|
|
doc = ve.dm.citeExample.createExampleDocument( cases[ i ].doc );
|
|
|
|
if ( cases[ i ].newDocData ) {
|
|
|
|
doc2 = new ve.dm.Document( cases[ i ].newDocData );
|
|
|
|
removalOps = [];
|
|
|
|
} else if ( cases[ i ].range ) {
|
|
|
|
doc2 = doc.cloneFromRange( cases[ i ].range );
|
|
|
|
cases[ i ].modify( doc2 );
|
2016-11-30 19:06:13 +00:00
|
|
|
tx = ve.dm.TransactionBuilder.static.newFromRemoval( doc, cases[ i ].range, true );
|
2016-02-05 20:54:55 +00:00
|
|
|
doc.commit( tx );
|
|
|
|
removalOps = tx.getOperations();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqualWithDomElements( removalOps, cases[ i ].removalOps, cases[ i ].msg + ': removal' );
|
|
|
|
|
2016-11-30 19:06:13 +00:00
|
|
|
tx = ve.dm.TransactionBuilder.static.newFromDocumentInsertion( doc, cases[ i ].offset, doc2 );
|
2016-02-05 20:54:55 +00:00
|
|
|
assert.deepEqualWithDomElements( tx.getOperations(), cases[ i ].expectedOps, cases[ i ].msg + ': transaction' );
|
|
|
|
|
|
|
|
actualStoreItems = [];
|
|
|
|
expectedStoreItems = cases[ i ].expectedStoreItems || [];
|
|
|
|
for ( j = 0; j < expectedStoreItems.length; j++ ) {
|
2016-08-17 19:52:28 +00:00
|
|
|
actualStoreItems[ j ] = doc.store.value( OO.getHash( expectedStoreItems[ j ] ) );
|
2016-02-05 20:54:55 +00:00
|
|
|
}
|
|
|
|
assert.deepEqual( actualStoreItems, expectedStoreItems, cases[ i ].msg + ': store items' );
|
|
|
|
}
|
|
|
|
} );
|