test-state.html 3.4 KB

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