返回 reveal.js
test-plugins.html
根目录 / test / test-plugins.html
1 <!doctype html>
2 <html lang="en">
3
4 <head>
5 <meta charset="utf-8">
6
7 <title>reveal.js - Test Plugins</title>
8 </head>
9
10 <body style="overflow: auto;">
11
12 <div id="qunit"></div>
13 <div id="qunit-fixture"></div>
14
15 <div class="reveal" style="display: none;">
16
17 <div class="slides">
18
19 <section>Slide content</section>
20
21 </div>
22
23 </div>
24
25 <script type="module">
26 import 'reveal.css';
27 import 'qunit/qunit/qunit.css';
28 import QUnit from 'qunit';
29 import Reveal from 'reveal.js';
30
31 QUnit.config.testTimeout = 30000;
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>
108
108 lines HTML