8
0

undofeature.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. 'use strict';
  7. import Editor from '/ckeditor5/editor.js';
  8. import ModelDocument from '/ckeditor5/engine/model/document.js';
  9. import Position from '/ckeditor5/engine/model/position.js';
  10. import UndoFeature from '/ckeditor5/undo/undo.js';
  11. let element, editor, undo, batch, doc, root;
  12. beforeEach( () => {
  13. element = document.createElement( 'div' );
  14. document.body.appendChild( element );
  15. editor = new Editor( element );
  16. doc = new ModelDocument();
  17. editor.document = doc;
  18. batch = doc.batch();
  19. root = doc.createRoot( 'root' );
  20. undo = new UndoFeature( editor );
  21. undo.init();
  22. } );
  23. afterEach( () => {
  24. undo.destroy();
  25. } );
  26. describe( 'UndoFeature', () => {
  27. it( 'should register undo command and redo command', () => {
  28. expect( editor.commands.get( 'undo' ) ).to.equal( undo._undoCommand );
  29. expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
  30. } );
  31. it( 'should add a batch to undo command whenever a new batch is applied to the document', () => {
  32. sinon.spy( undo._undoCommand, 'addBatch' );
  33. expect( undo._undoCommand.addBatch.called ).to.be.false;
  34. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  35. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  36. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  37. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  38. } );
  39. it( 'should add a batch to redo command whenever a batch is undone by undo command', () => {
  40. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  41. sinon.spy( undo._redoCommand, 'addBatch' );
  42. undo._undoCommand.fire( 'revert', batch );
  43. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  44. expect( undo._redoCommand.addBatch.calledWith( batch ) ).to.be.true;
  45. } );
  46. it( 'should add a batch to undo command whenever a batch is redone by redo command', () => {
  47. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  48. sinon.spy( undo._undoCommand, 'addBatch' );
  49. undo._redoCommand.fire( 'revert', batch );
  50. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  51. expect( undo._undoCommand.addBatch.calledWith( batch ) ).to.be.true;
  52. } );
  53. it( 'should clear redo command stack whenever a new batch is applied to the document', () => {
  54. sinon.spy( undo._redoCommand, 'clearStack' );
  55. batch.insert( new Position( root, [ 0 ] ), 'foobar' );
  56. expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
  57. } );
  58. } );