mediawiki-extensions-Visual.../tests/ve2/dm/ve.dm.TransactionProcessor.test.js
Catrope d9cd1392fc Add TransactionProcessor tests, and fix TP to make them pass
* Add .process() and its helpers from ve1
* Fix applyAnnotations()
** Use data[i] rather than data[j]
** Don't add empty annotation objects for no reason
** Remove empty annotation objects
*** I thought this wasn't needed, but it is needed for clean rollbacks
* Remove unused second parameter in applyAnnotations() call

Change-Id: Ia338f62d2eaf2a76f8ef653eead05bc44757a122
2012-04-30 19:06:20 -07:00

54 lines
2 KiB
JavaScript

module( 've.dm.TransactionProcessor' );
/* Tests */
test( 'retain', 2, function() {
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
var tx = new ve.dm.Transaction();
tx.pushRetain( 38 );
ve.dm.TransactionProcessor.commit( doc, tx );
deepEqual( doc.getData(), ve.dm.example.data,
'Transaction with just a retain operation does not change the linear model' );
ve.dm.TransactionProcessor.rollback( doc, tx );
deepEqual( doc.getData(), ve.dm.example.data,
'Rollback also does not change the linear model' );
} );
test( 'annotate', function() {
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
var tx = new ve.dm.Transaction();
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/bold' } );
tx.pushRetain( 2 );
tx.pushStartAnnotating( 'clear', { 'type': 'textStyle/italic' } );
tx.pushRetain( 2 );
tx.pushStopAnnotating( 'clear', { 'type': 'textStyle/italic' } );
tx.pushRetain( 8 );
tx.pushStopAnnotating( 'set', { 'type': 'textStyle/bold' } );
tx.pushRetain( 4 );
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/underline' } );
tx.pushRetain( 4 );
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/superscript' } );
tx.pushRetain( 10 );
var bold = { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } },
underline = { '{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' } },
underSuper = $.extend( {}, underline,
{ '{"type":"textStyle/superscript"}': { 'type': 'textStyle/superscript' } }
),
expectedData = ve.dm.example.data.slice( 0 );
expectedData[1] = [ 'a', bold ];
expectedData[2] = [ 'b', bold ];
expectedData[3] = [ 'c', bold ];
expectedData[9] = [ 'd', bold ];
expectedData[19] = [ 'f', underline ];
expectedData[28] = [ 'g', underSuper ];
ve.dm.TransactionProcessor.commit( doc, tx );
deepEqual( doc.getData(), expectedData,
'Complex annotation transaction commits correctly' );
ve.dm.TransactionProcessor.rollback( doc, tx );
deepEqual( doc.getData(), ve.dm.example.data,
'Complex annotation transaction rolls back correctly' );
} );