transition_spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var _ = require('../../../../src/util')
  2. var Vue = _.Vue
  3. var def = require('../../../../src/directives/transition')
  4. if (_.inBrowser) {
  5. describe('transition', function () {
  6. it('should instantiate a transition object with correct args', function () {
  7. var fns = {}
  8. var dir = {
  9. el: document.createElement('div'),
  10. expression: 'test',
  11. bind: def.bind,
  12. update: def.update,
  13. vm: {
  14. $options: {
  15. transitions: {
  16. test: fns
  17. }
  18. }
  19. }
  20. }
  21. dir.bind()
  22. var transition = dir.el.__v_trans
  23. expect(transition.el).toBe(dir.el)
  24. expect(transition.hooks).toBe(fns)
  25. expect(transition.enterClass).toBe('test-enter')
  26. expect(transition.leaveClass).toBe('test-leave')
  27. expect(dir.el.className === 'test-transition')
  28. dir.update('lol', 'test')
  29. transition = dir.el.__v_trans
  30. expect(transition.enterClass).toBe('lol-enter')
  31. expect(transition.leaveClass).toBe('lol-leave')
  32. expect(transition.fns).toBeUndefined()
  33. expect(dir.el.className === 'lol-transition')
  34. })
  35. it('should bind the transition to closest vm', function () {
  36. var vm1 = new Vue()
  37. var vm2 = new Vue()
  38. var dir = {
  39. el: document.createElement('div'),
  40. expression: 'test',
  41. bind: def.bind,
  42. update: def.update,
  43. vm: vm1
  44. }
  45. dir.el.__vue__ = vm2
  46. dir.bind()
  47. expect(dir.el.__v_trans.vm).toBe(vm2)
  48. })
  49. // TODO remove this in 1.0.0
  50. it('dynamic transitions', function (done) {
  51. var el = document.createElement('div')
  52. document.body.appendChild(el)
  53. var calls = {
  54. a: { enter: 0, leave: 0 },
  55. b: { enter: 0, leave: 0 }
  56. }
  57. var vm = new Vue({
  58. el: el,
  59. template: '<div v-show="show" transition="{{trans}}"></div>',
  60. data: {
  61. show: true,
  62. trans: 'a'
  63. },
  64. transitions: {
  65. a: {
  66. enter: function (el, done) {
  67. calls.a.enter++
  68. done()
  69. },
  70. leave: function (el, done) {
  71. calls.a.leave++
  72. done()
  73. }
  74. },
  75. b: {
  76. enter: function (el, done) {
  77. calls.b.enter++
  78. done()
  79. },
  80. leave: function (el, done) {
  81. calls.b.leave++
  82. done()
  83. }
  84. }
  85. }
  86. })
  87. assertCalls(0, 0, 0, 0)
  88. vm.show = false
  89. _.nextTick(function () {
  90. assertCalls(0, 1, 0, 0)
  91. vm.trans = 'b'
  92. vm.show = true
  93. _.nextTick(function () {
  94. assertCalls(0, 1, 1, 0)
  95. vm.show = false
  96. _.nextTick(function () {
  97. assertCalls(0, 1, 1, 1)
  98. vm.trans = 'a'
  99. vm.show = true
  100. _.nextTick(function () {
  101. assertCalls(1, 1, 1, 1)
  102. done()
  103. })
  104. })
  105. })
  106. })
  107. function assertCalls (a, b, c, d) {
  108. expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
  109. expect(calls.a.enter).toBe(a)
  110. expect(calls.a.leave).toBe(b)
  111. expect(calls.b.enter).toBe(c)
  112. expect(calls.b.leave).toBe(d)
  113. }
  114. })
  115. it('dynamic transitions (new syntax)', function (done) {
  116. var el = document.createElement('div')
  117. document.body.appendChild(el)
  118. var calls = {
  119. a: { enter: 0, leave: 0 },
  120. b: { enter: 0, leave: 0 }
  121. }
  122. var vm = new Vue({
  123. el: el,
  124. template: '<div v-show="show" bind-transition="trans"></div>',
  125. data: {
  126. show: true,
  127. trans: 'a'
  128. },
  129. transitions: {
  130. a: {
  131. enter: function (el, done) {
  132. calls.a.enter++
  133. done()
  134. },
  135. leave: function (el, done) {
  136. calls.a.leave++
  137. done()
  138. }
  139. },
  140. b: {
  141. enter: function (el, done) {
  142. calls.b.enter++
  143. done()
  144. },
  145. leave: function (el, done) {
  146. calls.b.leave++
  147. done()
  148. }
  149. }
  150. }
  151. })
  152. assertCalls(0, 0, 0, 0)
  153. vm.show = false
  154. _.nextTick(function () {
  155. assertCalls(0, 1, 0, 0)
  156. vm.trans = 'b'
  157. vm.show = true
  158. _.nextTick(function () {
  159. assertCalls(0, 1, 1, 0)
  160. vm.show = false
  161. _.nextTick(function () {
  162. assertCalls(0, 1, 1, 1)
  163. vm.trans = 'a'
  164. vm.show = true
  165. _.nextTick(function () {
  166. assertCalls(1, 1, 1, 1)
  167. done()
  168. })
  169. })
  170. })
  171. })
  172. function assertCalls (a, b, c, d) {
  173. expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
  174. expect(calls.a.enter).toBe(a)
  175. expect(calls.a.leave).toBe(b)
  176. expect(calls.b.enter).toBe(c)
  177. expect(calls.b.leave).toBe(d)
  178. }
  179. })
  180. })
  181. }