Skip to content

Commit

Permalink
Add promise based dialog tests and simple reworked unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Samsel committed Sep 27, 2019
1 parent 85b4783 commit 17594bd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 28 deletions.
23 changes: 23 additions & 0 deletions tests/_benderjs/ckeditor/static/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@
tc.wait();
},

asyncDialog: function( dialogName ) {
var editor = this.editor;

return new CKEDITOR.tools.promise( function( resolve, reject ) {

editor.on( 'dialogShow', function( event ) {
var dialog = event.data;

event.removeListener();

CKEDITOR.tools.setTimeout( function() {
resolve( dialog );
}, 0 );
} );

CKEDITOR.tools.setTimeout( function() {
reject();
}, 5000 );

editor.execCommand( dialogName );
} );
},

getData: function( fixHtml, compatHtml ) {
var data = this.editor.getData();

Expand Down
97 changes: 69 additions & 28 deletions tests/plugins/dialog/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
( function() {
'use strict';

var currentFocusCallback;
var dialogItemFocusListener = function( evt ) {
if ( currentFocusCallback ) {
currentFocusCallback( evt.sender );
}
};
var hasRejects = false; // true;

var singlePageDialogDefinition = function() {
return {
Expand Down Expand Up @@ -42,7 +37,9 @@

// attach focus listener in dialog;
CKEDITOR.tools.array.forEach( dialog._.focusList, function( item ) {
item.on( 'focus', dialogItemFocusListener, null, null, 100000 );
item.on( 'focus', function() {
dialog.fire( 'focus:change' );
}, null, null, 100000 );
} );

}
Expand All @@ -53,16 +50,78 @@
function assertFocus( dialog, element ) {
var currentlyFocusedElement = dialog._.focusList[ dialog._.currentFocusIndex ];
assert.areEqual( element, currentlyFocusedElement,
'Element: "' + element.id + '" should be equal to currently focused element: "' + currentlyFocusedElement.id + '".' );
'Element: "' + element.id + '" should be equal to currently focused element: "' + currentlyFocusedElement.id + '".' );
}

function focusNext( dialog ) {
dialog.changeFocus( 1 );
return new CKEDITOR.tools.promise( function( resolve, reject ) {
dialog.once( 'focus:change', function() {
// short moment for focus stabilization;
CKEDITOR.tools.setTimeout( function() {
resolve( dialog );
}, 50 );
} );

if ( hasRejects ) {
CKEDITOR.tools.setTimeout( reject, 5000 );
}

dialog.changeFocus( 1 );
} );
}

function focusPrevious( dialog ) {
return new CKEDITOR.tools.promise( function( resolve, reject ) {
dialog.once( 'focus:change', function() {
// short moment for focus stabilization;
CKEDITOR.tools.setTimeout( function() {
resolve( dialog );
}, 50 );

} );

if ( hasRejects ) {
CKEDITOR.tools.setTimeout( reject, 5000 );
}

dialog.changeFocus( -1 );
} );
}


bender.editor = true;

var tests = {
'test single page async': function() {
var bot = this.editorBot;

return bot.asyncDialog( 'singlePageDialog' )
.then( function( dialog ) {
assertFocus( dialog, dialog.getContentElement( 'test1', 'sp-input1' ) );
return dialog;
} )
.then( focusNext )
.then( function( dialog ) {
assertFocus( dialog, dialog.getContentElement( 'test1', 'sp-input2' ) );
return dialog;
} )
.then( focusNext )
.then( focusNext )
.then( function( dialog ) {
assertFocus( dialog, dialog.getButton( 'cancel' ) );
return dialog;
} )
.then( focusPrevious )
.then( function( dialog ) {
assertFocus( dialog, dialog.getContentElement( 'test1', 'sp-input3' ) );
return dialog;
} );
}
};

tests = bender.tools.createAsyncTests( tests );

CKEDITOR.tools.extend( tests, {
init: function() {
CKEDITOR.dialog.add( 'singlePageDialogDefinition', singlePageDialogDefinition );

Expand All @@ -72,29 +131,11 @@
tearDown: function() {
var dialog;

currentFocusCallback = null;
while ( ( dialog = CKEDITOR.dialog.getCurrent() ) ) {
dialog.hide();
}
},

'test single page test': function() {
var bot = this.editorBot;

bot.dialog( 'singlePageDialog', function( dialog ) {
currentFocusCallback = function() {
resume( function() {
assertFocus( dialog, dialog.getContentElement( 'test1', 'sp-input2' ) );
} );
};

assertFocus( dialog, dialog.getContentElement( 'test1', 'sp-input1' ) );

focusNext( dialog );
wait();
} );
}
};
} );

bender.test( tests );
} )();

0 comments on commit 17594bd

Please sign in to comment.