8
0

undo.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Undo from '../src/undo';
  8. import UndoEngine from '../src/undoengine';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. testUtils.createSinonSandbox();
  13. describe( 'Undo', () => {
  14. let editor, editorElement;
  15. beforeEach( () => {
  16. editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. return ClassicTestEditor.create( editorElement, { plugins: [ Undo ] } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. } );
  22. } );
  23. afterEach( () => {
  24. editorElement.remove();
  25. return editor.destroy();
  26. } );
  27. it( 'should be loaded', () => {
  28. expect( editor.plugins.get( Undo ) ).to.be.instanceOf( Undo );
  29. } );
  30. it( 'should load UndoEngine', () => {
  31. expect( editor.plugins.get( UndoEngine ) ).to.be.instanceOf( UndoEngine );
  32. } );
  33. testButton( 'undo', 'Undo', 'CTRL+Z' );
  34. testButton( 'redo', 'Redo', 'CTRL+Y' );
  35. it( 'should set CTRL+Z keystroke', () => {
  36. const spy = sinon.stub( editor, 'execute' );
  37. const wasHandled = editor.keystrokes.press( {
  38. keyCode: keyCodes.z,
  39. ctrlKey: true,
  40. preventDefault: sinon.spy(),
  41. stopPropagation: sinon.spy()
  42. } );
  43. expect( wasHandled ).to.be.true;
  44. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  45. } );
  46. it( 'should set CTRL+Y keystroke', () => {
  47. const spy = sinon.stub( editor, 'execute' );
  48. const wasHandled = editor.keystrokes.press( {
  49. keyCode: keyCodes.y,
  50. ctrlKey: true,
  51. preventDefault: sinon.spy(),
  52. stopPropagation: sinon.spy()
  53. } );
  54. expect( wasHandled ).to.be.true;
  55. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  56. } );
  57. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  58. const spy = sinon.stub( editor, 'execute' );
  59. const wasHandled = editor.keystrokes.press( {
  60. keyCode: keyCodes.z,
  61. ctrlKey: true,
  62. shiftKey: true,
  63. preventDefault: sinon.spy(),
  64. stopPropagation: sinon.spy()
  65. } );
  66. expect( wasHandled ).to.be.true;
  67. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  68. } );
  69. function testButton( featureName, label, featureKeystroke ) {
  70. describe( `${ featureName } button`, () => {
  71. let button;
  72. beforeEach( () => {
  73. button = editor.ui.componentFactory.create( featureName );
  74. } );
  75. it( 'should register feature component', () => {
  76. expect( button ).to.be.instanceOf( ButtonView );
  77. } );
  78. it( 'should create UI component with correct attribute values', () => {
  79. expect( button.isOn ).to.be.false;
  80. expect( button.label ).to.equal( label );
  81. expect( button.icon ).to.match( /<svg / );
  82. expect( button.keystroke ).to.equal( featureKeystroke );
  83. } );
  84. it( `should execute ${ featureName } command on model execute event`, () => {
  85. const executeSpy = testUtils.sinon.stub( editor, 'execute' );
  86. button.fire( 'execute' );
  87. sinon.assert.calledOnce( executeSpy );
  88. sinon.assert.calledWithExactly( executeSpy, featureName );
  89. } );
  90. it( `should bind model to ${ featureName } command`, () => {
  91. const command = editor.commands.get( featureName );
  92. expect( button.isOn ).to.be.false;
  93. const initState = command.isEnabled;
  94. expect( button.isEnabled ).to.equal( initState );
  95. command.isEnabled = !initState;
  96. expect( button.isEnabled ).to.equal( !initState );
  97. } );
  98. it( 'should set keystroke in the model', () => {
  99. expect( button.keystroke ).to.equal( featureKeystroke );
  100. } );
  101. } );
  102. }
  103. } );