transition_spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var _ = require('../../../../src/util')
  2. var Vue = _.Vue
  3. var def = require('../../../../src/directives/transition')
  4. if (_.inBrowser) {
  5. describe('v-transition', function () {
  6. it('should save the transition id and custom functions as data', 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. expect(dir.el.__v_trans.id).toBe('test')
  23. expect(dir.el.__v_trans.fns).toBe(fns)
  24. expect(dir.el.className === 'test-transition')
  25. dir.update('lol', 'test')
  26. expect(dir.el.__v_trans.id).toBe('lol')
  27. expect(dir.el.__v_trans.fns).toBeNull()
  28. expect(dir.el.className === 'lol-transition')
  29. })
  30. it('dynamic transitions', function (done) {
  31. var el = document.createElement('div')
  32. document.body.appendChild(el)
  33. var calls = {
  34. a: { enter: 0, leave: 0 },
  35. b: { enter: 0, leave: 0 }
  36. }
  37. var vm = new Vue({
  38. el: el,
  39. template: '<div v-show="show" v-transition="{{trans}}"></div>',
  40. data: {
  41. show: true,
  42. trans: 'a'
  43. },
  44. transitions: {
  45. a: {
  46. enter: function (el, done) {
  47. calls.a.enter++
  48. done()
  49. },
  50. leave: function (el, done) {
  51. calls.a.leave++
  52. done()
  53. }
  54. },
  55. b: {
  56. enter: function (el, done) {
  57. calls.b.enter++
  58. done()
  59. },
  60. leave: function (el, done) {
  61. calls.b.leave++
  62. done()
  63. }
  64. }
  65. }
  66. })
  67. assertCalls(0, 0, 0, 0)
  68. vm.show = false
  69. _.nextTick(function () {
  70. assertCalls(0, 1, 0, 0)
  71. vm.trans = 'b'
  72. vm.show = true
  73. _.nextTick(function () {
  74. assertCalls(0, 1, 1, 0)
  75. vm.show = false
  76. _.nextTick(function () {
  77. assertCalls(0, 1, 1, 1)
  78. vm.trans = 'a'
  79. vm.show = true
  80. _.nextTick(function () {
  81. assertCalls(1, 1, 1, 1)
  82. done()
  83. })
  84. })
  85. })
  86. })
  87. function assertCalls (a, b, c, d) {
  88. expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
  89. expect(calls.a.enter).toBe(a)
  90. expect(calls.a.leave).toBe(b)
  91. expect(calls.b.enter).toBe(c)
  92. expect(calls.b.leave).toBe(d)
  93. }
  94. })
  95. })
  96. }