autosave.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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._flush();
  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 add a pending action during the saving #2.', () => {
  117. const serverActionSpy = sinon.spy();
  118. const pendingActions = editor.plugins.get( PendingActions );
  119. autosave.provider = {
  120. save() {
  121. serverActionSpy();
  122. }
  123. };
  124. expect( pendingActions.isPending ).to.be.false;
  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. expect( pendingActions.isPending ).to.be.true;
  130. autosave._flush();
  131. // Server action needs to wait at least a cycle.
  132. return Promise.resolve().then( () => {
  133. expect( pendingActions.isPending ).to.be.false;
  134. } );
  135. } );
  136. it( 'should filter out change batches that don\'t change content', () => {
  137. autosave.provider = {
  138. save: sandbox.spy()
  139. };
  140. editor.model.change( writer => {
  141. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  142. } );
  143. autosave._flush();
  144. sinon.assert.notCalled( autosave.provider.save );
  145. } );
  146. it.skip( 'should filter out change batches that don\'t change content #2', () => {
  147. autosave.provider = {
  148. save: sandbox.spy()
  149. };
  150. const operation = { name: 'user:position' };
  151. editor.model.document.fire( 'change', {
  152. deltas: [ { operations: [ operation ] } ]
  153. } );
  154. autosave._flush();
  155. sinon.assert.notCalled( autosave.provider.save );
  156. } );
  157. it( 'should flush remaining calls after editor\'s destroy', () => {
  158. const spy = sandbox.spy();
  159. const savedStates = [];
  160. autosave.provider = {
  161. save() {
  162. spy();
  163. savedStates.push( editor.getData() );
  164. }
  165. };
  166. editor.model.change( writer => {
  167. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  168. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  169. } );
  170. editor.model.change( writer => {
  171. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  172. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  173. } );
  174. return editor.destroy().then( () => {
  175. expect( spy.callCount ).to.equal( 2 );
  176. expect( savedStates ).to.deep.equal( [
  177. '<p>foo</p><p>paragraph2</p>',
  178. '<p>foo</p><p>bar</p>',
  179. ] );
  180. } );
  181. } );
  182. it( 'should work after editor\'s destroy with long server\'s action time', () => {
  183. const serverActionSpy = sinon.spy();
  184. const pendingActions = editor.plugins.get( PendingActions );
  185. autosave.provider = {
  186. save() {
  187. return wait5ms()
  188. .then( serverActionSpy );
  189. }
  190. };
  191. editor.model.change( writer => {
  192. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  193. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  194. } );
  195. return editor.destroy()
  196. .then( () => {
  197. expect( pendingActions.isPending ).to.be.true;
  198. } )
  199. .then( wait5ms )
  200. .then( () => {
  201. expect( pendingActions.isPending ).to.be.false;
  202. } );
  203. } );
  204. } );
  205. function wait5ms() {
  206. return new Promise( res => {
  207. window.setTimeout( res, 5 );
  208. } );
  209. }
  210. } );