autosave.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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( { useFakeTimers: true } );
  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. sandbox.useFakeTimers();
  96. const pendingActions = editor.plugins.get( PendingActions );
  97. const serverActionSpy = sinon.spy();
  98. const serverActionStub = sinon.stub();
  99. serverActionStub.onCall( 0 ).resolves( wait( 500 ).then( serverActionSpy ) );
  100. autosave.provider = {
  101. save: serverActionStub
  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. sinon.assert.notCalled( serverActionSpy );
  108. expect( pendingActions.isPending ).to.be.true;
  109. expect( pendingActions.first.message ).to.equal( 'Saving in progress.' );
  110. sandbox.clock.tick( 500 );
  111. return Promise.resolve().then( () => Promise.resolve() ).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: serverActionSpy
  121. };
  122. expect( pendingActions.isPending ).to.be.false;
  123. editor.model.change( writer => {
  124. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  125. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  126. } );
  127. expect( pendingActions.isPending ).to.be.true;
  128. // Server action needs to wait at least a cycle.
  129. return Promise.resolve().then( () => {
  130. sinon.assert.calledOnce( serverActionSpy );
  131. expect( pendingActions.isPending ).to.be.false;
  132. } );
  133. } );
  134. it( 'should handle correctly throttled save action and preserve pending action until both save actions finish', () => {
  135. sandbox.useFakeTimers();
  136. const serverActionSpy = sinon.spy();
  137. const pendingActions = editor.plugins.get( PendingActions );
  138. // Create a fake server that responses after 500ms for the first call and after 1000ms for the second call.
  139. const serverActionStub = sinon.stub();
  140. serverActionStub.onCall( 0 ).resolves( wait( 500 ).then( serverActionSpy ) );
  141. serverActionStub.onCall( 1 ).resolves( wait( 1000 ).then( serverActionSpy ) );
  142. autosave.provider = {
  143. save: serverActionStub
  144. };
  145. expect( pendingActions.isPending ).to.be.false;
  146. editor.model.change( writer => {
  147. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  148. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  149. } );
  150. expect( pendingActions.isPending ).to.be.true;
  151. editor.model.change( writer => {
  152. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  153. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  154. } );
  155. autosave._flush();
  156. expect( pendingActions.isPending ).to.be.true;
  157. sandbox.clock.tick( 500 );
  158. return Promise.resolve().then( () => {
  159. expect( pendingActions.isPending ).to.be.true;
  160. sinon.assert.calledOnce( serverActionSpy );
  161. // Wait another 500ms for the second server action.
  162. sandbox.clock.tick( 500 );
  163. return Promise.resolve().then( () => {
  164. expect( pendingActions.isPending ).to.be.false;
  165. sinon.assert.calledTwice( serverActionSpy );
  166. } );
  167. } );
  168. } );
  169. it( 'should handle correctly throttled save action and preserve pending action until both save actions finish #2', () => {
  170. const serverActionSpy = sinon.spy();
  171. const pendingActions = editor.plugins.get( PendingActions );
  172. autosave.provider = {
  173. save: serverActionSpy
  174. };
  175. expect( pendingActions.isPending ).to.be.false;
  176. editor.model.change( writer => {
  177. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  178. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  179. } );
  180. expect( pendingActions.isPending ).to.be.true;
  181. editor.model.change( writer => {
  182. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  183. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  184. } );
  185. expect( pendingActions.isPending ).to.be.true;
  186. // Server action needs to wait at least a cycle.
  187. return Promise.resolve().then( () => {
  188. sinon.assert.calledOnce( serverActionSpy );
  189. expect( pendingActions.isPending ).to.be.true;
  190. autosave._flush();
  191. // Wait another promise cycle.
  192. return Promise.resolve().then( () => {
  193. sinon.assert.calledTwice( serverActionSpy );
  194. expect( pendingActions.isPending ).to.be.false;
  195. } );
  196. } );
  197. } );
  198. it( 'should filter out changes in the selection', () => {
  199. autosave.provider = {
  200. save: sandbox.spy()
  201. };
  202. editor.model.change( writer => {
  203. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  204. } );
  205. autosave._flush();
  206. sinon.assert.notCalled( autosave.provider.save );
  207. } );
  208. it( 'should filter out markers that does not affect the data model', () => {
  209. autosave.provider = {
  210. save: sandbox.spy()
  211. };
  212. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  213. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  214. editor.model.change( writer => {
  215. writer.addMarker( 'name', { usingOperation: true, range } );
  216. } );
  217. autosave._flush();
  218. editor.model.change( writer => {
  219. writer.updateMarker( 'name', { range: range2 } );
  220. } );
  221. autosave._flush();
  222. sinon.assert.notCalled( autosave.provider.save );
  223. } );
  224. it( 'should filter out markers that does not affect the data model #2', () => {
  225. autosave.provider = {
  226. save: sandbox.spy()
  227. };
  228. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  229. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  230. editor.model.change( writer => {
  231. writer.addMarker( 'name', { usingOperation: false, range } );
  232. } );
  233. autosave._flush();
  234. editor.model.change( writer => {
  235. writer.updateMarker( 'name', { range: range2 } );
  236. } );
  237. autosave._flush();
  238. sinon.assert.notCalled( autosave.provider.save );
  239. } );
  240. it( 'should call the save method when some marker affects the data model', () => {
  241. autosave.provider = {
  242. save: sandbox.spy()
  243. };
  244. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  245. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  246. editor.model.change( writer => {
  247. writer.addMarker( 'name', { usingOperation: true, affectsData: true, range } );
  248. } );
  249. autosave._flush();
  250. editor.model.change( writer => {
  251. writer.updateMarker( 'name', { range: range2 } );
  252. } );
  253. autosave._flush();
  254. sinon.assert.calledTwice( autosave.provider.save );
  255. } );
  256. it( 'should call the save method when some marker affects the data model #2', () => {
  257. autosave.provider = {
  258. save: sandbox.spy()
  259. };
  260. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  261. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  262. editor.model.change( writer => {
  263. writer.addMarker( 'name', { usingOperation: false, affectsData: true, range } );
  264. } );
  265. autosave._flush();
  266. sinon.assert.calledOnce( autosave.provider.save );
  267. editor.model.change( writer => {
  268. writer.updateMarker( 'name', { range: range2 } );
  269. } );
  270. autosave._flush();
  271. sinon.assert.calledTwice( autosave.provider.save );
  272. } );
  273. it( 'should call the save method when some marker affects the data model #3', () => {
  274. autosave.provider = {
  275. save: sandbox.spy()
  276. };
  277. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  278. editor.model.change( writer => {
  279. writer.addMarker( 'marker-not-affecting-data', { usingOperation: false, affectsData: true, range } );
  280. writer.addMarker( 'marker-affecting-data', { usingOperation: false, affectsData: false, range } );
  281. } );
  282. autosave._flush();
  283. sinon.assert.calledOnce( autosave.provider.save );
  284. } );
  285. it( 'should flush remaining calls after editor\'s destroy', () => {
  286. const spy = sandbox.spy();
  287. const savedStates = [];
  288. autosave.provider = {
  289. save() {
  290. spy();
  291. savedStates.push( editor.getData() );
  292. }
  293. };
  294. editor.model.change( writer => {
  295. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  296. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  297. } );
  298. editor.model.change( writer => {
  299. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  300. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  301. } );
  302. return editor.destroy().then( () => {
  303. expect( spy.callCount ).to.equal( 2 );
  304. expect( savedStates ).to.deep.equal( [
  305. '<p>foo</p><p>paragraph2</p>',
  306. '<p>foo</p><p>bar</p>',
  307. ] );
  308. } );
  309. } );
  310. it( 'should work after editor\'s destroy with long server\'s action time', () => {
  311. sandbox.useFakeTimers();
  312. const pendingActions = editor.plugins.get( PendingActions );
  313. const serverActionSpy = sinon.spy();
  314. const serverActionStub = sinon.stub();
  315. serverActionStub.onCall( 0 ).resolves( wait( 500 ).then( serverActionSpy ) );
  316. autosave.provider = {
  317. save: serverActionStub
  318. };
  319. editor.model.change( writer => {
  320. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  321. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  322. } );
  323. return editor.destroy()
  324. .then( () => {
  325. expect( pendingActions.isPending ).to.be.true;
  326. sandbox.clock.tick( 500 );
  327. } )
  328. .then( () => {
  329. expect( pendingActions.isPending ).to.be.false;
  330. sinon.assert.calledOnce( serverActionSpy );
  331. } );
  332. } );
  333. } );
  334. function wait( time ) {
  335. return new Promise( res => {
  336. window.setTimeout( res, time );
  337. } );
  338. }
  339. } );