command.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class SomeCommand extends Command {
  8. execute() {}
  9. }
  10. describe( 'Command', () => {
  11. let editor, command;
  12. beforeEach( () => {
  13. return ModelTestEditor
  14. .create()
  15. .then( newEditor => {
  16. editor = newEditor;
  17. command = new SomeCommand( editor );
  18. } );
  19. } );
  20. afterEach( () => {
  21. command.destroy();
  22. return editor.destroy();
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'sets the editor property', () => {
  26. expect( command.editor ).to.equal( editor );
  27. } );
  28. it( 'sets the state properties', () => {
  29. expect( command.value ).to.be.undefined;
  30. expect( command.isEnabled ).to.be.true;
  31. } );
  32. it( 'adds a listener which refreshed the command on editor.document#changesDone', () => {
  33. sinon.spy( command, 'refresh' );
  34. editor.document.fire( 'changesDone' );
  35. expect( command.refresh.calledOnce ).to.be.true;
  36. } );
  37. } );
  38. describe( 'value', () => {
  39. it( 'fires change event', () => {
  40. const spy = sinon.spy();
  41. command.on( 'change:value', spy );
  42. command.value = 1;
  43. expect( spy.calledOnce ).to.be.true;
  44. } );
  45. } );
  46. describe( 'isEnabled', () => {
  47. it( 'fires change event', () => {
  48. const spy = sinon.spy();
  49. command.on( 'change:isEnabled', spy );
  50. command.isEnabled = false;
  51. expect( spy.calledOnce ).to.be.true;
  52. } );
  53. } );
  54. describe( 'execute()', () => {
  55. it( 'is decorated', () => {
  56. const spy = sinon.spy();
  57. command.on( 'execute', spy );
  58. command.execute( 1, 2 );
  59. expect( spy.calledOnce ).to.be.true;
  60. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 1, 2 ] );
  61. } );
  62. } );
  63. describe( 'refresh()', () => {
  64. it( 'sets isEnabled to true', () => {
  65. command.isEnabled = false;
  66. command.refresh();
  67. expect( command.isEnabled ).to.be.true;
  68. } );
  69. // This is an acceptance test for the ability to override a command's state from outside
  70. // in a way that at any moment the action can be reverted by just offing the listener and
  71. // refreshing the command once again.
  72. it( 'is safely overridable using change:isEnabled', () => {
  73. command.on( 'change:isEnabled', callback, { priority: 'high' } );
  74. command.isEnabled = false;
  75. command.refresh();
  76. expect( command.isEnabled ).to.be.false;
  77. command.off( 'change:isEnabled', callback );
  78. command.refresh();
  79. expect( command.isEnabled ).to.be.true;
  80. function callback( evt ) {
  81. command.isEnabled = false;
  82. evt.stop();
  83. }
  84. } );
  85. } );
  86. } );