command.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. import Command from '../src/command';
  6. import ModelTestEditor from './_utils/modeltesteditor';
  7. describe( 'Command', () => {
  8. let editor, command;
  9. beforeEach( () => {
  10. return ModelTestEditor
  11. .create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. command = new Command( editor );
  15. } );
  16. } );
  17. afterEach( () => {
  18. command.destroy();
  19. return editor.destroy();
  20. } );
  21. describe( 'constructor()', () => {
  22. it( 'sets the editor property', () => {
  23. expect( command.editor ).to.equal( editor );
  24. } );
  25. it( 'sets the state properties', () => {
  26. expect( command.value ).to.be.undefined;
  27. expect( command.isEnabled ).to.be.false;
  28. } );
  29. it( 'adds a listener which refreshes the command on editor.model.Document#event:change', () => {
  30. sinon.spy( command, 'refresh' );
  31. editor.model.document.fire( 'change' );
  32. expect( command.refresh.calledOnce ).to.be.true;
  33. } );
  34. } );
  35. describe( 'value', () => {
  36. it( 'fires change event', () => {
  37. const spy = sinon.spy();
  38. command.on( 'change:value', spy );
  39. command.value = 1;
  40. expect( spy.calledOnce ).to.be.true;
  41. } );
  42. } );
  43. describe( 'isEnabled', () => {
  44. it( 'fires change event', () => {
  45. const spy = sinon.spy();
  46. command.on( 'change:isEnabled', spy );
  47. command.isEnabled = true;
  48. expect( spy.calledOnce ).to.be.true;
  49. } );
  50. it( 'is always falsy when the editor is in read-only mode', () => {
  51. editor.isReadOnly = false;
  52. command.isEnabled = true;
  53. editor.isReadOnly = true;
  54. // Is false.
  55. expect( command.isEnabled ).to.false;
  56. command.refresh();
  57. // Still false.
  58. expect( command.isEnabled ).to.false;
  59. editor.isReadOnly = false;
  60. // And is back to true.
  61. expect( command.isEnabled ).to.true;
  62. } );
  63. it( 'is observable when is overridden', () => {
  64. editor.isReadOnly = false;
  65. command.isEnabled = true;
  66. editor.bind( 'something' ).to( command, 'isEnabled' );
  67. expect( editor.something ).to.true;
  68. editor.isReadOnly = true;
  69. expect( editor.something ).to.false;
  70. } );
  71. it( 'stops `set` event to force disabled and not affect `change` event', () => {
  72. const setSpy = sinon.spy();
  73. const changeSpy = sinon.spy();
  74. command.isEnabled = true;
  75. editor.isReadOnly = false;
  76. command.on( 'set', setSpy );
  77. command.on( 'change', changeSpy );
  78. editor.isReadOnly = true;
  79. sinon.assert.notCalled( setSpy );
  80. sinon.assert.calledOnce( changeSpy );
  81. } );
  82. } );
  83. describe( 'execute()', () => {
  84. it( 'is decorated', () => {
  85. const spy = sinon.spy();
  86. command.on( 'execute', spy );
  87. command.isEnabled = true;
  88. command.execute( 1, 2 );
  89. expect( spy.calledOnce ).to.be.true;
  90. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 1, 2 ] );
  91. } );
  92. it( 'is automatically blocked (with low priority listener) if command is disabled', () => {
  93. const spyExecute = sinon.spy();
  94. const spyHighest = sinon.spy();
  95. const spyHigh = sinon.spy();
  96. class SpyCommand extends Command {
  97. execute() {
  98. spyExecute();
  99. }
  100. }
  101. const command = new SpyCommand( editor );
  102. command.on( 'execute', spyHighest, { priority: 'highest' } );
  103. command.on( 'execute', spyHigh, { priority: 'high' } );
  104. command.execute();
  105. expect( spyExecute.called ).to.be.false;
  106. expect( spyHighest.calledOnce ).to.be.true;
  107. expect( spyHigh.called ).to.be.false;
  108. } );
  109. } );
  110. describe( 'refresh()', () => {
  111. it( 'sets isEnabled to true', () => {
  112. command.refresh();
  113. expect( command.isEnabled ).to.be.true;
  114. } );
  115. // This is an acceptance test for the ability to override a command's state from outside
  116. // in a way that at any moment the action can be reverted by just offing the listener and
  117. // refreshing the command once again.
  118. it( 'is safely overridable using change:isEnabled', () => {
  119. command.on( 'change:isEnabled', callback, { priority: 'high' } );
  120. command.isEnabled = false;
  121. command.refresh();
  122. expect( command.isEnabled ).to.be.false;
  123. command.off( 'change:isEnabled', callback );
  124. command.refresh();
  125. expect( command.isEnabled ).to.be.true;
  126. function callback( evt ) {
  127. command.isEnabled = false;
  128. evt.stop();
  129. }
  130. } );
  131. } );
  132. describe( 'forceDisabled() / clearForceDisabled()', () => {
  133. it( 'forceDisabled() should disable the command', () => {
  134. command.forceDisabled( 'foo' );
  135. command.isEnabled = true;
  136. expect( command.isEnabled ).to.be.false;
  137. } );
  138. it( 'clearForceDisabled() should enable the command', () => {
  139. command.forceDisabled( 'foo' );
  140. command.clearForceDisabled( 'foo' );
  141. expect( command.isEnabled ).to.be.true;
  142. } );
  143. it( 'clearForceDisabled() used with wrong identifier should not enable the command', () => {
  144. command.forceDisabled( 'foo' );
  145. command.clearForceDisabled( 'bar' );
  146. command.isEnabled = true;
  147. expect( command.isEnabled ).to.be.false;
  148. } );
  149. it( 'using forceDisabled() twice with the same identifier should not have any effect', () => {
  150. command.forceDisabled( 'foo' );
  151. command.forceDisabled( 'foo' );
  152. command.clearForceDisabled( 'foo' );
  153. expect( command.isEnabled ).to.be.true;
  154. } );
  155. it( 'command is enabled only after all disables were cleared', () => {
  156. command.forceDisabled( 'foo' );
  157. command.forceDisabled( 'bar' );
  158. command.clearForceDisabled( 'foo' );
  159. command.isEnabled = true;
  160. expect( command.isEnabled ).to.be.false;
  161. command.clearForceDisabled( 'bar' );
  162. expect( command.isEnabled ).to.be.true;
  163. } );
  164. it( 'command should remain disabled if isEnabled has a callback disabling it', () => {
  165. command.on( 'set:isEnabled', evt => {
  166. evt.return = false;
  167. evt.stop();
  168. } );
  169. command.forceDisabled( 'foo' );
  170. command.clearForceDisabled( 'foo' );
  171. command.isEnabled = true;
  172. expect( command.isEnabled ).to.be.false;
  173. } );
  174. } );
  175. } );