You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wow-spec.coffee 10KB

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