undoengine.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.js';
  7. import Position from '/ckeditor5/engine/model/position.js';
  8. import UndoEngine from '/ckeditor5/undo/undoengine.js';
  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 whenever a new batch is applied to the document', () => {
  27. sinon.spy( undo._undoCommand, 'addBatch' );
  28. expect( undo._undoCommand.addBatch.called ).to.be.false;
  29. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  30. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  31. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  32. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  33. } );
  34. it( 'should add a batch to redo command whenever a batch is undone by undo command', () => {
  35. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  36. sinon.spy( undo._redoCommand, 'addBatch' );
  37. undo._undoCommand.fire( 'revert', batch );
  38. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  39. expect( undo._redoCommand.addBatch.calledWith( batch ) ).to.be.true;
  40. } );
  41. it( 'should add a batch to undo command whenever a batch is redone by redo command', () => {
  42. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  43. sinon.spy( undo._undoCommand, 'addBatch' );
  44. undo._redoCommand.fire( 'revert', batch );
  45. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  46. expect( undo._undoCommand.addBatch.calledWith( batch ) ).to.be.true;
  47. } );
  48. it( 'should clear redo command stack whenever a new batch is applied to the document', () => {
  49. sinon.spy( undo._redoCommand, 'clearStack' );
  50. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  51. expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
  52. } );
  53. } );