8
0

undoui.js 4.0 KB

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