8
0

autosave.js 14 KB

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