JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css">
<div id="qunit"></div>
<div id="plugin"></div>
JavaScript
;(function ($) {
function PluginName() {
/* some more code here */
}
$.extend(PluginName.prototype, {
_attachPlugin: function (target, options, value) {
target = $(target);
if (target.hasClass(this.shortenerClass)) {
return;
}
var instance = {
options: $.extend({}, this._defaults)
};
if (typeof instance.options.requiredOption === 'undefined') {
throw 'You need required option!';
}
},
});
var getters = [/* some getters */];
function isNotChained(method, otherArgs) {
if (method === 'option' && (otherArgs.length === 0 ||
(otherArgs.length === 1 && typeof otherArgs[0] === 'string'))) {
return true;
}
return $.inArray(method, getters) > -1;
}
$.fn.pluginname = function (options) {
var args = Array.prototype.slice.call(arguments, 1);
if (isNotChained(options, args)) {
return plugin['_' + options + 'Plugin'].apply(plugin, [this[0]].concat(args));
}
return this.each(function () {
if (typeof options === 'string') {
if (!plugin['_' + options + 'Plugin']) {
throw 'Unknown method: ' + options;
}
plugin['_' + options + 'Plugin'].apply(plugin, [this].concat(args));
} else {
plugin._attachPlugin(this, options || {});
}
});
};
var plugin = $.pluginname = new PluginName();
})(jQuery);
test('Init error', function () {
var selector = "#plugin";
throws(function() { $(selector).pluginname() }, 'Throws error when missing required option.')
});