transition_spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var _ = require('src/util')
  2. var Vue = require('src')
  3. var Directive = require('src/directive')
  4. var def = require('src/directives/internal/transition')
  5. describe('transition', function () {
  6. it('should instantiate a transition object with correct args', function () {
  7. var fns = {}
  8. var el = document.createElement('div')
  9. var vm = new Vue({
  10. transitions: {
  11. test: fns
  12. }
  13. })
  14. var dir = new Directive({
  15. name: 'transition',
  16. raw: 'test',
  17. def: def,
  18. modifiers: {
  19. literal: true
  20. }
  21. }, vm, el)
  22. dir._bind()
  23. var transition = dir.el.__v_trans
  24. expect(transition.el).toBe(dir.el)
  25. expect(transition.hooks).toBe(fns)
  26. expect(transition.enterClass).toBe('test-enter')
  27. expect(transition.leaveClass).toBe('test-leave')
  28. expect(dir.el.className === 'test-transition')
  29. dir.update('lol', 'test')
  30. transition = dir.el.__v_trans
  31. expect(transition.enterClass).toBe('lol-enter')
  32. expect(transition.leaveClass).toBe('lol-leave')
  33. expect(transition.fns).toBeUndefined()
  34. expect(dir.el.className === 'lol-transition')
  35. })
  36. it('dynamic transitions', function (done) {
  37. var el = document.createElement('div')
  38. document.body.appendChild(el)
  39. var calls = {
  40. a: { enter: 0, leave: 0 },
  41. b: { enter: 0, leave: 0 }
  42. }
  43. var vm = new Vue({
  44. el: el,
  45. template: '<div v-show="show" :transition="trans"></div>',
  46. data: {
  47. show: true,
  48. trans: 'a'
  49. },
  50. transitions: {
  51. a: {
  52. enter: function (el, done) {
  53. calls.a.enter++
  54. done()
  55. },
  56. leave: function (el, done) {
  57. calls.a.leave++
  58. done()
  59. }
  60. },
  61. b: {
  62. enter: function (el, done) {
  63. calls.b.enter++
  64. done()
  65. },
  66. leave: function (el, done) {
  67. calls.b.leave++
  68. done()
  69. }
  70. }
  71. }
  72. })
  73. assertCalls(0, 0, 0, 0)
  74. vm.show = false
  75. _.nextTick(function () {
  76. assertCalls(0, 1, 0, 0)
  77. vm.trans = 'b'
  78. vm.show = true
  79. _.nextTick(function () {
  80. assertCalls(0, 1, 1, 0)
  81. vm.show = false
  82. _.nextTick(function () {
  83. assertCalls(0, 1, 1, 1)
  84. vm.trans = 'a'
  85. vm.show = true
  86. _.nextTick(function () {
  87. assertCalls(1, 1, 1, 1)
  88. done()
  89. })
  90. })
  91. })
  92. })
  93. function assertCalls (a, b, c, d) {
  94. expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
  95. expect(calls.a.enter).toBe(a)
  96. expect(calls.a.leave).toBe(b)
  97. expect(calls.b.enter).toBe(c)
  98. expect(calls.b.leave).toBe(d)
  99. }
  100. })
  101. })