undoengine.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 not add a batch that has only non-document operations', () => {
  45. sinon.spy( undo._undoCommand, 'addBatch' );
  46. model.change( writer => {
  47. const docFrag = writer.createDocumentFragment();
  48. const element = writer.createElement( 'paragraph' );
  49. writer.insert( element, docFrag, 0 );
  50. writer.insertText( 'foo', null, element, 0 );
  51. } );
  52. expect( undo._undoCommand.addBatch.called ).to.be.false;
  53. } );
  54. it( 'should add a batch that has both document and non-document operations', () => {
  55. sinon.spy( undo._undoCommand, 'addBatch' );
  56. model.change( writer => {
  57. const element = writer.createElement( 'paragraph' );
  58. writer.insertText( 'foo', null, element, 0 );
  59. writer.insert( element, root, 0 );
  60. } );
  61. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  62. } );
  63. it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
  64. sinon.spy( undo._undoCommand, 'addBatch' );
  65. sinon.spy( undo._redoCommand, 'clearStack' );
  66. const batch = new Batch();
  67. undo._redoCommand._createdBatches.add( batch );
  68. model.enqueueChange( batch, writer => {
  69. writer.insertText( 'foobar', root );
  70. } );
  71. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  72. expect( undo._redoCommand.clearStack.called ).to.be.false;
  73. } );
  74. it( 'should add a batch to redo command on undo revert event', () => {
  75. sinon.spy( undo._redoCommand, 'addBatch' );
  76. sinon.spy( undo._redoCommand, 'clearStack' );
  77. undo._undoCommand.fire( 'revert', null, new Batch() );
  78. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  79. expect( undo._redoCommand.clearStack.called ).to.be.false;
  80. } );
  81. } );
  82. } );