command.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/core/editor.js';
  7. import Command from '/ckeditor5/core/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. describe( 'constructor', () => {
  25. it( 'should create a new command instance, that is enabled and bound to given editor', () => {
  26. expect( command ).to.have.property( 'editor' ).equal( editor );
  27. expect( command.isEnabled ).to.be.true;
  28. } );
  29. it( 'Command should have _doExecute method', () => {
  30. expect( () => {
  31. command._doExecute();
  32. } ).not.to.throw;
  33. } );
  34. it( 'should add listener to its refreshState event if checkSchema method is present', () => {
  35. expect( command._checkEnabled ).to.be.undefined;
  36. command._checkEnabled = sinon.spy();
  37. command.refreshState();
  38. expect( command._checkEnabled.called ).to.be.false;
  39. let newCommand = new CommandWithSchema( editor, true );
  40. sinon.spy( newCommand, '_checkEnabled' );
  41. newCommand.refreshState();
  42. expect( newCommand._checkEnabled.calledOnce ).to.be.true;
  43. } );
  44. } );
  45. describe( 'refreshState', () => {
  46. it( 'should fire refreshState event', () => {
  47. let spy = sinon.spy();
  48. command.on( 'refreshState', spy );
  49. command.refreshState();
  50. expect( spy.called ).to.be.true;
  51. } );
  52. it( 'should set isEnabled property to the value passed by object-reference', () => {
  53. command.on( 'refreshState', ( evt, data ) => {
  54. data.isEnabled = true;
  55. } );
  56. expect( command.isEnabled ).to.be.true;
  57. } );
  58. it( 'should set isEnabled to false if _checkEnabled returns false', () => {
  59. let disabledCommand = new CommandWithSchema( editor, false );
  60. disabledCommand.refreshState();
  61. expect( disabledCommand.isEnabled ).to.be.false;
  62. } );
  63. } );
  64. describe( 'disable', () => {
  65. it( 'should make command disabled', () => {
  66. command._disable();
  67. expect( command.isEnabled ).to.be.false;
  68. } );
  69. it( 'should not make command disabled if there is a high-priority listener forcing command to be enabled', () => {
  70. command.on( 'refreshState', ( evt ) => {
  71. evt.stop();
  72. return true;
  73. }, command, 1 );
  74. command._disable();
  75. expect( command.isEnabled ).to.be.true;
  76. } );
  77. } );
  78. describe( 'enable', () => {
  79. it( 'should make command enabled if it was previously disabled by disable()', () => {
  80. command._disable();
  81. command._enable();
  82. expect( command.isEnabled ).to.be.true;
  83. } );
  84. it( 'should not make command enabled if there are other listeners disabling it', () => {
  85. command._disable();
  86. command.on( 'refreshState', ( evt, data ) => {
  87. data.isEnabled = false;
  88. } );
  89. command.refreshState();
  90. command._enable();
  91. expect( command.isEnabled ).to.be.false;
  92. } );
  93. } );
  94. describe( '_execute', () => {
  95. it( 'should not execute command if it is disabled', () => {
  96. command._disable();
  97. sinon.spy( command, '_doExecute' );
  98. command._execute();
  99. expect( command._doExecute.called ).to.be.false;
  100. } );
  101. it( 'should execute command if it is enabled', () => {
  102. sinon.spy( command, '_doExecute' );
  103. command._execute();
  104. expect( command._doExecute.called ).to.be.true;
  105. } );
  106. } );