undoediting.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 UndoEditing from '../src/undoediting';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. describe( 'UndoEditing', () => {
  10. let editor, undo, model, root;
  11. beforeEach( () => {
  12. editor = new ModelTestEditor();
  13. model = editor.model;
  14. root = model.document.getRoot();
  15. undo = new UndoEditing( editor );
  16. undo.init();
  17. } );
  18. afterEach( () => {
  19. undo.destroy();
  20. } );
  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 add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
  45. sinon.spy( undo._undoCommand, 'addBatch' );
  46. sinon.spy( undo._redoCommand, 'clearStack' );
  47. const batch = new Batch();
  48. undo._redoCommand._createdBatches.add( batch );
  49. model.enqueueChange( batch, writer => {
  50. writer.insertText( 'foobar', root );
  51. } );
  52. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  53. expect( undo._redoCommand.clearStack.called ).to.be.false;
  54. } );
  55. it( 'should add a batch to redo command on undo revert event', () => {
  56. sinon.spy( undo._redoCommand, 'addBatch' );
  57. sinon.spy( undo._redoCommand, 'clearStack' );
  58. undo._undoCommand.fire( 'revert', null, new Batch() );
  59. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  60. expect( undo._redoCommand.clearStack.called ).to.be.false;
  61. } );
  62. it( 'should not add a batch that has only non-document operations', () => {
  63. sinon.spy( undo._undoCommand, 'addBatch' );
  64. model.change( writer => {
  65. const docFrag = writer.createDocumentFragment();
  66. const element = writer.createElement( 'paragraph' );
  67. writer.insert( element, docFrag, 0 );
  68. writer.insertText( 'foo', null, element, 0 );
  69. } );
  70. expect( undo._undoCommand.addBatch.called ).to.be.false;
  71. } );
  72. it( 'should add a batch that has both document and non-document operations', () => {
  73. sinon.spy( undo._undoCommand, 'addBatch' );
  74. model.change( writer => {
  75. const element = writer.createElement( 'paragraph' );
  76. writer.insertText( 'foo', null, element, 0 );
  77. writer.insert( element, root, 0 );
  78. } );
  79. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  80. } );
  81. it( 'should set CTRL+Z keystroke', () => {
  82. const spy = sinon.stub( editor, 'execute' );
  83. const wasHandled = editor.keystrokes.press( {
  84. keyCode: keyCodes.z,
  85. ctrlKey: true,
  86. preventDefault: sinon.spy(),
  87. stopPropagation: sinon.spy()
  88. } );
  89. expect( wasHandled ).to.be.true;
  90. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  91. } );
  92. it( 'should set CTRL+Y keystroke', () => {
  93. const spy = sinon.stub( editor, 'execute' );
  94. const wasHandled = editor.keystrokes.press( {
  95. keyCode: keyCodes.y,
  96. ctrlKey: true,
  97. preventDefault: sinon.spy(),
  98. stopPropagation: sinon.spy()
  99. } );
  100. expect( wasHandled ).to.be.true;
  101. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  102. } );
  103. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  104. const spy = sinon.stub( editor, 'execute' );
  105. const keyEventData = {
  106. keyCode: keyCodes.z,
  107. ctrlKey: true,
  108. shiftKey: true,
  109. preventDefault: sinon.spy(),
  110. stopPropagation: sinon.spy()
  111. };
  112. const wasHandled = editor.keystrokes.press( keyEventData );
  113. expect( wasHandled ).to.be.true;
  114. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  115. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  116. } );
  117. } );