undoediting.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 '../../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 set CTRL+Z keystroke', () => {
  63. const spy = sinon.stub( editor, 'execute' );
  64. const wasHandled = editor.keystrokes.press( {
  65. keyCode: keyCodes.z,
  66. ctrlKey: true,
  67. preventDefault: sinon.spy(),
  68. stopPropagation: sinon.spy()
  69. } );
  70. expect( wasHandled ).to.be.true;
  71. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  72. } );
  73. it( 'should set CTRL+Y keystroke', () => {
  74. const spy = sinon.stub( editor, 'execute' );
  75. const wasHandled = editor.keystrokes.press( {
  76. keyCode: keyCodes.y,
  77. ctrlKey: true,
  78. preventDefault: sinon.spy(),
  79. stopPropagation: sinon.spy()
  80. } );
  81. expect( wasHandled ).to.be.true;
  82. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  83. } );
  84. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  85. const spy = sinon.stub( editor, 'execute' );
  86. const keyEventData = {
  87. keyCode: keyCodes.z,
  88. ctrlKey: true,
  89. shiftKey: true,
  90. preventDefault: sinon.spy(),
  91. stopPropagation: sinon.spy()
  92. };
  93. const wasHandled = editor.keystrokes.press( keyEventData );
  94. expect( wasHandled ).to.be.true;
  95. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  96. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  97. } );
  98. } );