command.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Command from '../src/command';
  6. import ModelTestEditor from './_utils/modeltesteditor';
  7. describe( 'Command', () => {
  8. let editor, command;
  9. beforeEach( () => {
  10. return ModelTestEditor
  11. .create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. command = new Command( editor );
  15. } );
  16. } );
  17. afterEach( () => {
  18. command.destroy();
  19. return editor.destroy();
  20. } );
  21. describe( 'constructor()', () => {
  22. it( 'sets the editor property', () => {
  23. expect( command.editor ).to.equal( editor );
  24. } );
  25. it( 'sets the state properties', () => {
  26. expect( command.value ).to.be.undefined;
  27. expect( command.isEnabled ).to.be.false;
  28. } );
  29. it( 'adds a listener which refreshed the command on editor.document#changesDone', () => {
  30. sinon.spy( command, 'refresh' );
  31. editor.document.fire( 'changesDone' );
  32. expect( command.refresh.calledOnce ).to.be.true;
  33. } );
  34. } );
  35. describe( 'value', () => {
  36. it( 'fires change event', () => {
  37. const spy = sinon.spy();
  38. command.on( 'change:value', spy );
  39. command.value = 1;
  40. expect( spy.calledOnce ).to.be.true;
  41. } );
  42. } );
  43. describe( 'isEnabled', () => {
  44. it( 'fires change event', () => {
  45. const spy = sinon.spy();
  46. command.on( 'change:isEnabled', spy );
  47. command.isEnabled = true;
  48. expect( spy.calledOnce ).to.be.true;
  49. } );
  50. } );
  51. describe( 'execute()', () => {
  52. it( 'is decorated', () => {
  53. const spy = sinon.spy();
  54. command.on( 'execute', spy );
  55. command.isEnabled = true;
  56. command.execute( 1, 2 );
  57. expect( spy.calledOnce ).to.be.true;
  58. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 1, 2 ] );
  59. } );
  60. it( 'is automatically blocked (with low priority listener) if command is disabled', () => {
  61. const spyExecute = sinon.spy();
  62. const spyHighest = sinon.spy();
  63. const spyHigh = sinon.spy();
  64. class SpyCommand extends Command {
  65. execute() {
  66. spyExecute();
  67. }
  68. }
  69. const command = new SpyCommand( editor );
  70. command.on( 'execute', spyHighest, { priority: 'highest' } );
  71. command.on( 'execute', spyHigh, { priority: 'high' } );
  72. command.execute();
  73. expect( spyExecute.called ).to.be.false;
  74. expect( spyHighest.calledOnce ).to.be.true;
  75. expect( spyHigh.called ).to.be.false;
  76. } );
  77. } );
  78. describe( 'refresh()', () => {
  79. it( 'sets isEnabled to true', () => {
  80. command.refresh();
  81. expect( command.isEnabled ).to.be.true;
  82. } );
  83. // This is an acceptance test for the ability to override a command's state from outside
  84. // in a way that at any moment the action can be reverted by just offing the listener and
  85. // refreshing the command once again.
  86. it( 'is safely overridable using change:isEnabled', () => {
  87. command.on( 'change:isEnabled', callback, { priority: 'high' } );
  88. command.isEnabled = false;
  89. command.refresh();
  90. expect( command.isEnabled ).to.be.false;
  91. command.off( 'change:isEnabled', callback );
  92. command.refresh();
  93. expect( command.isEnabled ).to.be.true;
  94. function callback( evt ) {
  95. command.isEnabled = false;
  96. evt.stop();
  97. }
  98. } );
  99. } );
  100. } );