pendingactions.js 3.7 KB

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