transition_spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var transition = require('../../../../src/transition')
  4. var Transition = require('../../../../src/transition/transition')
  5. if (_.inBrowser && !_.isIE9) {
  6. describe('Transition', function () {
  7. // insert a test css
  8. function insertCSS (text) {
  9. var cssEl = document.createElement('style')
  10. cssEl.textContent = text
  11. document.head.appendChild(cssEl)
  12. }
  13. var duration = '50ms'
  14. insertCSS(
  15. '.test {\
  16. transition: opacity ' + duration + ' ease;\
  17. -webkit-transition: opacity ' + duration + ' ease;}'
  18. )
  19. insertCSS('.test-enter, .test-leave { opacity: 0; }')
  20. insertCSS(
  21. '.test-anim-enter {\
  22. animation: test-enter ' + duration + ';\
  23. -webkit-animation: test-enter ' + duration + ';}\
  24. .test-anim-leave {\
  25. animation: test-leave ' + duration + ';\
  26. -webkit-animation: test-leave ' + duration + ';}\
  27. @keyframes test-enter {\
  28. from { opacity: 0 }\
  29. to { opacity: 1 }}\
  30. @-webkit-keyframes test-enter {\
  31. from { opacity: 0 }\
  32. to { opacity: 1 }}\
  33. @keyframes test-leave {\
  34. from { opacity: 1 }\
  35. to { opacity: 0 }}\
  36. @-webkit-keyframes test-leave {\
  37. from { opacity: 1 }\
  38. to { opacity: 0 }}'
  39. )
  40. describe('Wrapper methods', function () {
  41. var spy, el, target, parent, vm
  42. beforeEach(function () {
  43. el = document.createElement('div')
  44. target = document.createElement('div')
  45. parent = document.createElement('div')
  46. parent.appendChild(target)
  47. spy = jasmine.createSpy('transition skip')
  48. vm = new Vue()
  49. spyOn(transition, 'apply')
  50. })
  51. it('append', function () {
  52. transition.append(el, parent, vm, spy)
  53. expect(parent.lastChild).toBe(el)
  54. expect(spy).toHaveBeenCalled()
  55. })
  56. it('before', function () {
  57. transition.before(el, target, vm, spy)
  58. expect(parent.firstChild).toBe(el)
  59. expect(el.nextSibling).toBe(target)
  60. expect(spy).toHaveBeenCalled()
  61. })
  62. it('remove', function () {
  63. transition.remove(target, vm, spy)
  64. expect(parent.childNodes.length).toBe(0)
  65. expect(spy).toHaveBeenCalled()
  66. })
  67. it('removeThenAppend', function () {
  68. transition.removeThenAppend(target, el, vm, spy)
  69. expect(parent.childNodes.length).toBe(0)
  70. expect(el.firstChild).toBe(target)
  71. expect(spy).toHaveBeenCalled()
  72. })
  73. })
  74. describe('Skipping', function () {
  75. var el, vm, op, cb
  76. beforeEach(function () {
  77. el = document.createElement('div')
  78. op = jasmine.createSpy('transition skip op')
  79. cb = jasmine.createSpy('transition skip cb')
  80. vm = new Vue()
  81. })
  82. it('skip el with no transition data', function () {
  83. transition.apply(el, 1, op, vm, cb)
  84. expect(op).toHaveBeenCalled()
  85. expect(cb).toHaveBeenCalled()
  86. })
  87. it('skip vm still being compiled', function () {
  88. el.__v_trans = new Transition(el, 'test', null, vm)
  89. transition.apply(el, 1, op, vm, cb)
  90. expect(op).toHaveBeenCalled()
  91. expect(cb).toHaveBeenCalled()
  92. })
  93. it('skip vm with parent still being compiled', function () {
  94. el.__v_trans = new Transition(el, 'test', null, vm)
  95. var child = vm.$addChild({
  96. el: el
  97. })
  98. expect(child._isCompiled).toBe(true)
  99. transition.apply(el, 1, op, child, cb)
  100. expect(op).toHaveBeenCalled()
  101. expect(cb).toHaveBeenCalled()
  102. })
  103. it('skip when css transition is not supported', function () {
  104. var e = _.transitionEndEvent
  105. _.transitionEndEvent = null
  106. el.__v_trans = new Transition(el, 'test', null, vm)
  107. vm.$mount(el)
  108. transition.apply(el, 1, op, vm, cb)
  109. expect(op).toHaveBeenCalled()
  110. expect(cb).toHaveBeenCalled()
  111. _.transitionEndEvent = e
  112. })
  113. })
  114. describe('CSS transitions', function () {
  115. var vm, el, op, cb, hooks
  116. beforeEach(function (done) {
  117. el = document.createElement('div')
  118. vm = new Vue({ el: el })
  119. op = jasmine.createSpy('css op')
  120. cb = jasmine.createSpy('css cb')
  121. document.body.appendChild(el)
  122. hooks = {
  123. beforeEnter: jasmine.createSpy('beforeEnter'),
  124. enter: jasmine.createSpy('enter'),
  125. afterEnter: jasmine.createSpy('afterEnter'),
  126. beforeLeave: jasmine.createSpy('beforeLeave'),
  127. leave: jasmine.createSpy('leave'),
  128. afterLeave: jasmine.createSpy('afterLeave')
  129. }
  130. // !IMPORTANT!
  131. // this ensures we force a layout for every test.
  132. _.nextTick(done)
  133. })
  134. afterEach(function () {
  135. document.body.removeChild(el)
  136. })
  137. it('skip on 0s duration (execute right at next frame)', function (done) {
  138. el.__v_trans = new Transition(el, 'test', hooks, vm)
  139. el.style.transition =
  140. el.style.WebkitTransition = 'opacity 0s ease'
  141. transition.apply(el, 1, op, vm, cb)
  142. expect(hooks.beforeEnter).toHaveBeenCalled()
  143. expect(hooks.enter).toHaveBeenCalled()
  144. _.nextTick(function () {
  145. expect(op).toHaveBeenCalled()
  146. expect(cb).toHaveBeenCalled()
  147. expect(hooks.afterEnter).toHaveBeenCalled()
  148. expect(el.classList.contains('test-enter')).toBe(false)
  149. transition.apply(el, -1, op, vm, cb)
  150. expect(hooks.beforeLeave).toHaveBeenCalled()
  151. expect(hooks.leave).toHaveBeenCalled()
  152. _.nextTick(function () {
  153. expect(op.calls.count()).toBe(2)
  154. expect(cb.calls.count()).toBe(2)
  155. expect(hooks.afterLeave).toHaveBeenCalled()
  156. expect(el.classList.contains('test-leave')).toBe(false)
  157. done()
  158. })
  159. })
  160. })
  161. it('skip when no transition available', function (done) {
  162. el.__v_trans = new Transition(el, 'test-no-trans', hooks, vm)
  163. transition.apply(el, 1, op, vm, cb)
  164. expect(hooks.beforeEnter).toHaveBeenCalled()
  165. expect(hooks.enter).toHaveBeenCalled()
  166. _.nextTick(function () {
  167. expect(op).toHaveBeenCalled()
  168. expect(cb).toHaveBeenCalled()
  169. expect(hooks.afterEnter).toHaveBeenCalled()
  170. expect(el.classList.contains('test-no-trans-enter')).toBe(false)
  171. // wait until transition.justEntered flag is off
  172. _.nextTick(function () {
  173. transition.apply(el, -1, op, vm, cb)
  174. expect(hooks.beforeLeave).toHaveBeenCalled()
  175. expect(hooks.leave).toHaveBeenCalled()
  176. _.nextTick(function () {
  177. expect(op.calls.count()).toBe(2)
  178. expect(cb.calls.count()).toBe(2)
  179. expect(hooks.afterLeave).toHaveBeenCalled()
  180. expect(el.classList.contains('test-no-trans-leave')).toBe(false)
  181. done()
  182. })
  183. })
  184. })
  185. })
  186. it('transition enter', function (done) {
  187. document.body.removeChild(el)
  188. el.__v_trans = new Transition(el, 'test', hooks, vm)
  189. // inline style
  190. el.style.transition =
  191. el.style.WebkitTransition = 'opacity ' + duration + ' ease'
  192. transition.apply(el, 1, function () {
  193. document.body.appendChild(el)
  194. op()
  195. }, vm, cb)
  196. expect(hooks.beforeEnter).toHaveBeenCalled()
  197. expect(hooks.enter).toHaveBeenCalled()
  198. expect(op).toHaveBeenCalled()
  199. expect(cb).not.toHaveBeenCalled()
  200. _.nextTick(function () {
  201. expect(el.classList.contains('test-enter')).toBe(false)
  202. expect(hooks.afterEnter).not.toHaveBeenCalled()
  203. _.on(el, _.transitionEndEvent, function () {
  204. expect(cb).toHaveBeenCalled()
  205. expect(hooks.afterEnter).toHaveBeenCalled()
  206. done()
  207. })
  208. })
  209. })
  210. it('transition leave', function (done) {
  211. el.__v_trans = new Transition(el, 'test', hooks, vm)
  212. // cascaded class style
  213. el.classList.add('test')
  214. // force a layout here so the transition can be triggered
  215. var f = el.offsetHeight
  216. transition.apply(el, -1, op, vm, cb)
  217. expect(hooks.beforeLeave).toHaveBeenCalled()
  218. expect(hooks.leave).toHaveBeenCalled()
  219. _.nextTick(function () {
  220. expect(op).not.toHaveBeenCalled()
  221. expect(cb).not.toHaveBeenCalled()
  222. expect(hooks.afterLeave).not.toHaveBeenCalled()
  223. expect(el.classList.contains('test-leave')).toBe(true)
  224. _.on(el, _.transitionEndEvent, function () {
  225. expect(op).toHaveBeenCalled()
  226. expect(cb).toHaveBeenCalled()
  227. expect(el.classList.contains('test-leave')).toBe(false)
  228. expect(hooks.afterLeave).toHaveBeenCalled()
  229. done()
  230. })
  231. })
  232. return f
  233. })
  234. it('animation enter', function (done) {
  235. document.body.removeChild(el)
  236. el.__v_trans = new Transition(el, 'test-anim', hooks, vm)
  237. transition.apply(el, 1, function () {
  238. document.body.appendChild(el)
  239. op()
  240. }, vm, cb)
  241. expect(hooks.beforeEnter).toHaveBeenCalled()
  242. expect(hooks.enter).toHaveBeenCalled()
  243. _.nextTick(function () {
  244. expect(op).toHaveBeenCalled()
  245. expect(cb).not.toHaveBeenCalled()
  246. expect(el.classList.contains('test-anim-enter')).toBe(true)
  247. expect(hooks.afterEnter).not.toHaveBeenCalled()
  248. _.on(el, _.animationEndEvent, function () {
  249. expect(el.classList.contains('test-anim-enter')).toBe(false)
  250. expect(cb).toHaveBeenCalled()
  251. expect(hooks.afterEnter).toHaveBeenCalled()
  252. done()
  253. })
  254. })
  255. })
  256. it('animation leave', function (done) {
  257. el.__v_trans = new Transition(el, 'test-anim', hooks, vm)
  258. transition.apply(el, -1, op, vm, cb)
  259. expect(hooks.beforeLeave).toHaveBeenCalled()
  260. expect(hooks.leave).toHaveBeenCalled()
  261. _.nextTick(function () {
  262. expect(op).not.toHaveBeenCalled()
  263. expect(cb).not.toHaveBeenCalled()
  264. expect(el.classList.contains('test-anim-leave')).toBe(true)
  265. expect(hooks.afterLeave).not.toHaveBeenCalled()
  266. _.on(el, _.animationEndEvent, function () {
  267. expect(op).toHaveBeenCalled()
  268. expect(cb).toHaveBeenCalled()
  269. expect(el.classList.contains('test-anim-leave')).toBe(false)
  270. expect(hooks.afterLeave).toHaveBeenCalled()
  271. done()
  272. })
  273. })
  274. })
  275. it('css + js hook with callback', function (done) {
  276. document.body.removeChild(el)
  277. el.classList.add('test')
  278. // enter hook that expects a second argument
  279. // indicates the user wants to control when the
  280. // transition ends.
  281. var enterCalled = false
  282. hooks.enter = function (el, enterDone) {
  283. enterCalled = true
  284. setTimeout(function () {
  285. enterDone()
  286. testDone()
  287. }, 100)
  288. }
  289. el.__v_trans = new Transition(el, 'test', hooks, vm)
  290. transition.apply(el, 1, function () {
  291. document.body.appendChild(el)
  292. op()
  293. }, vm, cb)
  294. expect(hooks.beforeEnter).toHaveBeenCalled()
  295. expect(op).toHaveBeenCalled()
  296. expect(cb).not.toHaveBeenCalled()
  297. expect(enterCalled).toBe(true)
  298. _.nextTick(function () {
  299. expect(el.classList.contains('test-enter')).toBe(false)
  300. expect(hooks.afterEnter).not.toHaveBeenCalled()
  301. _.on(el, _.transitionEndEvent, function () {
  302. // should wait until js callback is called!
  303. expect(cb).not.toHaveBeenCalled()
  304. expect(hooks.afterEnter).not.toHaveBeenCalled()
  305. })
  306. })
  307. // this is called by the enter hook
  308. function testDone () {
  309. expect(cb).toHaveBeenCalled()
  310. expect(hooks.afterEnter).toHaveBeenCalled()
  311. done()
  312. }
  313. })
  314. it('css + js hook with callback before transitionend', function (done) {
  315. document.body.removeChild(el)
  316. el.classList.add('test')
  317. // enter hook that expects a second argument
  318. // indicates the user wants to control when the
  319. // transition ends.
  320. var enterCalled = false
  321. hooks.enter = function (el, enterDone) {
  322. enterCalled = true
  323. setTimeout(function () {
  324. enterDone()
  325. testDone()
  326. }, 20)
  327. }
  328. el.__v_trans = new Transition(el, 'test', hooks, vm)
  329. transition.apply(el, 1, function () {
  330. document.body.appendChild(el)
  331. op()
  332. }, vm, cb)
  333. expect(hooks.beforeEnter).toHaveBeenCalled()
  334. expect(op).toHaveBeenCalled()
  335. expect(cb).not.toHaveBeenCalled()
  336. expect(enterCalled).toBe(true)
  337. _.nextTick(function () {
  338. expect(el.classList.contains('test-enter')).toBe(false)
  339. expect(hooks.afterEnter).not.toHaveBeenCalled()
  340. _.on(el, _.transitionEndEvent, function () {
  341. // callback should have been called, but only once, by the js callback
  342. expect(cb).toHaveBeenCalled()
  343. expect(cb.calls.count()).toBe(1)
  344. expect(hooks.afterEnter).toHaveBeenCalled()
  345. done()
  346. })
  347. })
  348. // this is called by the enter hook
  349. function testDone () {
  350. expect(cb).toHaveBeenCalled()
  351. expect(hooks.afterEnter).toHaveBeenCalled()
  352. }
  353. })
  354. it('clean up unfinished css callback', function (done) {
  355. el.__v_trans = new Transition(el, 'test', null, vm)
  356. el.classList.add('test')
  357. transition.apply(el, -1, function () {
  358. document.body.removeChild(el)
  359. }, vm, cb)
  360. // cancel early
  361. _.nextTick(function () {
  362. expect(el.__v_trans.pendingCssCb).toBeTruthy()
  363. expect(el.classList.contains('test-leave')).toBe(true)
  364. transition.apply(el, 1, function () {
  365. document.body.appendChild(el)
  366. }, vm)
  367. expect(cb).not.toHaveBeenCalled()
  368. expect(el.classList.contains('test-leave')).toBe(false)
  369. expect(el.__v_trans.pendingCssCb).toBeNull()
  370. // IMPORTANT
  371. // Let the queue flush finish before enter the next
  372. // test. Don't remove the nextTick.
  373. _.nextTick(done)
  374. })
  375. })
  376. it('cache transition sniff results', function (done) {
  377. el.__v_trans = new Transition(el, 'test', null, vm)
  378. el.classList.add('test')
  379. transition.apply(el, 1, op, vm)
  380. _.nextTick(function () {
  381. expect(el.__v_trans.typeCache['test-enter']).not.toBeUndefined()
  382. // for some reason window.getComputedStyle cannot be spied on in
  383. // phantomjs after the refactor...
  384. var calls = 0
  385. Object.defineProperty(el.__v_trans.typeCache, 'test-enter', {
  386. get: function () {
  387. calls++
  388. return 1
  389. }
  390. })
  391. transition.apply(el, 1, op, vm)
  392. _.nextTick(function () {
  393. expect(calls).toBe(1)
  394. done()
  395. })
  396. })
  397. })
  398. })
  399. describe('JavaScript only transitions', function () {
  400. var el, vm, op, cb, hooks
  401. beforeEach(function () {
  402. hooks = {}
  403. el = document.createElement('div')
  404. document.body.appendChild(el)
  405. op = jasmine.createSpy('js transition op')
  406. cb = jasmine.createSpy('js transition cb')
  407. vm = new Vue({ el: el })
  408. })
  409. afterEach(function () {
  410. document.body.removeChild(el)
  411. })
  412. it('beforeEnter', function () {
  413. var spy = jasmine.createSpy('js transition beforeEnter')
  414. hooks.beforeEnter = function (el) {
  415. spy(this, el)
  416. }
  417. el.__v_trans = new Transition(el, 'test', hooks, vm)
  418. transition.apply(el, 1, op, vm, cb)
  419. expect(spy).toHaveBeenCalledWith(vm, el)
  420. })
  421. it('enter', function () {
  422. var spy = jasmine.createSpy('js enter')
  423. hooks.enter = function (e, done) {
  424. expect(e).toBe(el)
  425. expect(op).toHaveBeenCalled()
  426. done()
  427. expect(cb).toHaveBeenCalled()
  428. spy(this)
  429. }
  430. el.__v_trans = new Transition(el, 'test', hooks, vm)
  431. transition.apply(el, 1, op, vm, cb)
  432. expect(spy).toHaveBeenCalledWith(vm)
  433. })
  434. it('leave', function () {
  435. var spy = jasmine.createSpy('js leave')
  436. hooks.leave = function (e, done) {
  437. expect(e).toBe(el)
  438. done()
  439. expect(op).toHaveBeenCalled()
  440. expect(cb).toHaveBeenCalled()
  441. spy(this)
  442. }
  443. el.__v_trans = new Transition(el, 'test', hooks, vm)
  444. transition.apply(el, -1, op, vm, cb)
  445. expect(spy).toHaveBeenCalledWith(vm)
  446. })
  447. it('no def', function (done) {
  448. el.__v_trans = new Transition(el, 'test', null, vm)
  449. transition.apply(el, 1, op, vm, cb)
  450. _.nextTick(function () {
  451. expect(op).toHaveBeenCalled()
  452. expect(cb).toHaveBeenCalled()
  453. transition.apply(el, -1, op, vm, cb)
  454. _.nextTick(function () {
  455. expect(op.calls.count()).toBe(2)
  456. expect(cb.calls.count()).toBe(2)
  457. done()
  458. })
  459. })
  460. })
  461. it('cancel hook', function (done) {
  462. var cleanupSpy = jasmine.createSpy('js cleanup')
  463. var leaveSpy = jasmine.createSpy('js leave')
  464. var timeout
  465. hooks.enter = function (el, done) {
  466. timeout = setTimeout(done, 30)
  467. }
  468. hooks.enterCancelled = function () {
  469. clearTimeout(timeout)
  470. cleanupSpy()
  471. }
  472. hooks.leave = function (el, done) {
  473. expect(cleanupSpy).toHaveBeenCalled()
  474. leaveSpy()
  475. done()
  476. }
  477. el.__v_trans = new Transition(el, 'test', hooks, vm)
  478. transition.apply(el, 1, op, vm, cb)
  479. setTimeout(function () {
  480. transition.apply(el, -1, op, vm)
  481. expect(leaveSpy).toHaveBeenCalled()
  482. setTimeout(function () {
  483. expect(cb).not.toHaveBeenCalled()
  484. done()
  485. }, 30)
  486. }, 15)
  487. })
  488. })
  489. })
  490. }