test-state.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>reveal.js - Test State</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>No state</section>
  16. <section id="slide2" data-state="state1">State 1</section>
  17. <section data-state="state1">State 1</section>
  18. <section data-state="state2">State 2</section>
  19. <section>
  20. <section>No state</section>
  21. <section data-state="state1">State 1</section>
  22. <section data-state="state3">State 3</section>
  23. <section>No state</section>
  24. </section>
  25. <section>No state</section>
  26. </div>
  27. </div>
  28. <script src="../dist/reveal.js"></script>
  29. <script>
  30. Reveal.initialize().then( function() {
  31. console.log(Reveal);
  32. QUnit.module( 'State' );
  33. QUnit.test( 'Fire events when changing slide', function( assert ) {
  34. assert.expect( 2 );
  35. var state1 = assert.async();
  36. var state2 = assert.async();
  37. var _onState1 = function( event ) {
  38. assert.ok( true, 'state1 fired' );
  39. state1();
  40. }
  41. var _onState2 = function( event ) {
  42. assert.ok( true, 'state2 fired' );
  43. state2();
  44. }
  45. Reveal.on( 'state1', _onState1 );
  46. Reveal.on( 'state2', _onState2 );
  47. Reveal.slide( 1 );
  48. Reveal.slide( 3 );
  49. Reveal.off( 'state1', _onState1 );
  50. Reveal.off( 'state2', _onState2 );
  51. });
  52. QUnit.test( 'Fire state events for vertical slides', function( assert ) {
  53. assert.expect( 2 );
  54. var done = assert.async( 2 );
  55. var _onState1 = function( event ) {
  56. assert.ok( true, 'state1 fired' );
  57. done();
  58. }
  59. var _onState3 = function( event ) {
  60. assert.ok( true, 'state3 fired' );
  61. done();
  62. }
  63. Reveal.on( 'state1', _onState1 );
  64. Reveal.on( 'state3', _onState3 );
  65. Reveal.slide( 0 );
  66. Reveal.slide( 4, 1 );
  67. Reveal.slide( 4, 2 );
  68. Reveal.off( 'state1', _onState1 );
  69. Reveal.off( 'state3', _onState3 );
  70. });
  71. QUnit.test( 'No events if state remains unchanged', function( assert ) {
  72. var stateChanges = 0;
  73. var _onEvent = function( event ) {
  74. stateChanges += 1;
  75. }
  76. Reveal.on( 'state1', _onEvent );
  77. Reveal.slide( 0 ); // no state
  78. Reveal.slide( 1 ); // state1
  79. Reveal.slide( 2 ); // state1
  80. Reveal.prev(); // state1
  81. Reveal.next(); // state1
  82. Reveal.slide( 4, 1 ); // state1
  83. Reveal.slide( 0 ); // no state
  84. Reveal.off( 'state1', _onEvent );
  85. assert.strictEqual( stateChanges, 1, 'no event was fired when going to slide with same state' );
  86. });
  87. QUnit.test( 'Event order', function( assert ) {
  88. var _onEvent = function( event ) {
  89. assert.ok( Reveal.getCurrentSlide() == document.querySelector( '#slide2' ), 'correct current slide immediately after state event' );
  90. }
  91. Reveal.on( 'state1', _onEvent );
  92. Reveal.slide( 0 );
  93. Reveal.slide( 1 );
  94. Reveal.off( 'state1', _onEvent );
  95. });
  96. } );
  97. </script>
  98. </body>
  99. </html>