Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | |
| 4 | <head> |
| 5 | <meta charset="utf-8"> |
| 6 | |
| 7 | <title>reveal.js - Test Plugins</title> |
| 8 | |
| 9 | <link rel="stylesheet" href="../dist/reveal.css"> |
| 10 | <link rel="stylesheet" href="../node_modules/qunit/qunit/qunit.css"> |
| 11 | <script src="../node_modules/qunit/qunit/qunit.js"></script> |
| 12 | </head> |
| 13 | |
| 14 | <body style="overflow: auto;"> |
| 15 | |
| 16 | <div id="qunit"></div> |
| 17 | <div id="qunit-fixture"></div> |
| 18 | |
| 19 | <div class="reveal" style="display: none;"> |
| 20 | |
| 21 | <div class="slides"> |
| 22 | |
| 23 | <section>Slide content</section> |
| 24 | |
| 25 | </div> |
| 26 | |
| 27 | </div> |
| 28 | |
| 29 | <script src="../dist/reveal.js"></script> |
| 30 | <script> |
| 31 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 32 | QUnit.config.testTimeout = 30000; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 33 | QUnit.module( 'Plugins' ); |
| 34 | |
| 35 | var initCounter = { PluginB: 0, PluginC: 0, PluginD: 0 }; |
| 36 | |
| 37 | // Plugin with no init method |
| 38 | var PluginA = { id: 'PluginA' }; |
| 39 | |
| 40 | // Plugin with init method |
| 41 | var PluginB = { id: 'PluginB', init: function() { |
| 42 | initCounter['PluginB'] += 1; |
| 43 | } }; |
| 44 | |
| 45 | // Async plugin with init method |
| 46 | var PluginC = { id: 'PluginC', init: function() { |
| 47 | return new Promise(function( resolve ) { |
| 48 | setTimeout( () => { |
| 49 | initCounter['PluginC'] += 1; |
| 50 | resolve(); |
| 51 | }, 1000 ); |
| 52 | }); |
| 53 | } }; |
| 54 | |
| 55 | // Plugin initialized after reveal.js is ready |
| 56 | var PluginD = { id: 'PluginD', init: function() { |
| 57 | initCounter['PluginD'] += 1; |
| 58 | } }; |
| 59 | |
| 60 | var PluginE = { id: 'PluginE' }; |
| 61 | |
| 62 | var reveal = new Reveal( document.querySelector( '.reveal' ), { |
| 63 | plugins: [ PluginA ] |
| 64 | } ); |
| 65 | |
| 66 | reveal.registerPlugin( PluginB ); |
| 67 | reveal.registerPlugin( PluginC ); |
| 68 | |
| 69 | reveal.initialize(); |
| 70 | |
| 71 | QUnit.test( 'Can initialize synchronously', function( assert ) { |
| 72 | assert.strictEqual( initCounter['PluginB'], 1 ); |
| 73 | |
| 74 | reveal.registerPlugin( PluginB ); |
| 75 | |
| 76 | assert.strictEqual( initCounter['PluginB'], 1, 'prevents duplicate registration' ); |
| 77 | }); |
| 78 | |
| 79 | QUnit.test( 'Can initialize asynchronously', function( assert ) { |
| 80 | assert.expect( 3 ); |
| 81 | var done = assert.async( 2 ); |
| 82 | |
| 83 | assert.strictEqual( initCounter['PluginC'], 0, 'async plugin not immediately initialized' ); |
| 84 | |
| 85 | reveal.on( 'ready', function() { |
| 86 | assert.strictEqual( initCounter['PluginC'], 1, 'finsihed initializing when reveal.js dispatches "ready"' ); |
| 87 | done(); |
| 88 | |
| 89 | reveal.registerPlugin( PluginD ); |
| 90 | assert.strictEqual( initCounter['PluginD'], 1, 'plugin registered after reveal.js is ready still initiailizes' ); |
| 91 | done(); |
| 92 | }); |
| 93 | } ); |
| 94 | |
| 95 | QUnit.test( 'Can check if plugin is registered', function( assert ) { |
| 96 | assert.strictEqual( reveal.hasPlugin( 'PluginA' ), true ); |
| 97 | assert.strictEqual( reveal.hasPlugin( 'PluginE' ), false ); |
| 98 | reveal.registerPlugin( PluginE ); |
| 99 | assert.strictEqual( reveal.hasPlugin( 'PluginE' ), true ); |
| 100 | } ); |
| 101 | |
| 102 | QUnit.test( 'Can retrieve plugin instance', function( assert ) { |
| 103 | assert.strictEqual( reveal.getPlugin( 'PluginB' ), PluginB ); |
| 104 | } ); |
| 105 | </script> |
| 106 | |
| 107 | </body> |
| 108 | </html> |