command.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 refreshed the command on editor.model.document#changesDone', () => {
  30. sinon.spy( command, 'refresh' );
  31. editor.model.document.fire( 'changesDone' );
  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. } );
  72. describe( 'execute()', () => {
  73. it( 'is decorated', () => {
  74. const spy = sinon.spy();
  75. command.on( 'execute', spy );
  76. command.isEnabled = true;
  77. command.execute( 1, 2 );
  78. expect( spy.calledOnce ).to.be.true;
  79. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 1, 2 ] );
  80. } );
  81. it( 'is automatically blocked (with low priority listener) if command is disabled', () => {
  82. const spyExecute = sinon.spy();
  83. const spyHighest = sinon.spy();
  84. const spyHigh = sinon.spy();
  85. class SpyCommand extends Command {
  86. execute() {
  87. spyExecute();
  88. }
  89. }
  90. const command = new SpyCommand( editor );
  91. command.on( 'execute', spyHighest, { priority: 'highest' } );
  92. command.on( 'execute', spyHigh, { priority: 'high' } );
  93. command.execute();
  94. expect( spyExecute.called ).to.be.false;
  95. expect( spyHighest.calledOnce ).to.be.true;
  96. expect( spyHigh.called ).to.be.false;
  97. } );
  98. } );
  99. describe( 'refresh()', () => {
  100. it( 'sets isEnabled to true', () => {
  101. command.refresh();
  102. expect( command.isEnabled ).to.be.true;
  103. } );
  104. // This is an acceptance test for the ability to override a command's state from outside
  105. // in a way that at any moment the action can be reverted by just offing the listener and
  106. // refreshing the command once again.
  107. it( 'is safely overridable using change:isEnabled', () => {
  108. command.on( 'change:isEnabled', callback, { priority: 'high' } );
  109. command.isEnabled = false;
  110. command.refresh();
  111. expect( command.isEnabled ).to.be.false;
  112. command.off( 'change:isEnabled', callback );
  113. command.refresh();
  114. expect( command.isEnabled ).to.be.true;
  115. function callback( evt ) {
  116. command.isEnabled = false;
  117. evt.stop();
  118. }
  119. } );
  120. } );
  121. } );