Parcourir la source

Renamed _batchStack to more correct _items and made minor simplifications. Tests: Fixed the way how feature is initialized and made sure to use beforeEach().

Piotrek Koszuliński il y a 9 ans
Parent
commit
7d0ce4596c

+ 12 - 14
packages/ckeditor5-undo/src/undocommand.js

@@ -17,12 +17,15 @@ export default class UndoCommand extends Command {
 		super( editor );
 
 		/**
-		 * Items that are pairs of batches which are saved by the command and model selection state at the moment of saving the batch.
+		 * Items that are pairs of:
+		 *
+		 * * batches which are saved by the command and,
+		 * * model selection state at the moment of saving the batch.
 		 *
 		 * @private
-		 * @member {Array.<engine.treeModel.Batch>} undo.UndoCommand#_batchStack
+		 * @member {Array} undo.UndoCommand#_items
 		 */
-		this._batchStack = [];
+		this._items = [];
 	}
 
 	/**
@@ -36,7 +39,7 @@ export default class UndoCommand extends Command {
 			isBackward: this.editor.document.selection.isBackward
 		};
 
-		this._batchStack.push( { batch, selection } );
+		this._items.push( { batch, selection } );
 		this.refreshState();
 	}
 
@@ -44,7 +47,7 @@ export default class UndoCommand extends Command {
 	 * Removes all batches from the stack.
 	 */
 	clearStack() {
-		this._batchStack = [];
+		this._items = [];
 		this.refreshState();
 	}
 
@@ -52,7 +55,7 @@ export default class UndoCommand extends Command {
 	 * @inheritDoc
 	 */
 	_checkEnabled() {
-		return this._batchStack.length > 0;
+		return this._items.length > 0;
 	}
 
 	/**
@@ -70,17 +73,12 @@ export default class UndoCommand extends Command {
 		// If batch is not given, set `batchIndex` to the last index in command stack.
 		// If it is given, find it on the stack.
 		if ( !batch ) {
-			batchIndex = this._batchStack.length - 1;
+			batchIndex = this._items.length - 1;
 		} else {
-			for ( let i = 0; i < this._batchStack.length; i++ ) {
-				if ( this._batchStack[ i ].batch == batch ) {
-					batchIndex = i;
-					break;
-				}
-			}
+			batchIndex = this._items.findIndex( item => item.batch == batch );
 		}
 
-		const undoItem = this._batchStack.splice( batchIndex, 1 )[ 0 ];
+		const undoItem = this._items.splice( batchIndex, 1 )[ 0 ];
 
 		// Get the batch to undo.
 		const undoBatch = undoItem.batch;

+ 13 - 21
packages/ckeditor5-undo/tests/undo.js

@@ -9,30 +9,30 @@ import Editor from '/ckeditor5/editor.js';
 import ModelDocument from '/ckeditor5/engine/treemodel/document.js';
 import Range from '/ckeditor5/engine/treemodel/range.js';
 import Position from '/ckeditor5/engine/treemodel/position.js';
-import UndoFeature from '/ckeditor5/undo/undo.js';
-
-let element, editor, undo, doc, root;
+import Undo from '/ckeditor5/undo/undo.js';
+import Creator from '/ckeditor5/creator/creator.js';
 
 import { getData, setData } from '/tests/engine/_utils/model.js';
 
-//import deleteContents from '/ckeditor5/engine/treemodel/composer/deletecontents.js';
+// import deleteContents from '/ckeditor5/engine/treemodel/composer/deletecontents.js';
+
+let element, editor, doc, root;
 
-before( () => {
+beforeEach( () => {
 	element = document.createElement( 'div' );
 	document.body.appendChild( element );
 
-	editor = new Editor( element );
-
 	doc = new ModelDocument();
-	editor.document = doc;
 	root = doc.createRoot( 'root' );
 
-	undo = new UndoFeature( editor );
-	undo.init();
-} );
+	editor = new Editor( element, {
+		creator: Creator,
+		features: [ Undo ]
+	} );
 
-after( () => {
-	undo.destroy();
+	editor.document = doc;
+
+	return editor.init();
 } );
 
 function setSelection( pathA, pathB ) {
@@ -52,14 +52,6 @@ function undoDisabled() {
 }
 
 describe( 'undo integration', () => {
-	beforeEach( () => {
-		// Clearing root because `setData` has a bug.
-		root.removeChildren( 0, root.getChildCount() );
-
-		editor.commands.get( 'undo' ).clearStack();
-		editor.commands.get( 'redo' ).clearStack();
-	} );
-
 	describe( 'adding and removing content', () => {
 		it( 'add and undo', () => {
 			input( '<p>fo<selection />o</p><p>bar</p>' );

+ 4 - 3
packages/ckeditor5-undo/tests/undocommand.js

@@ -33,16 +33,17 @@ afterEach( () => {
 describe( 'UndoCommand', () => {
 	describe( 'constructor', () => {
 		it( 'should create undo command with empty batch stack', () => {
-			expect( undo._batchStack.length ).to.equal( 0 );
+			expect( undo._checkEnabled() ).to.be.false;
 		} );
 	} );
 
 	describe( 'clearStack', () => {
 		it( 'should remove all batches from the stack', () => {
 			undo.addBatch( doc.batch() );
-			undo.clearStack();
+			expect( undo._checkEnabled() ).to.be.true;
 
-			expect( undo._batchStack.length ).to.equal( 0 );
+			undo.clearStack();
+			expect( undo._checkEnabled() ).to.be.false;
 		} );
 	} );