8
0

undoengine.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'ckeditor5-core/tests/_utils/modeltesteditor';
  6. import Position from 'ckeditor5-engine/src/model/position';
  7. import UndoEngine from 'ckeditor5-undo/src/undoengine';
  8. describe( 'UndoEngine', () => {
  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 on undo revert event', () => {
  49. sinon.spy( undo._redoCommand, 'addBatch' );
  50. sinon.spy( undo._redoCommand, 'clearStack' );
  51. undo._undoCommand.fire( 'revert', null, batch );
  52. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  53. expect( undo._redoCommand.clearStack.called ).to.be.false;
  54. } );
  55. } );
  56. } );