Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Add tests to TextWatcher covering #isEnabled observable
Browse files Browse the repository at this point in the history
  • Loading branch information
panr committed Jan 28, 2020
1 parent 267595a commit 30671da
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion tests/textwatcher.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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 );

Expand All @@ -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 );

Expand Down Expand Up @@ -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 );
} );
} );
} );

0 comments on commit 30671da

Please sign in to comment.