autosave.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. /* globals document, window */
  5. import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
  6. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import Autosave from '../src/autosave';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import PendingActions from '@ckeditor/ckeditor5-core/src/pendingactions';
  11. describe( 'Autosave', () => {
  12. const sandbox = sinon.sandbox.create();
  13. let editor, element, autosave;
  14. beforeEach( () => {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. return ClassicTestEditor
  18. .create( element, {
  19. plugins: [ Autosave, Paragraph ]
  20. } )
  21. .then( _editor => {
  22. const data = '<p>paragraph1</p><p>paragraph2</p>';
  23. editor = _editor;
  24. editor.setData( data );
  25. autosave = editor.plugins.get( Autosave );
  26. // Clean autosave's state after setting data.
  27. autosave._throttledSave.cancel();
  28. } );
  29. } );
  30. afterEach( () => {
  31. document.body.removeChild( element );
  32. sandbox.restore();
  33. return editor.destroy();
  34. } );
  35. it( 'should have static pluginName property', () => {
  36. expect( Autosave.pluginName ).to.equal( 'Autosave' );
  37. } );
  38. describe( 'initialization', () => {
  39. it( 'should initialize provider with an undefined value', () => {
  40. expect( autosave.provider ).to.be.undefined;
  41. } );
  42. it( 'should allow plugin to work without any defined provider', () => {
  43. editor.model.change( writer => {
  44. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  45. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  46. } );
  47. autosave._flush();
  48. } );
  49. } );
  50. describe( 'autosaving', () => {
  51. it( 'should run provider\'s save method when the editor\'s change event is fired', () => {
  52. autosave.provider = {
  53. save: sinon.spy()
  54. };
  55. editor.model.change( writer => {
  56. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  57. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  58. } );
  59. // Go to the next cycle to because synchronization of CS documentVersion is async.
  60. autosave._flush();
  61. sinon.assert.calledOnce( autosave.provider.save );
  62. } );
  63. it( 'should throttle editor\'s change event', () => {
  64. const spy = sinon.spy();
  65. const savedStates = [];
  66. autosave.provider = {
  67. save() {
  68. spy();
  69. savedStates.push( editor.getData() );
  70. }
  71. };
  72. // Leading (will fire change).
  73. editor.model.change( writer => {
  74. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  75. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  76. } );
  77. // Throttled (won't fire change).
  78. editor.model.change( writer => {
  79. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  80. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  81. } );
  82. // Flushed (will fire change).
  83. editor.model.change( writer => {
  84. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  85. editor.model.insertContent( new ModelText( 'biz' ), editor.model.document.selection );
  86. } );
  87. autosave._flush();
  88. expect( spy.callCount ).to.equal( 2 );
  89. expect( savedStates ).to.deep.equal( [
  90. '<p>paragraph1</p><p>foo</p>',
  91. '<p>paragraph1</p><p>biz</p>'
  92. ] );
  93. } );
  94. it( 'should add a pending action during the saving.', () => {
  95. const serverActionSpy = sinon.spy();
  96. const pendingActions = editor.plugins.get( PendingActions );
  97. autosave.provider = {
  98. save() {
  99. return wait5ms()
  100. .then( serverActionSpy );
  101. }
  102. };
  103. editor.model.change( writer => {
  104. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  105. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  106. } );
  107. autosave._flush();
  108. sinon.assert.notCalled( serverActionSpy );
  109. expect( pendingActions.isPending ).to.be.true;
  110. expect( pendingActions.first.message ).to.equal( 'Saving in progress.' );
  111. return wait5ms().then( () => {
  112. sinon.assert.calledOnce( serverActionSpy );
  113. expect( pendingActions.isPending ).to.be.false;
  114. } );
  115. } );
  116. it( 'should flush remaining calls after editor\'s destroy', () => {
  117. const spy = sandbox.spy();
  118. const savedStates = [];
  119. autosave.provider = {
  120. save() {
  121. spy();
  122. savedStates.push( editor.getData() );
  123. }
  124. };
  125. editor.model.change( writer => {
  126. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  127. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  128. } );
  129. editor.model.change( writer => {
  130. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  131. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  132. } );
  133. return editor.destroy().then( () => {
  134. expect( spy.callCount ).to.equal( 2 );
  135. expect( savedStates ).to.deep.equal( [
  136. '<p>foo</p><p>paragraph2</p>',
  137. '<p>foo</p><p>bar</p>',
  138. ] );
  139. } );
  140. } );
  141. it( 'should work after editor\'s destroy with long server action time', () => {
  142. const serverActionSpy = sinon.spy();
  143. const pendingActions = editor.plugins.get( PendingActions );
  144. autosave.provider = {
  145. save() {
  146. return wait5ms()
  147. .then( serverActionSpy );
  148. }
  149. };
  150. editor.model.change( writer => {
  151. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  152. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  153. } );
  154. return editor.destroy()
  155. .then( () => {
  156. expect( pendingActions.isPending ).to.be.true;
  157. } )
  158. .then( wait5ms )
  159. .then( () => {
  160. expect( pendingActions.isPending ).to.be.false;
  161. } );
  162. } );
  163. } );
  164. function wait5ms() {
  165. return new Promise( res => {
  166. window.setTimeout( res, 5 );
  167. } );
  168. }
  169. } );