command.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.js';
  8. let element, editor, command;
  9. class CommandWithSchema extends Command {
  10. constructor( editor, schemaFails ) {
  11. super( editor );
  12. this.schemaFails = schemaFails;
  13. }
  14. checkSchema() {
  15. return this.schemaFails;
  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 execute method', () => {
  30. expect( command ).to.have.property( 'execute' );
  31. expect( () => {
  32. command.execute();
  33. } ).not.to.throw;
  34. } );
  35. it( 'should add listener to its checkEnabled event if checkSchema method is present', () => {
  36. expect( command.checkSchema ).to.be.undefined;
  37. command.checkSchema = sinon.spy();
  38. command.checkEnabled();
  39. expect( command.checkSchema.called ).to.be.false;
  40. let newCommand = new CommandWithSchema( editor, false );
  41. sinon.spy( newCommand, 'checkSchema' );
  42. newCommand.checkEnabled();
  43. expect( newCommand.checkSchema.calledOnce ).to.be.true;
  44. } );
  45. } );
  46. describe( 'checkEnabled', () => {
  47. it( 'should fire checkEnabled event', () => {
  48. let spy = sinon.spy();
  49. command.on( 'checkEnabled', spy );
  50. command.checkEnabled();
  51. expect( spy.called ).to.be.true;
  52. } );
  53. it( 'should set isEnabled property to the value returned by last event callback', () => {
  54. command.on( 'checkEnabled', () => {
  55. return true;
  56. } );
  57. command.on( 'checkEnabled', ( evt ) => {
  58. evt.stop();
  59. return false;
  60. } );
  61. command.on( 'checkEnabled', () => {
  62. return true;
  63. } );
  64. command.checkEnabled();
  65. expect( command.isEnabled ).to.be.false;
  66. } );
  67. } );
  68. describe( 'disable', () => {
  69. it( 'should make command disabled', () => {
  70. command._disable();
  71. expect( command.isEnabled ).to.be.false;
  72. } );
  73. it( 'should not make command disabled if there is a high-priority listener forcing command to be enabled', () => {
  74. command.on( 'checkEnabled', ( evt ) => {
  75. evt.stop();
  76. return true;
  77. }, command, 1 );
  78. command._disable();
  79. expect( command.isEnabled ).to.be.true;
  80. } );
  81. } );
  82. describe( 'enable', () => {
  83. it( 'should make command enabled if it was previously disabled by disable()', () => {
  84. command._disable();
  85. command._enable();
  86. expect( command.isEnabled ).to.be.true;
  87. } );
  88. it( 'should not make command enabled if there are other listeners disabling it', () => {
  89. command._disable();
  90. command.on( 'checkEnabled', ( evt ) => {
  91. evt.stop();
  92. return false;
  93. } );
  94. command.checkEnabled();
  95. command._enable();
  96. expect( command.isEnabled ).to.be.false;
  97. } );
  98. } );