浏览代码

Add tests to TextWatcher covering #isEnabled observable

panr 5 年之前
父节点
当前提交
451d838523
共有 1 个文件被更改,包括 104 次插入1 次删除
  1. 104 1
      packages/ckeditor5-typing/tests/textwatcher.js

+ 104 - 1
packages/ckeditor5-typing/tests/textwatcher.js

@@ -40,12 +40,25 @@ describe( 'TextWatcher', () => {
 
 	afterEach( () => {
 		sinon.restore();
+		watcher.off();
 
 		if ( editor ) {
 			return editor.destroy();
 		}
 	} );
 
+	describe( '#isEnabled', () => {
+		it( 'should be enabled after initialization', () => {
+			expect( watcher.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be disabled after setting #isEnabled to false', () => {
+			watcher.isEnabled = false;
+
+			expect( watcher.isEnabled ).to.be.false;
+		} );
+	} );
+
 	describe( 'testCallback', () => {
 		it( 'should evaluate text before caret for data changes', () => {
 			model.change( writer => {
@@ -97,6 +110,35 @@ describe( 'TextWatcher', () => {
 
 			sinon.assert.notCalled( testCallbackStub );
 		} );
+
+		it( 'should not evaluate text when watcher is disabled', () => {
+			watcher.isEnabled = false;
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+		} );
+
+		it( 'should evaluate text when watcher is enabled again', () => {
+			watcher.isEnabled = false;
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+
+			watcher.isEnabled = true;
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.calledOnce( testCallbackStub );
+			sinon.assert.calledWithExactly( testCallbackStub, 'foo @@' );
+		} );
 	} );
 
 	describe( 'events', () => {
@@ -113,6 +155,22 @@ describe( 'TextWatcher', () => {
 			sinon.assert.notCalled( unmatchedSpy );
 		} );
 
+		it( 'should not fire "matched:data" event when watcher is disabled' +
+		' (even when test callback returns true for model data changes)', () => {
+			watcher.isEnabled = false;
+
+			testCallbackStub.returns( true );
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+			sinon.assert.notCalled( matchedDataSpy );
+			sinon.assert.notCalled( matchedSelectionSpy );
+			sinon.assert.notCalled( unmatchedSpy );
+		} );
+
 		it( 'should fire "matched:selection" event when test callback returns true for model data changes', () => {
 			testCallbackStub.returns( true );
 
@@ -130,6 +188,26 @@ describe( 'TextWatcher', () => {
 			sinon.assert.notCalled( unmatchedSpy );
 		} );
 
+		it( 'should not fire "matched:selection" event when when watcher is disabled' +
+		' (even when test callback returns true for model data changes)', () => {
+			watcher.isEnabled = false;
+
+			testCallbackStub.returns( true );
+
+			model.enqueueChange( 'transparent', writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			model.change( writer => {
+				writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+			sinon.assert.notCalled( matchedDataSpy );
+			sinon.assert.notCalled( matchedSelectionSpy );
+			sinon.assert.notCalled( unmatchedSpy );
+		} );
+
 		it( 'should not fire "matched" event when test callback returns false', () => {
 			testCallbackStub.returns( false );
 
@@ -188,6 +266,31 @@ describe( 'TextWatcher', () => {
 			sinon.assert.notCalled( matchedSelectionSpy );
 			sinon.assert.calledOnce( unmatchedSpy );
 		} );
+
+		it( 'should not fire "umatched" event when selection is expanded if watcher is disabled', () => {
+			watcher.isEnabled = false;
+
+			testCallbackStub.returns( true );
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+			sinon.assert.notCalled( matchedDataSpy );
+			sinon.assert.notCalled( matchedSelectionSpy );
+			sinon.assert.notCalled( unmatchedSpy );
+
+			model.change( writer => {
+				const start = writer.createPositionAt( doc.getRoot().getChild( 0 ), 0 );
+
+				writer.setSelection( writer.createRange( start, start.getShiftedBy( 1 ) ) );
+			} );
+
+			sinon.assert.notCalled( testCallbackStub );
+			sinon.assert.notCalled( matchedDataSpy );
+			sinon.assert.notCalled( matchedSelectionSpy );
+			sinon.assert.notCalled( unmatchedSpy );
+		} );
 	} );
 } );
-