undo.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '/tests/core/_utils/classictesteditor.js';
  7. import Undo from '/ckeditor5/undo/undo.js';
  8. import UndoEngine from '/ckeditor5/undo/undoengine.js';
  9. import ButtonController from '/ckeditor5/ui/button/button.js';
  10. import testUtils from '/tests/core/_utils/utils.js';
  11. import { keyCodes } from '/ckeditor5/utils/keyboard.js';
  12. testUtils.createSinonSandbox();
  13. describe( 'Undo', () => {
  14. let editor;
  15. beforeEach( () => {
  16. const editorElement = document.createElement( 'div' );
  17. return ClassicTestEditor.create( editorElement, {
  18. features: [ 'undo' ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. } );
  23. } );
  24. it( 'should be loaded', () => {
  25. expect( editor.plugins.get( Undo ) ).to.be.instanceOf( Undo );
  26. } );
  27. it( 'should load UndoEngine', () => {
  28. expect( editor.plugins.get( UndoEngine ) ).to.be.instanceOf( UndoEngine );
  29. } );
  30. testButton( 'undo', 'CTRL+Z' );
  31. testButton( 'redo', 'CTRL+Y' );
  32. it( 'should set CTRL+Z keystroke', () => {
  33. const spy = sinon.stub( editor, 'execute' );
  34. const wasHandled = editor.keystrokes.press( { keyCode: keyCodes.z, ctrlKey: true } );
  35. expect( wasHandled ).to.be.true;
  36. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  37. } );
  38. it( 'should set CTRL+Y keystroke', () => {
  39. const spy = sinon.stub( editor, 'execute' );
  40. const wasHandled = editor.keystrokes.press( { keyCode: keyCodes.y, ctrlKey: true } );
  41. expect( wasHandled ).to.be.true;
  42. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  43. } );
  44. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  45. const spy = sinon.stub( editor, 'execute' );
  46. const wasHandled = editor.keystrokes.press( { keyCode: keyCodes.z, ctrlKey: true, shiftKey: true } );
  47. expect( wasHandled ).to.be.true;
  48. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  49. } );
  50. function testButton( featureName, featureKeystroke ) {
  51. describe( `${ featureName } button`, () => {
  52. let buttonController;
  53. beforeEach( () => {
  54. buttonController = editor.ui.featureComponents.create( featureName );
  55. } );
  56. it( 'should register feature component', () => {
  57. expect( buttonController ).to.be.instanceOf( ButtonController );
  58. } );
  59. it( `should execute ${ featureName } command on model execute event`, () => {
  60. const executeSpy = testUtils.sinon.stub( editor, 'execute' );
  61. const model = buttonController.model;
  62. model.fire( 'execute' );
  63. sinon.assert.calledOnce( executeSpy );
  64. sinon.assert.calledWithExactly( executeSpy, featureName );
  65. } );
  66. it( `should bind model to ${ featureName } command`, () => {
  67. const model = buttonController.model;
  68. const command = editor.commands.get( featureName );
  69. expect( model.isOn ).to.be.false;
  70. const initState = command.isEnabled;
  71. expect( model.isEnabled ).to.equal( initState );
  72. command.isEnabled = !initState;
  73. expect( model.isEnabled ).to.equal( !initState );
  74. } );
  75. it( 'should set keystroke in the model', () => {
  76. expect( buttonController.model.keystroke ).to.equal( featureKeystroke );
  77. } );
  78. } );
  79. }
  80. } );