test-plugins.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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="../css/reveal.css">
  7. <link rel="stylesheet" href="qunit-2.5.0.css">
  8. </head>
  9. <body style="overflow: auto;">
  10. <div id="qunit"></div>
  11. <div id="qunit-fixture"></div>
  12. <div class="reveal" style="display: none;">
  13. <div class="slides">
  14. <section>Slide content</section>
  15. </div>
  16. </div>
  17. <script src="../js/reveal.js"></script>
  18. <script src="qunit-2.5.0.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 = {};
  24. // Plugin with init method
  25. var PluginB = { init: function() {
  26. initCounter['PluginB'] += 1;
  27. } };
  28. // Async plugin with init method
  29. var 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 = { init: function() {
  39. initCounter['PluginD'] += 1;
  40. } };
  41. var PluginE = {};
  42. Reveal.registerPlugin( 'PluginA', PluginA );
  43. Reveal.registerPlugin( 'PluginB', PluginB );
  44. Reveal.registerPlugin( 'PluginC', PluginC );
  45. Reveal.initialize();
  46. QUnit.test( 'Can initialize synchronously', function( assert ) {
  47. assert.strictEqual( initCounter['PluginB'], 1 );
  48. Reveal.registerPlugin( 'PluginB', PluginB );
  49. assert.strictEqual( initCounter['PluginB'], 1, 'prevents duplicate registration' );
  50. });
  51. QUnit.test( 'Can initialize asynchronously', function( assert ) {
  52. assert.expect( 3 );
  53. var done = assert.async( 2 );
  54. assert.strictEqual( initCounter['PluginC'], 0, 'async plugin not immediately initialized' );
  55. Reveal.addEventListener( 'ready', function() {
  56. assert.strictEqual( initCounter['PluginC'], 1, 'finsihed initializing when reveal.js dispatches "ready"' );
  57. done();
  58. Reveal.registerPlugin( 'PluginD', PluginD );
  59. assert.strictEqual( initCounter['PluginD'], 1, 'plugin registered after reveal.js is ready still initiailizes' );
  60. done();
  61. });
  62. } );
  63. QUnit.test( 'Can check if plugin is registered', function( assert ) {
  64. assert.strictEqual( Reveal.hasPlugin( 'PluginA' ), true );
  65. assert.strictEqual( Reveal.hasPlugin( 'PluginE' ), false );
  66. Reveal.registerPlugin( 'PluginE', PluginE );
  67. assert.strictEqual( Reveal.hasPlugin( 'PluginE' ), true );
  68. } );
  69. QUnit.test( 'Can retrieve plugin instance', function( assert ) {
  70. assert.strictEqual( Reveal.getPlugin( 'PluginB' ), PluginB );
  71. } );
  72. </script>
  73. </body>
  74. </html>