8
0

command.js 2.9 KB

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