command.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from 'ckeditor5-core/src/editor/editor';
  6. import Command from 'ckeditor5-core/src/command/command';
  7. describe( 'Command', () => {
  8. let editor, command;
  9. class CommandWithSchema extends Command {
  10. constructor( editor, schemaValid ) {
  11. super( editor );
  12. this.schemaValid = schemaValid;
  13. }
  14. _checkEnabled() {
  15. return this.schemaValid;
  16. }
  17. }
  18. beforeEach( () => {
  19. editor = new Editor();
  20. command = new Command( editor );
  21. } );
  22. afterEach( () => {
  23. // Might be redundant if editor destroys the commands.
  24. command.destroy();
  25. editor.destroy();
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'should create a new command instance, that is enabled and bound to given editor', () => {
  29. expect( command ).to.have.property( 'editor' ).equal( editor );
  30. expect( command.isEnabled ).to.be.true;
  31. } );
  32. it( 'Command should have _doExecute method', () => {
  33. expect( () => {
  34. command._doExecute();
  35. } ).not.to.throw;
  36. } );
  37. it( 'should add listener to its refreshState event if checkSchema method is present', () => {
  38. expect( command._checkEnabled ).to.be.undefined;
  39. command._checkEnabled = sinon.spy();
  40. command.refreshState();
  41. expect( command._checkEnabled.called ).to.be.false;
  42. let newCommand = new CommandWithSchema( editor, true );
  43. sinon.spy( newCommand, '_checkEnabled' );
  44. newCommand.refreshState();
  45. expect( newCommand._checkEnabled.calledOnce ).to.be.true;
  46. } );
  47. } );
  48. describe( 'destroy', () => {
  49. it( 'should stop listening', () => {
  50. sinon.spy( command, 'stopListening' );
  51. command.destroy();
  52. expect( command.stopListening.calledOnce ).to.be.true;
  53. } );
  54. } );
  55. describe( 'refreshState', () => {
  56. it( 'should fire refreshState event', () => {
  57. let spy = sinon.spy();
  58. command.on( 'refreshState', spy );
  59. command.refreshState();
  60. expect( spy.called ).to.be.true;
  61. } );
  62. it( 'should set isEnabled property to the value passed by object-reference', () => {
  63. command.on( 'refreshState', ( evt, data ) => {
  64. data.isEnabled = true;
  65. } );
  66. expect( command.isEnabled ).to.be.true;
  67. } );
  68. it( 'should set isEnabled to false if _checkEnabled returns false', () => {
  69. let disabledCommand = new CommandWithSchema( editor, false );
  70. disabledCommand.refreshState();
  71. expect( disabledCommand.isEnabled ).to.be.false;
  72. } );
  73. } );
  74. describe( 'disable', () => {
  75. it( 'should make command disabled', () => {
  76. command._disable();
  77. expect( command.isEnabled ).to.be.false;
  78. } );
  79. it( 'should not make command disabled if there is a high-priority listener forcing command to be enabled', () => {
  80. command.on( 'refreshState', ( evt ) => {
  81. evt.stop();
  82. return true;
  83. }, command, 1 );
  84. command._disable();
  85. expect( command.isEnabled ).to.be.true;
  86. } );
  87. } );
  88. describe( 'enable', () => {
  89. it( 'should make command enabled if it was previously disabled by disable()', () => {
  90. command._disable();
  91. command._enable();
  92. expect( command.isEnabled ).to.be.true;
  93. } );
  94. it( 'should not make command enabled if there are other listeners disabling it', () => {
  95. command._disable();
  96. command.on( 'refreshState', ( evt, data ) => {
  97. data.isEnabled = false;
  98. } );
  99. command.refreshState();
  100. command._enable();
  101. expect( command.isEnabled ).to.be.false;
  102. } );
  103. } );
  104. describe( '_execute', () => {
  105. it( 'should not execute command if it is disabled', () => {
  106. command._disable();
  107. sinon.spy( command, '_doExecute' );
  108. command._execute();
  109. expect( command._doExecute.called ).to.be.false;
  110. } );
  111. it( 'should execute command if it is enabled', () => {
  112. sinon.spy( command, '_doExecute' );
  113. command._execute();
  114. expect( command._doExecute.called ).to.be.true;
  115. } );
  116. } );
  117. } );