command.js 3.6 KB

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