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

Commit

Permalink
Removed timeouts from tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Aug 14, 2018
1 parent e334195 commit aa66f51
Showing 1 changed file with 25 additions and 30 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.
expect( spy.notCalled ).to.true;

// 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.calledOnce );
} );

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.notCalled );
} );

// Checks if preventDefault method was called by FakeSelectionObserver for specified key code.
Expand Down

0 comments on commit aa66f51

Please sign in to comment.