undoengine.js 2.3 KB

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