8
0

undoengine.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.js';
  7. import Position from '/ckeditor5/engine/model/position.js';
  8. import UndoEngine from '/ckeditor5/undo/undoengine.js';
  9. let editor, undo, batch, doc, root;
  10. beforeEach( () => {
  11. editor = new ModelTestEditor();
  12. doc = editor.document;
  13. batch = doc.batch();
  14. root = doc.getRoot();
  15. undo = new UndoEngine( editor );
  16. undo.init();
  17. } );
  18. afterEach( () => {
  19. undo.destroy();
  20. } );
  21. describe( 'UndoEngine', () => {
  22. it( 'should register undo command and redo command', () => {
  23. expect( editor.commands.get( 'undo' ) ).to.equal( undo._undoCommand );
  24. expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
  25. } );
  26. it( 'should add a batch to undo command and clear redo stack, if it\'s type is different than "undo" and "redo"', () => {
  27. sinon.spy( undo._undoCommand, 'addBatch' );
  28. sinon.spy( undo._redoCommand, 'clearStack' );
  29. expect( undo._undoCommand.addBatch.called ).to.be.false;
  30. expect( undo._redoCommand.clearStack.called ).to.be.false;
  31. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  32. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  33. expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
  34. } );
  35. it( 'should add a batch to undo command, if it\'s type is redo and not clear redo stack', () => {
  36. sinon.spy( undo._undoCommand, 'addBatch' );
  37. sinon.spy( undo._redoCommand, 'clearStack' );
  38. batch.type = 'redo';
  39. expect( undo._undoCommand.addBatch.called ).to.be.false;
  40. expect( undo._redoCommand.clearStack.called ).to.be.false;
  41. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  42. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  43. expect( undo._redoCommand.clearStack.calledOnce ).to.be.false;
  44. } );
  45. it( 'should add a batch to redo command, if it\'s type is undo', () => {
  46. batch.type = 'undo';
  47. sinon.spy( undo._redoCommand, 'addBatch' );
  48. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  49. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  50. expect( undo._redoCommand.addBatch.calledWith( batch ) ).to.be.true;
  51. } );
  52. } );