8
0

command.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. checkSchema() {
  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 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 refreshState event if checkSchema method is present', () => {
  36. expect( command.checkSchema ).to.be.undefined;
  37. command.checkSchema = sinon.spy();
  38. command.refreshState();
  39. expect( command.checkSchema.called ).to.be.false;
  40. let newCommand = new CommandWithSchema( editor, true );
  41. sinon.spy( newCommand, 'checkSchema' );
  42. newCommand.refreshState();
  43. expect( newCommand.checkSchema.calledOnce ).to.be.true;
  44. } );
  45. } );
  46. describe( 'refreshState', () => {
  47. it( 'should fire refreshState event', () => {
  48. let spy = sinon.spy();
  49. command.on( 'refreshState', spy );
  50. command.refreshState();
  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( 'refreshState', () => {
  55. return true;
  56. } );
  57. command.on( 'refreshState', ( evt ) => {
  58. evt.stop();
  59. return false;
  60. } );
  61. command.on( 'refreshState', () => {
  62. return true;
  63. } );
  64. command.refreshState();
  65. expect( command.isEnabled ).to.be.false;
  66. } );
  67. it( 'should set isEnabled to false if checkSchema returns false', () => {
  68. let disabledCommand = new CommandWithSchema( editor, false );
  69. disabledCommand.refreshState();
  70. expect( disabledCommand.isEnabled ).to.be.false;
  71. } );
  72. } );
  73. describe( 'disable', () => {
  74. it( 'should make command disabled', () => {
  75. command._disable();
  76. expect( command.isEnabled ).to.be.false;
  77. } );
  78. it( 'should not make command disabled if there is a high-priority listener forcing command to be enabled', () => {
  79. command.on( 'refreshState', ( evt ) => {
  80. evt.stop();
  81. return true;
  82. }, command, 1 );
  83. command._disable();
  84. expect( command.isEnabled ).to.be.true;
  85. } );
  86. } );
  87. describe( 'enable', () => {
  88. it( 'should make command enabled if it was previously disabled by disable()', () => {
  89. command._disable();
  90. command._enable();
  91. expect( command.isEnabled ).to.be.true;
  92. } );
  93. it( 'should not make command enabled if there are other listeners disabling it', () => {
  94. command._disable();
  95. command.on( 'refreshState', ( evt ) => {
  96. evt.stop();
  97. return false;
  98. } );
  99. command.refreshState();
  100. command._enable();
  101. expect( command.isEnabled ).to.be.false;
  102. } );
  103. } );