Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Tests: Fix list tests so they logic isn't changed after incorporating…
Browse files Browse the repository at this point in the history
… selection post-fixer.
  • Loading branch information
jodator committed Jul 10, 2018
1 parent 7447b0d commit 28af705
Showing 1 changed file with 40 additions and 45 deletions.
85 changes: 40 additions & 45 deletions tests/listediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';

import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import { getData as getModelData, setData as setModelData, parse as parseModel } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getModelData, parse as parseModel, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData, parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';

import { insertElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
Expand Down Expand Up @@ -3861,32 +3861,32 @@ describe( 'ListEditing', () => {
const item = input.substring( selStart, selEnd );
const modelInput = input.substring( 0, selStart ) + input.substring( selEnd );

const actionCallback = () => {
const actionCallback = selection => {
model.change( writer => {
writer.insert( parseModel( item, model.schema ), modelDoc.selection.getFirstPosition() );
writer.insert( parseModel( item, model.schema ), selection.getFirstPosition() );
} );
};

_test( testName, modelInput, output, actionCallback, testUndo );
}

function testRemove( testName, input, output ) {
const actionCallback = () => {
const actionCallback = selection => {
model.change( writer => {
writer.remove( modelDoc.selection.getFirstRange() );
writer.remove( selection.getFirstRange() );
} );
};

_test( testName, input, output, actionCallback );
}

function testChangeType( testName, input, output ) {
const actionCallback = () => {
const element = modelDoc.selection.getFirstPosition().nodeAfter;
const actionCallback = selection => {
const element = selection.getFirstPosition().nodeAfter;
const newType = element.getAttribute( 'listType' ) == 'numbered' ? 'bulleted' : 'numbered';

model.change( writer => {
const itemsToChange = Array.from( modelDoc.selection.getSelectedBlocks() );
const itemsToChange = Array.from( selection.getSelectedBlocks() );

for ( const item of itemsToChange ) {
writer.setAttribute( 'listType', newType, item );
Expand All @@ -3898,8 +3898,7 @@ describe( 'ListEditing', () => {
}

function testRenameFromListItem( testName, input, output, testUndo = true ) {
const actionCallback = undoSelection => {
const selection = undoSelection ? undoSelection : modelDoc.selection;
const actionCallback = selection => {
const element = selection.getFirstPosition().nodeAfter;

model.change( writer => {
Expand All @@ -3913,9 +3912,7 @@ describe( 'ListEditing', () => {
}

function testRenameToListItem( testName, newIndent, input, output ) {
const actionCallback = undoSelection => {
const selection = undoSelection ? undoSelection : modelDoc.selection;

const actionCallback = selection => {
const element = selection.getFirstPosition().nodeAfter;

model.change( writer => {
Expand All @@ -3928,21 +3925,19 @@ describe( 'ListEditing', () => {
}

function testChangeIndent( testName, newIndent, input, output ) {
const actionCallback = () => {
const actionCallback = selection => {
model.change( writer => {
writer.setAttribute( 'listIndent', newIndent, modelDoc.selection.getFirstRange() );
writer.setAttribute( 'listIndent', newIndent, selection.getFirstRange() );
} );
};

_test( testName, input, output, actionCallback );
}

function testMove( testName, input, rootOffset, output, testUndo = true ) {
const actionCallback = undoSelection => {
const actionCallback = selection => {
const targetPosition = ModelPosition.createAt( modelRoot, rootOffset );

const selection = undoSelection ? undoSelection : modelDoc.selection;

model.change( writer => {
writer.move( selection.getFirstRange(), targetPosition );
} );
Expand All @@ -3953,18 +3948,36 @@ describe( 'ListEditing', () => {

function _test( testName, input, output, actionCallback ) {
it( testName, () => {
// Wrap all changes in one block to avoid post-fixing the selection
// (which may be incorrect) in the meantime.
model.change( () => {
setModelData( model, input );
const callbackSelection = prepareTest( model, input );

actionCallback();
} );
actionCallback( callbackSelection );

expect( getViewData( view, { withoutSelection: true } ) ).to.equal( output );
} );

it( testName + ' (undo integration)', () => {
const callbackSelection = prepareTest( model, input );

const modelBefore = getModelData( model );
const viewBefore = getViewData( view, { withoutSelection: true } );

actionCallback( callbackSelection );

const modelAfter = getModelData( model );
const viewAfter = getViewData( view, { withoutSelection: true } );

editor.execute( 'undo' );

expect( getModelData( model ) ).to.equal( modelBefore );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal( viewBefore );

editor.execute( 'redo' );

expect( getModelData( model ) ).to.equal( modelAfter );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal( viewAfter );
} );

function prepareTest( model, input ) {
const modelRoot = model.document.getRoot( 'main' );

// Parse data string to model.
Expand All @@ -3980,14 +3993,11 @@ describe( 'ListEditing', () => {
writer.remove( ModelRange.createIn( modelRoot ) );
writer.insert( modelDocumentFragment, modelRoot );

// // Clean up previous document selection.
// Clean up previous document selection.
writer.setSelection( null );
writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );
} );

const modelBefore = getModelData( model );
const viewBefore = getViewData( view, { withoutSelection: true } );

const ranges = [];

for ( const range of selection.getRanges() ) {
Expand All @@ -3997,22 +4007,7 @@ describe( 'ListEditing', () => {
ranges.push( new ModelRange( start, end ) );
}

const callbackSelection = new Selection( ranges );

actionCallback( callbackSelection );

const modelAfter = getModelData( model );
const viewAfter = getViewData( view, { withoutSelection: true } );

editor.execute( 'undo' );

expect( getModelData( model ) ).to.equal( modelBefore );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal( viewBefore );

editor.execute( 'redo' );

expect( getModelData( model ) ).to.equal( modelAfter );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal( viewAfter );
} );
return new Selection( ranges );
}
}
} );

0 comments on commit 28af705

Please sign in to comment.