8
0

undoengine.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "default"', () => {
  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 each batch only once', () => {
  36. sinon.spy( undo._undoCommand, 'addBatch' );
  37. batch.insert( new Position( root, [ 0 ] ), 'foobar' ).insert( new Position( root, [ 0 ] ), 'foobar' );
  38. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  39. } );
  40. it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
  41. sinon.spy( undo._undoCommand, 'addBatch' );
  42. sinon.spy( undo._redoCommand, 'clearStack' );
  43. undo._redoCommand._createdBatches.add( batch );
  44. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  45. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  46. expect( undo._redoCommand.clearStack.called ).to.be.false;
  47. } );
  48. it( 'should add a batch to redo command, if it\'s type is undo', () => {
  49. sinon.spy( undo._redoCommand, 'addBatch' );
  50. sinon.spy( undo._redoCommand, 'clearStack' );
  51. undo._undoCommand._createdBatches.add( batch );
  52. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  53. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  54. expect( undo._redoCommand.clearStack.called ).to.be.false;
  55. } );
  56. } );