8
0

pendingactions.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 isPending observable', () => {
  26. const spy = sinon.spy();
  27. pendingActions.on( 'change:isPending', spy );
  28. expect( pendingActions ).to.have.property( 'isPending', false );
  29. pendingActions.isPending = 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 isPending observable', () => {
  47. expect( pendingActions ).to.have.property( 'isPending', false );
  48. pendingActions.add( 'Action' );
  49. expect( pendingActions ).to.have.property( 'isPending', 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. } );
  57. describe( 'remove()', () => {
  58. it( 'should remove given pending action and update observable', () => {
  59. const action1 = pendingActions.add( 'Action 1' );
  60. const action2 = pendingActions.add( 'Action 2' );
  61. expect( pendingActions ).to.have.property( 'isPending', true );
  62. pendingActions.remove( action1 );
  63. expect( pendingActions ).to.have.property( 'isPending', true );
  64. pendingActions.remove( action2 );
  65. expect( pendingActions ).to.have.property( 'isPending', false );
  66. } );
  67. } );
  68. describe( 'iterator', () => {
  69. it( 'should return all panding actions', () => {
  70. pendingActions.add( 'Action 1' );
  71. pendingActions.add( 'Action 2' );
  72. expect( Array.from( pendingActions, action => action.message ) ).to.have.members( [ 'Action 1', 'Action 2' ] );
  73. } );
  74. } );
  75. } );