Skip to content

Commit

Permalink
add hasPlugin and getPlugin API methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Apr 1, 2019
1 parent d6f0f41 commit df25fbe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 26 additions & 1 deletion js/reveal.js
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,29 @@

}

/**
* Checks if a specific plugin has been registered.
*
* @param {String} id Unique plugin identifier
*/
function hasPlugin( id ) {

return !!plugins[id];

}

/**
* Returns the specific plugin instance, if a plugin
* with the given ID has been registered.
*
* @param {String} id Unique plugin identifier
*/
function getPlugin( id ) {

return plugins[id];

}

/**
* Add a custom key binding with optional description to
* be added to the help screen.
Expand Down Expand Up @@ -5975,8 +5998,10 @@
addKeyBinding: addKeyBinding,
removeKeyBinding: removeKeyBinding,

// Called by plugins to register themselves
// API for registering and retrieving plugins
registerPlugin: registerPlugin,
hasPlugin: hasPlugin,
getPlugin: getPlugin,

// Programatically triggers a keyboard event
triggerKey: function( keyCode ) {
Expand Down
15 changes: 14 additions & 1 deletion test/test-plugins.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
initCounter['PluginD'] += 1;
} };

var PluginE = {};

Reveal.registerPlugin( 'PluginA', PluginA );
Reveal.registerPlugin( 'PluginB', PluginB );
Reveal.registerPlugin( 'PluginC', PluginC );
Expand All @@ -71,7 +73,7 @@
assert.strictEqual( initCounter['PluginB'], 1, 'prevents duplicate registration' );
});

QUnit.test( 'Can initialie asynchronously', function( assert ) {
QUnit.test( 'Can initialize asynchronously', function( assert ) {
assert.expect( 3 );
var done = assert.async( 2 );

Expand All @@ -86,6 +88,17 @@
done();
});
} );

QUnit.test( 'Can check if plugin is registered', function( assert ) {
assert.strictEqual( Reveal.hasPlugin( 'PluginA' ), true );
assert.strictEqual( Reveal.hasPlugin( 'PluginE' ), false );
Reveal.registerPlugin( 'PluginE', PluginE );
assert.strictEqual( Reveal.hasPlugin( 'PluginE' ), true );
} );

QUnit.test( 'Can retrieve plugin instance', function( assert ) {
assert.strictEqual( Reveal.getPlugin( 'PluginB' ), PluginB );
} );
</script>

</body>
Expand Down

0 comments on commit df25fbe

Please sign in to comment.