Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T/204 provide custom Quail path. #218

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 56 additions & 27 deletions quailInclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,65 @@
* actually load it async at runtime, it would keep the file smaller).
*/

var acNamespace = CKEDITOR.plugins.a11ychecker,
Engine = acNamespace.Engine,
IssueList = acNamespace.IssueList,
Issue = acNamespace.Issue,
IssueDetails = acNamespace.IssueDetails,
Quail,
EngineQuailConfig,
$ = window.jQuery || window.$;

// EngineQuailConfig class can still be loaded with RequireJS as it does not have any deps.
require( [ 'EngineQuailConfig' ], function( _EngineQuailConfig ) {
EngineQuailConfig = _EngineQuailConfig;
} );
/* @exclude */
define( [
'window',
'callback',
'EngineQuail',
'editor'
], function( window, callback, EngineQuail, editor ) {
/* @endexclude */
function quailInclude() {
var acNamespace = CKEDITOR.plugins.a11ychecker,
Engine = acNamespace.Engine,
IssueList = acNamespace.IssueList,
Issue = acNamespace.Issue,
IssueDetails = acNamespace.IssueDetails,
Quail,
EngineQuailConfig,
$ = window.jQuery || window.$;

(function() {
if ( !$ || !$.fn ) {
throw new Error( 'Missing jQuery. Accessibility Checker\'s default engine, Quail.js requires jQuery ' +
'to work correctly.' );
}
// EngineQuailConfig class can still be loaded with RequireJS as it does not have any deps.
require( [ 'EngineQuailConfig' ], function( _EngineQuailConfig ) {
EngineQuailConfig = _EngineQuailConfig;
} );

// We'll load custom Quail only if it's not already registered.
if ( $.fn.quail ) {
return;
}
/*@include libs/quail/quail.jquery.min.js */
}());

Quail = $.fn.quail;
if ( !$ || !$.fn ) {
throw new Error( 'Missing jQuery. Accessibility Checker\'s default engine, Quail.js requires jQuery ' +
'to work correctly.' );
}

// We'll load custom Quail only if it's not already registered.
if ( $.fn.quail ) {
includeEngineQuailAndContinue();
return;
}

var quailPath = editor.config.a11ychecker_quailPath || 'plugins/a11ychecker/libs/quail/quail.jquery.min.js';

CKEDITOR.scriptLoader.load( [ quailPath ], function( completed ) {

/*@include ../../EngineQuail.js */
if ( completed.length ) {
includeEngineQuailAndContinue();
} else {
throw new Error( 'Could not load Quail' );
}
} );

function includeEngineQuailAndContinue() {
Quail = $.fn.quail;

/*@include ../../EngineQuail.js */

callback( EngineQuail );
}
}

quailInclude();
/* @exclude */
return quailInclude;
} );
/* @endexclude */

callback( EngineQuail );
/* jshint ignore:end */
120 changes: 120 additions & 0 deletions tests/quailinclude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* bender-tags: unit,1.0.1,204 */

( function() {
'use strict';

bender.editors = {
pathDefined: {
config: {
language: 'en',
a11ychecker_quailPath: 'test/path'
}
},
pathNotDefined: {
config: {
language: 'en'
}
}
};

CKEDITOR.plugins.a11ychecker = {
Engine: {}
};

define( 'window', [], function() {
return {
jQuery: {
fn: {}
}
};
} );

define( 'EngineQuail', [], function() {} );

define( 'callback', [], function() {
return function() {
resume( function() {
assert.isTrue( true );
} );
};
} );


function mockLoad( success, onEnd ) {
CKEDITOR.scriptLoader.load = function( path, callback ) {
try {
callback( success ? [ path ] : [] );
} catch ( e ) {
onEnd( e );
return;
}
onEnd( null );
};
}

bender.test( {
'test loading Quail from the path defined in the config': function() {
var editor = this.editors.pathDefined;
define( 'editor', [], function() {
return editor;
} );


var load = CKEDITOR.scriptLoader.load;
CKEDITOR.scriptLoader.load = sinon.spy();

require( [ 'quailInclude' ], function( quailInclude ) {
resume( function() {
assert.isTrue( CKEDITOR.scriptLoader.load.calledWith( [ 'test/path' ] ) );

// Cleanup after the test.
CKEDITOR.scriptLoader.load = load;
requirejs.undef( 'quailInclude' );
requirejs.undef( 'editor' );
} );
} );

wait();
},
'test loading Quail from the default path': function() {
var editor = this.editors.pathNotDefined;
define( 'editor', [], function() {
return editor;
} );

var load = CKEDITOR.scriptLoader.load;
CKEDITOR.scriptLoader.load = sinon.spy();

require( [ 'quailInclude' ], function( quailInclude ) {
resume( function() {
assert.isTrue( CKEDITOR.scriptLoader.load.calledWith( [ 'plugins/a11ychecker/libs/quail/quail.jquery.min.js' ] ) );

CKEDITOR.scriptLoader.load = load;
requirejs.undef( 'quailInclude' );
requirejs.undef( 'editor' );

} );
} );

wait();
},
'test wrong Quail path throws exception': function() {
var editor = this.editors.pathNotDefined;
define( 'editor', [], function() {
return editor;
} );

mockLoad( false, function( error ) {
resume( function() {
assert.isObject( error );
assert.areEqual( 'Could not load Quail', error.message );
} );
} );

require( [ 'quailInclude' ], function( quailInclude ) {} );

wait();
}
} );

} )();