Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

wow-spec.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. (function() {
  2. describe('WOW', function() {
  3. var timeout, winHeight;
  4. window.console = {
  5. warn: function() {}
  6. };
  7. timeout = 100;
  8. winHeight = 300;
  9. describe('smoke test', function() {
  10. it('exists', function() {
  11. return expect(WOW).toBeDefined();
  12. });
  13. return it("has an 'init' method", function() {
  14. return expect(new WOW().init).toBeDefined();
  15. });
  16. });
  17. describe('simple test environment', function() {
  18. beforeEach(function() {
  19. return loadFixtures('simple.html');
  20. });
  21. it('emulates window height', function() {
  22. return expect(document.documentElement.clientHeight).toBe(winHeight);
  23. });
  24. return it('has boxes set up for testing', function() {
  25. var boxCount, boxHeight, offset, style;
  26. boxHeight = 200;
  27. boxCount = $('#simple').children().length;
  28. expect($('#simple').height()).toBe(boxHeight * boxCount);
  29. expect($('#simple-1').height()).toBe(boxHeight);
  30. expect($('#simple-2').height()).toBe(boxHeight);
  31. expect($('#simple-3').height()).toBe(boxHeight);
  32. expect($('#simple-4').height()).toBe(boxHeight);
  33. expect($('#simple-5').height()).toBe(boxHeight);
  34. offset = $('#simple').offset().top;
  35. expect($('#simple-1').offset().top).toBe(offset + boxHeight * 0);
  36. expect($('#simple-2').offset().top).toBe(offset + boxHeight * 1);
  37. expect($('#simple-3').offset().top).toBe(offset + boxHeight * 2);
  38. expect($('#simple-4').offset().top).toBe(offset + boxHeight * 3);
  39. expect($('#simple-5').offset().top).toBe(offset + boxHeight * 4);
  40. style = $('#simple-5')[0].style;
  41. expect(style.background).toBe('yellow');
  42. return expect(style.color).toBe('red');
  43. });
  44. });
  45. describe('library behaviour', function() {
  46. var wow;
  47. wow = null;
  48. beforeEach(function(done) {
  49. loadFixtures('simple.html');
  50. (wow = new WOW).init();
  51. return setTimeout(function() {
  52. return done();
  53. }, timeout);
  54. });
  55. it('animates elements that are fully visible on the page', function() {
  56. expect($('#simple-1')).toHaveClass('animated');
  57. return expect($('#simple-1').css('visibility')).toBe('visible');
  58. });
  59. it("does not touch elements that don't have the marker class", function() {
  60. expect($('#simple-2')).not.toHaveClass('animated');
  61. return expect($('#simple-2').css('visibility')).toBe('visible');
  62. });
  63. it('does not animate elements not yet visible on the page', function() {
  64. expect($('#simple-3')).not.toHaveClass('animated');
  65. expect($('#simple-3').css('visibility')).not.toBe('visible');
  66. expect($('#simple-4')).not.toHaveClass('animated');
  67. return expect($('#simple-4').css('visibility')).not.toBe('visible');
  68. });
  69. it('animates elements after scrolling down and they become visible', function(done) {
  70. window.scrollTo(0, $('#simple-3').offset().top - winHeight + 150);
  71. return setTimeout(function() {
  72. expect($('#simple-3')).toHaveClass('animated');
  73. expect($('#simple-3').css('visibility')).toBe('visible');
  74. expect($('#simple-4')).not.toHaveClass('animated');
  75. expect($('#simple-4').css('visibility')).not.toBe('visible');
  76. window.scrollTo(0, $('#simple-4').offset().top - winHeight + 150);
  77. return setTimeout(function() {
  78. expect($('#simple-4')).toHaveClass('animated');
  79. expect($('#simple-4').css('visibility')).toBe('visible');
  80. return done();
  81. }, timeout);
  82. }, timeout);
  83. });
  84. it('does not tamper with the style attribute', function(done) {
  85. window.scrollTo(0, $('#simple-5').offset().top - winHeight + 150);
  86. return setTimeout(function() {
  87. expect($('#simple-5')).toHaveClass('animated');
  88. expect($('#simple-5').css('visibility')).toBe('visible');
  89. expect($('#simple-5')[0].style.background).toBe('yellow');
  90. expect($('#simple-5')[0].style.color).toBe('red');
  91. return done();
  92. }, timeout);
  93. });
  94. it('works with asynchronously loaded content', function(done) {
  95. $('#simple').append($('<div/>', {
  96. id: 'simple-6',
  97. "class": 'wow'
  98. }));
  99. wow.sync();
  100. window.scrollTo(0, $('#simple-6').offset().top - winHeight + 150);
  101. return setTimeout(function() {
  102. expect($('#simple-6')).toHaveClass('animated');
  103. expect($('#simple-6').css('visibility')).toBe('visible');
  104. return done();
  105. }, timeout);
  106. });
  107. return it('works with asynchronously loaded nested content', function(done) {
  108. $('#simple').append($('<div/>')).children().first().append($('<div/>', {
  109. id: 'simple-7',
  110. "class": 'wow'
  111. }));
  112. wow.sync();
  113. window.scrollTo(0, $('#simple-7').offset().top - winHeight + 150);
  114. return setTimeout(function() {
  115. expect($('#simple-7')).toHaveClass('animated');
  116. expect($('#simple-7').css('visibility')).toBe('visible');
  117. return done();
  118. }, timeout);
  119. });
  120. });
  121. describe('custom test environment', function() {
  122. beforeEach(function() {
  123. return loadFixtures('custom.html');
  124. });
  125. it('emulates window height', function() {
  126. return expect(document.documentElement.clientHeight).toBe(winHeight);
  127. });
  128. return it('has boxes set up for testing', function() {
  129. var offset;
  130. expect($('#custom').height()).toBe(800);
  131. expect($('#custom-1').height()).toBe(200);
  132. expect($('#custom-2').height()).toBe(200);
  133. expect($('#custom-3').height()).toBe(200);
  134. expect($('#custom-4').height()).toBe(200);
  135. offset = $('#custom').offset().top;
  136. expect($('#custom-1').offset().top).toBe(offset + 200 * 0);
  137. expect($('#custom-2').offset().top).toBe(offset + 200 * 1);
  138. expect($('#custom-3').offset().top).toBe(offset + 200 * 2);
  139. return expect($('#custom-4').offset().top).toBe(offset + 200 * 3);
  140. });
  141. });
  142. return describe('library behaviour with custom settings', function() {
  143. var called;
  144. called = false;
  145. beforeEach(function(done) {
  146. called = false;
  147. loadFixtures('custom.html');
  148. new WOW({
  149. boxClass: 'block',
  150. animateClass: 'fancy',
  151. offset: 10,
  152. callback: function() {
  153. return called = true;
  154. }
  155. }).init();
  156. $('.block').on('block', function() {
  157. return $(this).addClass('triggered');
  158. });
  159. return setTimeout(function() {
  160. return done();
  161. }, timeout);
  162. });
  163. it("creates two instances of the WOW.js with different configs", function() {
  164. var wow1, wow2;
  165. wow1 = new WOW({
  166. boxClass: 'block1',
  167. animateClass: 'fancy1',
  168. offset: 10
  169. });
  170. wow2 = new WOW({
  171. boxClass: 'block2',
  172. animateClass: 'fancy2',
  173. offset: 20
  174. });
  175. expect(wow1.config.boxClass).toBe("block1");
  176. expect(wow1.config.animateClass).toBe("fancy1");
  177. return expect(wow1.config.offset).toBe(10);
  178. });
  179. it("does not touch elements that don't have the marker class", function(done) {
  180. window.scrollTo(0, $('#custom-1').offset().top - winHeight + 15);
  181. return setTimeout(function() {
  182. expect($('#custom-1')).not.toHaveClass('fancy');
  183. return done();
  184. }, timeout);
  185. });
  186. it("animates elements that are partially visible on the page based on the 'offset' config", function(done) {
  187. return setTimeout(function() {
  188. window.scrollTo(0, $('#custom-2').offset().top - winHeight + 5);
  189. expect($('#custom-2')).not.toHaveClass('fancy');
  190. window.scrollTo(0, $('#custom-2').offset().top - winHeight + 15);
  191. return setTimeout(function() {
  192. expect($('#custom-2')).toHaveClass('fancy');
  193. expect($('#custom-2').css('visibility')).toBe('visible');
  194. return done();
  195. }, timeout);
  196. }, timeout);
  197. });
  198. it('does not animate elements not yet visible on the page', function() {
  199. expect($('#custom-3')).not.toHaveClass('fancy');
  200. return expect($('#custom-4')).not.toHaveClass('fancy');
  201. });
  202. it('animates elements after scrolling down and they become visible', function(done) {
  203. window.scrollTo(0, $('#custom-3').offset().top - winHeight + 150);
  204. return setTimeout(function() {
  205. expect($('#custom-3')).toHaveClass('fancy');
  206. expect($('#custom-3').css('visibility')).toBe('visible');
  207. expect($('#custom-3')[0].style.webkitAnimationIterationCount).toBe('2');
  208. expect($('#custom-4')).not.toHaveClass('fancy');
  209. window.scrollTo(0, $('#custom-4').offset().top - winHeight + 150);
  210. return setTimeout(function() {
  211. expect($('#custom-4')).toHaveClass('fancy');
  212. expect($('#custom-4').css('visibility')).toBe('visible');
  213. expect($('#custom-4')[0].style.webkitAnimationIterationCount).toBe('infinite');
  214. expect($('#custom-4')[0].style.webkitAnimationDuration).toBe('2s');
  215. expect($('#custom-4')[0].style.webkitAnimationDelay).toBe('1s');
  216. return done();
  217. }, timeout);
  218. }, timeout);
  219. });
  220. it("fires the callback", function(done) {
  221. called = false;
  222. window.scrollTo(0, $('#custom-3').offset().top - winHeight + 150);
  223. return setTimeout(function() {
  224. expect(called).toBe(true);
  225. return done();
  226. }, timeout);
  227. });
  228. return it('fires the callback on the visible element', function(done) {
  229. window.scrollTo(0, $('#custom-3').offset().top - winHeight + 150);
  230. return setTimeout(function() {
  231. expect($('#custom-3')).toHaveClass('triggered');
  232. expect($('#custom-4')).not.toHaveClass('triggered');
  233. window.scrollTo(0, $('#custom-4').offset().top - winHeight + 150);
  234. return setTimeout(function() {
  235. expect($('#custom-3')).toHaveClass('triggered');
  236. expect($('#custom-4')).toHaveClass('triggered');
  237. return done();
  238. }, timeout);
  239. }, timeout);
  240. });
  241. });
  242. });
  243. }).call(this);