undo.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 'ckeditor5-core/tests/_utils/classictesteditor';
  7. import Undo from 'ckeditor5-undo/src/undo';
  8. import UndoEngine from 'ckeditor5-undo/src/undoengine';
  9. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  10. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  11. import { keyCodes } from 'ckeditor5-utils/src/keyboard';
  12. testUtils.createSinonSandbox();
  13. describe( 'Undo', () => {
  14. let editor;
  15. beforeEach( () => {
  16. const editorElement = document.createElement( 'div' );
  17. return ClassicTestEditor.create( editorElement, {
  18. plugins: [ 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', 'Undo', 'CTRL+Z' );
  31. testButton( 'redo', '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, label, featureKeystroke ) {
  51. describe( `${ featureName } button`, () => {
  52. let button;
  53. beforeEach( () => {
  54. button = editor.ui.componentFactory.create( featureName );
  55. } );
  56. it( 'should register feature component', () => {
  57. expect( button ).to.be.instanceOf( ButtonView );
  58. } );
  59. it( 'should create UI component with correct attribute values', () => {
  60. expect( button.isOn ).to.be.false;
  61. expect( button.label ).to.equal( label );
  62. expect( button.icon ).to.match( /<svg / );
  63. expect( button.keystroke ).to.equal( featureKeystroke );
  64. } );
  65. it( `should execute ${ featureName } command on model execute event`, () => {
  66. const executeSpy = testUtils.sinon.stub( editor, 'execute' );
  67. button.fire( 'execute' );
  68. sinon.assert.calledOnce( executeSpy );
  69. sinon.assert.calledWithExactly( executeSpy, featureName );
  70. } );
  71. it( `should bind model to ${ featureName } command`, () => {
  72. const command = editor.commands.get( featureName );
  73. expect( button.isOn ).to.be.false;
  74. const initState = command.isEnabled;
  75. expect( button.isEnabled ).to.equal( initState );
  76. command.isEnabled = !initState;
  77. expect( button.isEnabled ).to.equal( !initState );
  78. } );
  79. it( 'should set keystroke in the model', () => {
  80. expect( button.keystroke ).to.equal( featureKeystroke );
  81. } );
  82. } );
  83. }
  84. } );