undoengine.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  7. import UndoEngine from '../src/undoengine';
  8. describe( 'UndoEngine', () => {
  9. let editor, undo, model, root;
  10. beforeEach( () => {
  11. editor = new ModelTestEditor();
  12. model = editor.model;
  13. root = model.document.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. model.change( writer => {
  31. writer.insertText( 'foobar', root );
  32. } );
  33. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  34. expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
  35. } );
  36. it( 'should add each batch only once', () => {
  37. sinon.spy( undo._undoCommand, 'addBatch' );
  38. model.change( writer => {
  39. writer.insertText( 'foobar', root );
  40. writer.insertText( 'foobar', root );
  41. } );
  42. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  43. } );
  44. it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
  45. sinon.spy( undo._undoCommand, 'addBatch' );
  46. sinon.spy( undo._redoCommand, 'clearStack' );
  47. const batch = new Batch();
  48. undo._redoCommand._createdBatches.add( batch );
  49. model.enqueueChange( batch, writer => {
  50. writer.insertText( 'foobar', root );
  51. } );
  52. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  53. expect( undo._redoCommand.clearStack.called ).to.be.false;
  54. } );
  55. it( 'should add a batch to redo command on undo revert event', () => {
  56. sinon.spy( undo._redoCommand, 'addBatch' );
  57. sinon.spy( undo._redoCommand, 'clearStack' );
  58. undo._undoCommand.fire( 'revert', null, new Batch() );
  59. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  60. expect( undo._redoCommand.clearStack.called ).to.be.false;
  61. } );
  62. } );
  63. } );