auto-play.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. QUnit.test( 'auto play', function( assert ) {
  2. 'use strict';
  3. var done = assert.async();
  4. var flkty = new Flickity( '#auto-play', {
  5. autoPlay: 200
  6. });
  7. var selectCount = 0;
  8. var testDelay = flkty.options.autoPlay + 100;
  9. var tests;
  10. function nextTest() {
  11. if ( tests.length ) {
  12. var next = tests.shift();
  13. next();
  14. } else {
  15. flkty.stopPlayer();
  16. done();
  17. }
  18. }
  19. tests = [
  20. // check that player runs
  21. function() {
  22. var onSelect = function() {
  23. selectCount++;
  24. if ( selectCount < 5 ) {
  25. assert.equal( flkty.selectedIndex, selectCount % flkty.cells.length,
  26. 'auto-played to ' + flkty.selectedIndex );
  27. } else if ( selectCount == 5 ) {
  28. // HACK do async, should be able to stop after a tick
  29. flkty.off( 'select', onSelect );
  30. nextTest();
  31. }
  32. };
  33. flkty.on( 'select', onSelect );
  34. },
  35. // pause & unpause
  36. function() {
  37. function onPauseSelect() {
  38. assert.ok( false, 'player ticked during pause' );
  39. }
  40. flkty.on( 'select', onPauseSelect );
  41. flkty.pausePlayer();
  42. setTimeout( function() {
  43. assert.ok( true, 'player did not tick during pause' );
  44. flkty.off( 'select', onPauseSelect );
  45. flkty.once( 'select', function() {
  46. assert.ok( true, 'player resumed after unpausing' );
  47. nextTest();
  48. });
  49. flkty.unpausePlayer();
  50. }, testDelay );
  51. },
  52. // stopPlayer
  53. function() {
  54. var ticks = 0;
  55. function onSelect() {
  56. ticks++;
  57. }
  58. flkty.stopPlayer();
  59. setTimeout( function() {
  60. flkty.off( 'select', onSelect );
  61. assert.equal( ticks, 0, 'no ticks after stopped' );
  62. nextTest();
  63. }, testDelay * 2 );
  64. },
  65. // double playPlayer()
  66. function() {
  67. var ticks = 0;
  68. function onSelect() {
  69. ticks++;
  70. }
  71. flkty.stopPlayer();
  72. flkty.on( 'select', onSelect );
  73. flkty.playPlayer();
  74. flkty.playPlayer();
  75. setTimeout( function() {
  76. flkty.off( 'select', onSelect );
  77. assert.equal( ticks, 1, 'only one tick after double playPlayer' );
  78. nextTest();
  79. }, testDelay );
  80. },
  81. ];
  82. nextTest();
  83. });