Skip to content

Commit

Permalink
Introduced bender.amd.require() - a tool to easily require CKEditor m…
Browse files Browse the repository at this point in the history
…odules in tests.
  • Loading branch information
fredck committed Jan 22, 2015
1 parent 758b958 commit 26f6c7c
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion dev/bender/plugins/ckeditor5/static/extensions.js
Expand Up @@ -3,14 +3,88 @@
* For licensing, see LICENSE.md.
*/

/* globals bender, CKEDITOR */
/* globals bender, CKEDITOR, describe: true */

'use strict';

// ### General extensions.
( function() {
// Make Bender wait to start running tests.
var done = bender.defer();

// Wait for the "ckeditor" module to be ready to start testing.
CKEDITOR.require( [ 'ckeditor' ], done );
} )();

// ### AMD related extensions.
( function() {
var wasRequireCalled,
requires;

/**
* AMD tools related to CKEditor.
*/
bender.amd = {
/**
* Specifies the list of CKEditor modules to be loaded before tests start. The modules will be passed to the
* describe() functions as parameters.
*
* @params {...String} module The name of the module to load.
*/
require: function() {
if ( wasRequireCalled ) {
throw 'bender.amd.require() must be called just once.';
}

wasRequireCalled = true;

var done = bender.defer();

var modules = [].slice.call( arguments );

CKEDITOR.require( modules, function() {
// Save all returned modules.
requires = [].slice.call( arguments );

// Call all describe()s that where waiting for `requires` to load.
flushDescribeQueue();

// Finally give green light for tests to start.
done();
} );
}
};

var originalDescribe = describe;
var describeQueue = [];

// Override the original Mocha's describe() so we can pass required modules to it.
describe = function( title, fn ) {
if ( wasRequireCalled ) {
// Queue the call.
describeQueue.push( [ this, title, fn ] );

// Then eventually call it for real, if `requires` have beel loaded already.
flushDescribeQueue();
} else {
originalDescribe.apply( this, arguments );
}
};

// Call all defined describe()s if `requires` have been loaded already.
function flushDescribeQueue() {
if ( requires ) {
describeQueue = describeQueue.filter( function( describeEntry ) {
var scope = describeEntry[ 0 ];
var title = describeEntry[ 1 ];
var fn = describeEntry[ 2 ];

originalDescribe.call( scope, title, function() {
fn.apply( this, requires );
} );

return false;
} );
}
}
} )();

0 comments on commit 26f6c7c

Please sign in to comment.