autosave.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 changes in the selection', () => {
  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( 'should filter out markers that does not affect the data model', () => {
  147. autosave.provider = {
  148. save: sandbox.spy()
  149. };
  150. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  151. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  152. editor.model.change( writer => {
  153. writer.addMarker( 'name', { usingOperation: true, range } );
  154. } );
  155. autosave._flush();
  156. editor.model.change( writer => {
  157. writer.updateMarker( 'name', { range: range2 } );
  158. } );
  159. autosave._flush();
  160. sinon.assert.notCalled( autosave.provider.save );
  161. } );
  162. it( 'should filter out markers that does not affect the data model #2', () => {
  163. autosave.provider = {
  164. save: sandbox.spy()
  165. };
  166. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  167. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  168. editor.model.change( writer => {
  169. writer.addMarker( 'name', { usingOperation: false, range } );
  170. } );
  171. autosave._flush();
  172. editor.model.change( writer => {
  173. writer.updateMarker( 'name', { range: range2 } );
  174. } );
  175. autosave._flush();
  176. sinon.assert.notCalled( autosave.provider.save );
  177. } );
  178. it( 'should call the save method when some marker affects the data model', () => {
  179. autosave.provider = {
  180. save: sandbox.spy()
  181. };
  182. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  183. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  184. editor.model.change( writer => {
  185. writer.addMarker( 'name', { usingOperation: true, affectsData: true, range } );
  186. } );
  187. autosave._flush();
  188. editor.model.change( writer => {
  189. writer.updateMarker( 'name', { range: range2 } );
  190. } );
  191. autosave._flush();
  192. sinon.assert.calledTwice( autosave.provider.save );
  193. } );
  194. it( 'should call the save method when some marker affects the data model #2', () => {
  195. autosave.provider = {
  196. save: sandbox.spy()
  197. };
  198. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  199. const range2 = ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) );
  200. editor.model.change( writer => {
  201. writer.addMarker( 'name', { usingOperation: false, affectsData: true, range } );
  202. } );
  203. autosave._flush();
  204. sinon.assert.calledOnce( autosave.provider.save );
  205. editor.model.change( writer => {
  206. writer.updateMarker( 'name', { range: range2 } );
  207. } );
  208. autosave._flush();
  209. sinon.assert.calledTwice( autosave.provider.save );
  210. } );
  211. it( 'should call the save method when some marker affects the data model #3', () => {
  212. autosave.provider = {
  213. save: sandbox.spy()
  214. };
  215. const range = ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) );
  216. editor.model.change( writer => {
  217. writer.addMarker( 'marker-not-affecting-data', { usingOperation: false, affectsData: true, range } );
  218. writer.addMarker( 'marker-affecting-data', { usingOperation: false, affectsData: false, range } );
  219. } );
  220. autosave._flush();
  221. sinon.assert.calledOnce( autosave.provider.save );
  222. } );
  223. it( 'should flush remaining calls after editor\'s destroy', () => {
  224. const spy = sandbox.spy();
  225. const savedStates = [];
  226. autosave.provider = {
  227. save() {
  228. spy();
  229. savedStates.push( editor.getData() );
  230. }
  231. };
  232. editor.model.change( writer => {
  233. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  234. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  235. } );
  236. editor.model.change( writer => {
  237. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
  238. editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
  239. } );
  240. return editor.destroy().then( () => {
  241. expect( spy.callCount ).to.equal( 2 );
  242. expect( savedStates ).to.deep.equal( [
  243. '<p>foo</p><p>paragraph2</p>',
  244. '<p>foo</p><p>bar</p>',
  245. ] );
  246. } );
  247. } );
  248. it( 'should work after editor\'s destroy with long server\'s action time', () => {
  249. const serverActionSpy = sinon.spy();
  250. const pendingActions = editor.plugins.get( PendingActions );
  251. autosave.provider = {
  252. save() {
  253. return wait5ms()
  254. .then( serverActionSpy );
  255. }
  256. };
  257. editor.model.change( writer => {
  258. writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
  259. editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
  260. } );
  261. return editor.destroy()
  262. .then( () => {
  263. expect( pendingActions.isPending ).to.be.true;
  264. } )
  265. .then( wait5ms )
  266. .then( () => {
  267. expect( pendingActions.isPending ).to.be.false;
  268. } );
  269. } );
  270. } );
  271. function wait5ms() {
  272. return new Promise( res => {
  273. window.setTimeout( res, 5 );
  274. } );
  275. }
  276. } );