8
0

undo.js 2.9 KB

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