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

Commit

Permalink
Change toolbar max-width test by removing timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
panr committed Feb 26, 2020
1 parent eaa5e31 commit d672f3f
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions tests/inlineeditoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import createRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot.
import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
import ResizeObserver from '@ckeditor/ckeditor5-utils/src/dom/resizeobserver';

const toPx = toUnit( 'px' );

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

/* globals setTimeout */
/* globals */

describe( 'InlineEditorUIView', () => {
let locale, view, editingView, editingViewRoot;
let resizeCallback;

testUtils.createSinonSandbox();

Expand All @@ -31,6 +33,20 @@ describe( 'InlineEditorUIView', () => {
editingViewRoot = createRoot( editingView.document );
view = new InlineEditorUIView( locale, editingView );
view.editable.name = editingViewRoot.rootName;

// Make sure other tests of the editor do not affect tests that follow.
// Without it, if an instance of ResizeObserver already exists somewhere undestroyed
// in DOM, the following DOM mock will have no effect.
ResizeObserver._observerInstance = null;

testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
resizeCallback = callback;

return {
observe: sinon.spy(),
unobserve: sinon.spy()
};
} );
} );

describe( 'constructor()', () => {
Expand Down Expand Up @@ -102,27 +118,25 @@ describe( 'InlineEditorUIView', () => {
expect( view.viewportTopOffset ).to.equal( 0 );
} );

// Sometimes this test can fail, due to the fact that it have to wait for async ResizeObserver execution.
it( 'max-width should be set to the width of the editable element', done => {
it( 'max-width should be set to the width of the editable element', () => {
const viewElement = view.editable.element;

// View element should be inside the body, otherwise the `Rect` instance will complain
// that it's not available in the DOM.
global.document.body.appendChild( viewElement );
expect( global.document.body.contains( viewElement ) ).to.be.true;

viewElement.style.width = '400px';

// Unfortunately we have to wait for async ResizeObserver execution.
// ResizeObserver which has been called after changing width of viewElement,
// needs 2x requestAnimationFrame or timeout to update a layout.
// See more: https://twitter.com/paul_irish/status/912693347315150849/photo/1
setTimeout( () => {
const editableWidthWithPadding = toPx( new Rect( viewElement ).width );
expect( view.toolbar.maxWidth ).to.be.equal( editableWidthWithPadding );
resizeCallback( [ {
target: viewElement,
contentRect: new Rect( viewElement )
} ] );

const editableWidthWithPadding = toPx( new Rect( viewElement ).width );

global.document.body.removeChild( viewElement );
expect( view.toolbar.maxWidth ).to.be.equal( editableWidthWithPadding );

done();
}, 500 );
viewElement.remove();
} );
} );

Expand Down

0 comments on commit d672f3f

Please sign in to comment.