8
0

command.js 3.7 KB

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