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

Commit

Permalink
Merge 19ab2df into e334195
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Aug 14, 2018
2 parents e334195 + 19ab2df commit ee4b320
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 70 deletions.
55 changes: 25 additions & 30 deletions tests/view/observer/fakeselectionobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

/* globals document, setTimeout */
/* globals document */

import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
import FakeSelectionObserver from '../../../src/view/observer/fakeselectionobserver';
Expand All @@ -12,9 +12,11 @@ import DomEventData from '../../../src/view/observer/domeventdata';
import createViewRoot from '../_utils/createroot';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import { setData, stringify } from '../../../src/dev-utils/view';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'FakeSelectionObserver', () => {
let observer, view, viewDocument, root, domRoot;
testUtils.createSinonSandbox();

before( () => {
domRoot = createElement( document, 'div', {
Expand Down Expand Up @@ -100,7 +102,8 @@ describe( 'FakeSelectionObserver', () => {
);
} );

it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
it( 'should fire `selectionChangeDone` event after selection stop changing', () => {
const clock = testUtils.sinon.useFakeTimers();
const spy = sinon.spy();

viewDocument.on( 'selectionChangeDone', spy );
Expand All @@ -109,25 +112,21 @@ describe( 'FakeSelectionObserver', () => {
changeFakeSelectionPressing( keyCodes.arrowdown );

// Wait 100ms.
// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
// See: https://github.com/lodash/lodash/issues/304
setTimeout( () => {
// Check if spy was called.
expect( spy.notCalled ).to.true;
clock.tick( 100 );

// Change selection one more time.
changeFakeSelectionPressing( keyCodes.arrowdown );
// Check if spy was called.
sinon.assert.notCalled( spy );

// Wait 210ms (debounced function should be called).
setTimeout( () => {
expect( spy.calledOnce ).to.true;
// Change selection one more time.
changeFakeSelectionPressing( keyCodes.arrowdown );

done();
}, 210 );
}, 100 );
// Wait 210ms (debounced function should be called).
clock.tick( 210 );
sinon.assert.calledOnce( spy );
} );

it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
it( 'should not fire `selectionChangeDone` event when observer will be destroyed', () => {
const clock = testUtils.sinon.useFakeTimers();
const spy = sinon.spy();

viewDocument.on( 'selectionChangeDone', spy );
Expand All @@ -136,20 +135,16 @@ describe( 'FakeSelectionObserver', () => {
changeFakeSelectionPressing( keyCodes.arrowdown );

// Wait 100ms.
// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
// See: https://github.com/lodash/lodash/issues/304
setTimeout( () => {
// And destroy observer.
observer.destroy();

// Wait another 110ms.
setTimeout( () => {
// Check that event won't be called.
expect( spy.notCalled ).to.true;

done();
}, 110 );
}, 100 );
clock.tick( 100 );

// And destroy observer.
observer.destroy();

// Wait another 110ms.
clock.tick( 110 );

// Check that event won't be called.
sinon.assert.notCalled( spy );
} );

// Checks if preventDefault method was called by FakeSelectionObserver for specified key code.
Expand Down
71 changes: 31 additions & 40 deletions tests/view/observer/selectionobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ describe( 'SelectionObserver', () => {
}
} );

it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
it( 'should fire `selectionChangeDone` event after selection stop changing', () => {
const spy = sinon.spy();
const clock = testUtils.sinon.useFakeTimers();

viewDocument.on( 'selectionChangeDone', spy );

Expand All @@ -250,64 +251,54 @@ describe( 'SelectionObserver', () => {
changeDomSelection();

// Wait 100ms.
// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
// See: https://github.com/lodash/lodash/issues/304
setTimeout( () => {
// Check if spy was called.
expect( spy.notCalled ).to.true;
clock.tick( 100 );
// Check if spy was called.
sinon.assert.notCalled( spy );

// Change selection one more time.
changeDomSelection();
// Change selection one more time.
changeDomSelection();

// Wait 210ms (debounced function should be called).
setTimeout( () => {
const data = spy.firstCall.args[ 1 ];
// Wait 210ms (debounced function should be called).
clock.tick( 210 );

expect( spy.calledOnce ).to.true;
expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
const data = spy.firstCall.args[ 1 ];

expect( data ).to.have.property( 'oldSelection' ).to.instanceof( DocumentSelection );
expect( data.oldSelection.rangeCount ).to.equal( 0 );
sinon.assert.calledOnce( spy );
expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );

expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
expect( data.newSelection.rangeCount ).to.equal( 1 );
expect( data ).to.have.property( 'oldSelection' ).to.instanceof( DocumentSelection );
expect( data.oldSelection.rangeCount ).to.equal( 0 );

const newViewRange = data.newSelection.getFirstRange();
const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
expect( data.newSelection.rangeCount ).to.equal( 1 );

expect( newViewRange.start.parent ).to.equal( viewFoo );
expect( newViewRange.start.offset ).to.equal( 3 );
expect( newViewRange.end.parent ).to.equal( viewFoo );
expect( newViewRange.end.offset ).to.equal( 3 );
const newViewRange = data.newSelection.getFirstRange();
const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );

done();
}, 210 );
}, 100 );
expect( newViewRange.start.parent ).to.equal( viewFoo );
expect( newViewRange.start.offset ).to.equal( 3 );
expect( newViewRange.end.parent ).to.equal( viewFoo );
expect( newViewRange.end.offset ).to.equal( 3 );
} );

it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
it( 'should not fire `selectionChangeDone` event when observer will be destroyed', () => {
const spy = sinon.spy();
const clock = testUtils.sinon.useFakeTimers();

viewDocument.on( 'selectionChangeDone', spy );

// Change selection.
changeDomSelection();

// Wait 100ms.
// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
// See: https://github.com/lodash/lodash/issues/304
setTimeout( () => {
// And destroy observer.
selectionObserver.destroy();

// Wait another 110ms.
setTimeout( () => {
// Check that event won't be called.
expect( spy.notCalled ).to.true;
clock.tick( 100 );
// And destroy observer.
selectionObserver.destroy();

done();
}, 110 );
}, 100 );
// Wait another 110ms.
clock.tick( 110 );
// Check that event won't be called.
sinon.assert.notCalled( spy );
} );

it( 'should re-render view if selections are similar if DOM selection is in incorrect place', done => {
Expand Down

0 comments on commit ee4b320

Please sign in to comment.