test-plugins.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>reveal.js - Test Plugins</title>
  6. <link rel="stylesheet" href="../dist/reveal.css">
  7. <link rel="stylesheet" href="../node_modules/qunit/qunit/qunit.css">
  8. <script src="../node_modules/qunit/qunit/qunit.js"></script>
  9. </head>
  10. <body style="overflow: auto;">
  11. <div id="qunit"></div>
  12. <div id="qunit-fixture"></div>
  13. <div class="reveal" style="display: none;">
  14. <div class="slides">
  15. <section>Slide content</section>
  16. </div>
  17. </div>
  18. <script src="../dist/reveal.js"></script>
  19. <script>
  20. QUnit.module( 'Plugins' );
  21. var initCounter = { PluginB: 0, PluginC: 0, PluginD: 0 };
  22. // Plugin with no init method
  23. var PluginA = { id: 'PluginA' };
  24. // Plugin with init method
  25. var PluginB = { id: 'PluginB', init: function() {
  26. initCounter['PluginB'] += 1;
  27. } };
  28. // Async plugin with init method
  29. var PluginC = { id: 'PluginC', init: function() {
  30. return new Promise(function( resolve ) {
  31. setTimeout( () => {
  32. initCounter['PluginC'] += 1;
  33. resolve();
  34. }, 1000 );
  35. });
  36. } };
  37. // Plugin initialized after reveal.js is ready
  38. var PluginD = { id: 'PluginD', init: function() {
  39. initCounter['PluginD'] += 1;
  40. } };
  41. var PluginE = { id: 'PluginE' };
  42. var reveal = new Reveal( document.querySelector( '.reveal' ), {
  43. plugins: [ PluginA ]
  44. } );
  45. reveal.registerPlugin( PluginB );
  46. reveal.registerPlugin( PluginC );
  47. reveal.initialize();
  48. QUnit.test( 'Can initialize synchronously', function( assert ) {
  49. assert.strictEqual( initCounter['PluginB'], 1 );
  50. reveal.registerPlugin( PluginB );
  51. assert.strictEqual( initCounter['PluginB'], 1, 'prevents duplicate registration' );
  52. });
  53. QUnit.test( 'Can initialize asynchronously', function( assert ) {
  54. assert.expect( 3 );
  55. var done = assert.async( 2 );
  56. assert.strictEqual( initCounter['PluginC'], 0, 'async plugin not immediately initialized' );
  57. reveal.on( 'ready', function() {
  58. assert.strictEqual( initCounter['PluginC'], 1, 'finsihed initializing when reveal.js dispatches "ready"' );
  59. done();
  60. reveal.registerPlugin( PluginD );
  61. assert.strictEqual( initCounter['PluginD'], 1, 'plugin registered after reveal.js is ready still initiailizes' );
  62. done();
  63. });
  64. } );
  65. QUnit.test( 'Can check if plugin is registered', function( assert ) {
  66. assert.strictEqual( reveal.hasPlugin( 'PluginA' ), true );
  67. assert.strictEqual( reveal.hasPlugin( 'PluginE' ), false );
  68. reveal.registerPlugin( PluginE );
  69. assert.strictEqual( reveal.hasPlugin( 'PluginE' ), true );
  70. } );
  71. QUnit.test( 'Can retrieve plugin instance', function( assert ) {
  72. assert.strictEqual( reveal.getPlugin( 'PluginB' ), PluginB );
  73. } );
  74. </script>
  75. </body>
  76. </html>