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 | |
| 32 | QUnit.module( 'Plugins' ); |
| 33 | |
| 34 | var initCounter = { PluginB: 0, PluginC: 0, PluginD: 0 }; |
| 35 | |
| 36 | // Plugin with no init method |
| 37 | var PluginA = { id: 'PluginA' }; |
| 38 | |
| 39 | // Plugin with init method |
| 40 | var PluginB = { id: 'PluginB', init: function() { |
| 41 | initCounter['PluginB'] += 1; |
| 42 | } }; |
| 43 | |
| 44 | // Async plugin with init method |
| 45 | var PluginC = { id: 'PluginC', init: function() { |
| 46 | return new Promise(function( resolve ) { |
| 47 | setTimeout( () => { |
| 48 | initCounter['PluginC'] += 1; |
| 49 | resolve(); |
| 50 | }, 1000 ); |
| 51 | }); |
| 52 | } }; |
| 53 | |
| 54 | // Plugin initialized after reveal.js is ready |
| 55 | var PluginD = { id: 'PluginD', init: function() { |
| 56 | initCounter['PluginD'] += 1; |
| 57 | } }; |
| 58 | |
| 59 | var PluginE = { id: 'PluginE' }; |
| 60 | |
| 61 | var reveal = new Reveal( document.querySelector( '.reveal' ), { |
| 62 | plugins: [ PluginA ] |
| 63 | } ); |
| 64 | |
| 65 | reveal.registerPlugin( PluginB ); |
| 66 | reveal.registerPlugin( PluginC ); |
| 67 | |
| 68 | reveal.initialize(); |
| 69 | |
| 70 | QUnit.test( 'Can initialize synchronously', function( assert ) { |
| 71 | assert.strictEqual( initCounter['PluginB'], 1 ); |
| 72 | |
| 73 | reveal.registerPlugin( PluginB ); |
| 74 | |
| 75 | assert.strictEqual( initCounter['PluginB'], 1, 'prevents duplicate registration' ); |
| 76 | }); |
| 77 | |
| 78 | QUnit.test( 'Can initialize asynchronously', function( assert ) { |
| 79 | assert.expect( 3 ); |
| 80 | var done = assert.async( 2 ); |
| 81 | |
| 82 | assert.strictEqual( initCounter['PluginC'], 0, 'async plugin not immediately initialized' ); |
| 83 | |
| 84 | reveal.on( 'ready', function() { |
| 85 | assert.strictEqual( initCounter['PluginC'], 1, 'finsihed initializing when reveal.js dispatches "ready"' ); |
| 86 | done(); |
| 87 | |
| 88 | reveal.registerPlugin( PluginD ); |
| 89 | assert.strictEqual( initCounter['PluginD'], 1, 'plugin registered after reveal.js is ready still initiailizes' ); |
| 90 | done(); |
| 91 | }); |
| 92 | } ); |
| 93 | |
| 94 | QUnit.test( 'Can check if plugin is registered', function( assert ) { |
| 95 | assert.strictEqual( reveal.hasPlugin( 'PluginA' ), true ); |
| 96 | assert.strictEqual( reveal.hasPlugin( 'PluginE' ), false ); |
| 97 | reveal.registerPlugin( PluginE ); |
| 98 | assert.strictEqual( reveal.hasPlugin( 'PluginE' ), true ); |
| 99 | } ); |
| 100 | |
| 101 | QUnit.test( 'Can retrieve plugin instance', function( assert ) { |
| 102 | assert.strictEqual( reveal.getPlugin( 'PluginB' ), PluginB ); |
| 103 | } ); |
| 104 | </script> |
| 105 | |
| 106 | </body> |
| 107 | </html> |