8
0
Maciej Bukowski 7 лет назад
Родитель
Сommit
2339010cd8
1 измененных файлов с 106 добавлено и 4 удалено
  1. 106 4
      packages/ckeditor5-autosave/tests/autosave.js

+ 106 - 4
packages/ckeditor5-autosave/tests/autosave.js

@@ -75,7 +75,7 @@ describe( 'Autosave', () => {
 		} );
 	} );
 
-	describe( 'config', () => {
+	describe( 'config.autosave.save', () => {
 		beforeEach( () => {
 			element = document.createElement( 'div' );
 			document.body.appendChild( element );
@@ -119,7 +119,7 @@ describe( 'Autosave', () => {
 			} );
 		} );
 
-		it( 'its callback and adapter callback should be called if both are provided', () => {
+		it( 'config callback and adapter callback should be called if both are provided', () => {
 			editor.config.get( 'autosave' ).save.resetHistory();
 
 			autosave.adapter = {
@@ -160,6 +160,45 @@ describe( 'Autosave', () => {
 		} );
 	} );
 
+	describe( 'config.autosave.waitingTime', () => {
+		afterEach( () => {
+			document.body.removeChild( element );
+
+			return editor.destroy();
+		} );
+
+		it( 'should specify the time of waiting on the next use action before saving', () => {
+			element = document.createElement( 'div' );
+			document.body.appendChild( element );
+
+			return ClassicTestEditor
+				.create( element, {
+					plugins: [ Autosave, Paragraph ],
+					autosave: {
+						save: sinon.spy(),
+						waitingTime: 500
+					}
+				} )
+				.then( _editor => {
+					editor = _editor;
+
+					const data = '<p>paragraph1</p><p>paragraph2</p>';
+					editor.setData( data );
+
+					editor.model.change( writer => {
+						writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+						editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
+					} );
+
+					sandbox.clock.tick( 500 );
+
+					return Promise.resolve().then( () => {
+						sinon.assert.calledOnce( editor.config.get( 'autosave' ).save );
+					} );
+				} );
+		} );
+	} );
+
 	describe( 'autosaving', () => {
 		beforeEach( () => {
 			element = document.createElement( 'div' );
@@ -232,10 +271,12 @@ describe( 'Autosave', () => {
 				editor.model.insertContent( new ModelText( 'biz' ), editor.model.document.selection );
 			} );
 
+			sinon.assert.notCalled( spy );
+
 			sandbox.clock.tick( 2000 );
 
 			return Promise.resolve().then( () => {
-				expect( spy.callCount ).to.equal( 1 );
+				sinon.assert.calledOnce( spy );
 				expect( savedStates ).to.deep.equal( [
 					'<p>paragraph1</p><p>biz</p>'
 				] );
@@ -277,6 +318,65 @@ describe( 'Autosave', () => {
 			} );
 		} );
 
+		// Integration test.
+		it( 'should add a pending action during the saving #2.', () => {
+			const pendingActions = editor.plugins.get( PendingActions );
+			const serverActionSpy = sinon.spy();
+			const serverActionStub = sinon.stub();
+			serverActionStub.callsFake( () => wait( 1000 ).then( serverActionSpy ) );
+
+			autosave.adapter = {
+				save: serverActionStub
+			};
+
+			editor.model.change( writer => {
+				writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+				editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
+			} );
+
+			sinon.assert.notCalled( serverActionSpy );
+			expect( pendingActions.hasAny ).to.be.true;
+			expect( pendingActions.first.message ).to.equal( 'Saving changes' );
+
+			sandbox.clock.tick( 2000 );
+			expect( autosave.state ).to.equal( 'saving' );
+
+			return Promise.resolve().then( () => {
+				// Add new change before the response from the server.
+
+				editor.model.change( writer => {
+					writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+					editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
+				} );
+
+				sandbox.clock.tick( 1000 );
+
+				return runPromiseCycles();
+			} ).then( () => {
+				// Now there should come the first server response.
+				expect( autosave.state ).to.equal( 'waiting' );
+				expect( pendingActions.hasAny ).to.be.true;
+				sinon.assert.calledOnce( serverActionSpy );
+
+				sandbox.clock.tick( 2000 );
+
+				return runPromiseCycles();
+			} ).then( () => {
+				expect( autosave.state ).to.equal( 'saving' );
+				expect( pendingActions.hasAny ).to.be.true;
+				sinon.assert.calledOnce( serverActionSpy );
+
+				// Wait for the second server response.
+				sandbox.clock.tick( 1000 );
+
+				return runPromiseCycles();
+			} ).then( () => {
+				expect( pendingActions.hasAny ).to.be.false;
+				expect( autosave.state ).to.equal( 'synchronized' );
+				sinon.assert.calledTwice( serverActionSpy );
+			} );
+		} );
+
 		it( 'should add a pending action during the saving #2.', () => {
 			const serverActionSpy = sinon.spy();
 			const pendingActions = editor.plugins.get( PendingActions );
@@ -456,8 +556,10 @@ describe( 'Autosave', () => {
 				editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
 			} );
 
+			sinon.assert.notCalled( spy );
+
 			return editor.destroy().then( () => {
-				expect( spy.callCount ).to.equal( 1 );
+				sinon.assert.calledOnce( spy );
 				expect( savedStates ).to.deep.equal( [
 					'<p>foo</p><p>bar</p>',
 				] );