multicommand.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 MultiCommand from '../src/multicommand';
  6. import ModelTestEditor from './_utils/modeltesteditor';
  7. import Command from '../src/command';
  8. describe( 'MultiCommand', () => {
  9. let editor, multiCommand;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. multiCommand = new MultiCommand( editor );
  16. } );
  17. } );
  18. afterEach( () => {
  19. multiCommand.destroy();
  20. return editor.destroy();
  21. } );
  22. describe( 'isEnabled', () => {
  23. it( 'is always falsy when no child commands are registered', () => {
  24. expect( multiCommand.isEnabled ).to.false;
  25. multiCommand.refresh();
  26. expect( multiCommand.isEnabled ).to.false;
  27. } );
  28. it( 'is set to true if one of registered commands is true', () => {
  29. expect( multiCommand.isEnabled ).to.false;
  30. const commandA = new Command( editor );
  31. const commandB = new Command( editor );
  32. multiCommand.registerChildCommand( commandA );
  33. multiCommand.registerChildCommand( commandB );
  34. expect( multiCommand.isEnabled ).to.false;
  35. commandA.isEnabled = true;
  36. expect( multiCommand.isEnabled ).to.be.true;
  37. commandA.isEnabled = false;
  38. expect( multiCommand.isEnabled ).to.be.false;
  39. commandB.isEnabled = true;
  40. expect( multiCommand.isEnabled ).to.be.true;
  41. } );
  42. } );
  43. describe( 'execute()', () => {
  44. it( 'does not call any command if all are disabled', () => {
  45. const commandA = new Command( editor );
  46. const commandB = new Command( editor );
  47. multiCommand.registerChildCommand( commandA );
  48. multiCommand.registerChildCommand( commandB );
  49. const spyA = sinon.spy( commandA, 'execute' );
  50. const spyB = sinon.spy( commandB, 'execute' );
  51. multiCommand.execute();
  52. sinon.assert.notCalled( spyA );
  53. sinon.assert.notCalled( spyB );
  54. } );
  55. it( 'executes enabled command', () => {
  56. const commandA = new Command( editor );
  57. const commandB = new Command( editor );
  58. const commandC = new Command( editor );
  59. multiCommand.registerChildCommand( commandA );
  60. multiCommand.registerChildCommand( commandB );
  61. multiCommand.registerChildCommand( commandC );
  62. const spyA = sinon.spy( commandA, 'execute' );
  63. const spyB = sinon.spy( commandB, 'execute' );
  64. const spyC = sinon.spy( commandC, 'execute' );
  65. commandC.isEnabled = true;
  66. multiCommand.execute();
  67. sinon.assert.notCalled( spyA );
  68. sinon.assert.notCalled( spyB );
  69. sinon.assert.calledOnce( spyC );
  70. } );
  71. it( 'returns the result of command\'s execute()', () => {
  72. const command = new Command( editor );
  73. const commandResult = { foo: 'bar' };
  74. sinon.stub( command, 'execute' ).returns( commandResult );
  75. multiCommand.registerChildCommand( command );
  76. command.isEnabled = true;
  77. const multiCommandResult = multiCommand.execute();
  78. expect( multiCommandResult, 'multiCommand.execute()' ).to.equal( commandResult );
  79. expect( multiCommandResult, 'multiCommand.execute()' ).to.deep.equal( { foo: 'bar' } );
  80. } );
  81. it( 'executes first registered command if many are enabled', () => {
  82. const commandA = new Command( editor );
  83. const commandB = new Command( editor );
  84. const commandC = new Command( editor );
  85. multiCommand.registerChildCommand( commandA );
  86. multiCommand.registerChildCommand( commandB );
  87. multiCommand.registerChildCommand( commandC );
  88. const spyA = sinon.spy( commandA, 'execute' );
  89. const spyB = sinon.spy( commandB, 'execute' );
  90. const spyC = sinon.spy( commandC, 'execute' );
  91. commandC.isEnabled = true;
  92. commandB.isEnabled = true;
  93. multiCommand.execute();
  94. sinon.assert.notCalled( spyA );
  95. sinon.assert.calledOnce( spyB );
  96. sinon.assert.notCalled( spyC );
  97. } );
  98. } );
  99. } );