command.js 3.8 KB

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