undo.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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( { keyCode: keyCodes.z, ctrlKey: true } );
  38. expect( wasHandled ).to.be.true;
  39. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  40. } );
  41. it( 'should set CTRL+Y keystroke', () => {
  42. const spy = sinon.stub( editor, 'execute' );
  43. const wasHandled = editor.keystrokes.press( { keyCode: keyCodes.y, ctrlKey: true } );
  44. expect( wasHandled ).to.be.true;
  45. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  46. } );
  47. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  48. const spy = sinon.stub( editor, 'execute' );
  49. const wasHandled = editor.keystrokes.press( { keyCode: keyCodes.z, ctrlKey: true, shiftKey: true } );
  50. expect( wasHandled ).to.be.true;
  51. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  52. } );
  53. function testButton( featureName, label, featureKeystroke ) {
  54. describe( `${ featureName } button`, () => {
  55. let button;
  56. beforeEach( () => {
  57. button = editor.ui.componentFactory.create( featureName );
  58. } );
  59. it( 'should register feature component', () => {
  60. expect( button ).to.be.instanceOf( ButtonView );
  61. } );
  62. it( 'should create UI component with correct attribute values', () => {
  63. expect( button.isOn ).to.be.false;
  64. expect( button.label ).to.equal( label );
  65. expect( button.icon ).to.match( /<svg / );
  66. expect( button.keystroke ).to.equal( featureKeystroke );
  67. } );
  68. it( `should execute ${ featureName } command on model execute event`, () => {
  69. const executeSpy = testUtils.sinon.stub( editor, 'execute' );
  70. button.fire( 'execute' );
  71. sinon.assert.calledOnce( executeSpy );
  72. sinon.assert.calledWithExactly( executeSpy, featureName );
  73. } );
  74. it( `should bind model to ${ featureName } command`, () => {
  75. const command = editor.commands.get( featureName );
  76. expect( button.isOn ).to.be.false;
  77. const initState = command.isEnabled;
  78. expect( button.isEnabled ).to.equal( initState );
  79. command.isEnabled = !initState;
  80. expect( button.isEnabled ).to.equal( !initState );
  81. } );
  82. it( 'should set keystroke in the model', () => {
  83. expect( button.keystroke ).to.equal( featureKeystroke );
  84. } );
  85. } );
  86. }
  87. } );