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 '/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, if it\'s type is undo', () => {
  48. sinon.spy( undo._redoCommand, 'addBatch' );
  49. sinon.spy( undo._redoCommand, 'clearStack' );
  50. undo._undoCommand._createdBatches.add( batch );
  51. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  52. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  53. expect( undo._redoCommand.clearStack.called ).to.be.false;
  54. } );
  55. } );