pendingactions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtaulTestEditor from './_utils/virtualtesteditor';
  6. import PendingActions from '../src/pendingactions';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. let editor, pendingActions;
  9. beforeEach( () => {
  10. return VirtaulTestEditor.create( {
  11. plugins: [ PendingActions ],
  12. } ).then( newEditor => {
  13. editor = newEditor;
  14. pendingActions = editor.plugins.get( PendingActions );
  15. } );
  16. } );
  17. afterEach( () => {
  18. return editor.destroy();
  19. } );
  20. describe( 'PendingActions', () => {
  21. it( 'should define static pluginName property', () => {
  22. expect( PendingActions ).to.have.property( 'pluginName', 'PendingActions' );
  23. } );
  24. describe( 'init()', () => {
  25. it( 'should have hasAny observable', () => {
  26. const spy = sinon.spy();
  27. pendingActions.on( 'change:hasAny', spy );
  28. expect( pendingActions ).to.have.property( 'hasAny', false );
  29. pendingActions.hasAny = true;
  30. sinon.assert.calledOnce( spy );
  31. } );
  32. } );
  33. describe( 'add()', () => {
  34. it( 'should register and return pending action', () => {
  35. const action = pendingActions.add( 'Action' );
  36. expect( action ).be.an( 'object' );
  37. expect( action.message ).to.equal( 'Action' );
  38. } );
  39. it( 'should return observable', () => {
  40. const spy = sinon.spy();
  41. const action = pendingActions.add( 'Action' );
  42. action.on( 'change:message', spy );
  43. action.message = 'New message';
  44. sinon.assert.calledOnce( spy );
  45. } );
  46. it( 'should update hasAny observable', () => {
  47. expect( pendingActions ).to.have.property( 'hasAny', false );
  48. pendingActions.add( 'Action' );
  49. expect( pendingActions ).to.have.property( 'hasAny', true );
  50. } );
  51. it( 'should throw an error when invalid message is given', () => {
  52. expect( () => {
  53. pendingActions.add( {} );
  54. } ).to.throw( CKEditorError, /^pendingactions-add-invalid-message/ );
  55. } );
  56. it( 'should fire add event with added item', () => {
  57. const spy = sinon.spy();
  58. pendingActions.on( 'add', spy );
  59. const action = pendingActions.add( 'Some action' );
  60. sinon.assert.calledWith( spy, sinon.match.any, action );
  61. } );
  62. } );
  63. describe( 'remove()', () => {
  64. it( 'should remove given pending action and update observable', () => {
  65. const action1 = pendingActions.add( 'Action 1' );
  66. const action2 = pendingActions.add( 'Action 2' );
  67. expect( pendingActions ).to.have.property( 'hasAny', true );
  68. pendingActions.remove( action1 );
  69. expect( pendingActions ).to.have.property( 'hasAny', true );
  70. pendingActions.remove( action2 );
  71. expect( pendingActions ).to.have.property( 'hasAny', false );
  72. } );
  73. it( 'should fire remove event with removed item', () => {
  74. const spy = sinon.spy();
  75. pendingActions.on( 'remove', spy );
  76. const action = pendingActions.add( 'Some action' );
  77. pendingActions.remove( action );
  78. sinon.assert.calledWith( spy, sinon.match.any, action );
  79. } );
  80. } );
  81. describe( 'first', () => {
  82. it( 'should return first pending action from the list', () => {
  83. expect( pendingActions.first ).to.be.null;
  84. const action = pendingActions.add( 'Action 1' );
  85. pendingActions.add( 'Action 2' );
  86. expect( pendingActions.first ).to.equal( action );
  87. } );
  88. } );
  89. describe( 'iterator', () => {
  90. it( 'should return all panding actions', () => {
  91. pendingActions.add( 'Action 1' );
  92. pendingActions.add( 'Action 2' );
  93. expect( Array.from( pendingActions, action => action.message ) ).to.have.members( [ 'Action 1', 'Action 2' ] );
  94. } );
  95. } );
  96. } );