undoengine.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 UndoEngine from '../src/undoengine';
  7. describe( 'UndoEngine', () => {
  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.insertText( 'foobar', root );
  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.insertText( 'foobar', root );
  37. batch.insertText( 'foobar', root );
  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.insertText( 'foobar', root );
  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. } );