transition_spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. })
  25. it('dynamic transitions', function (done) {
  26. var el = document.createElement('div')
  27. document.body.appendChild(el)
  28. var calls = {
  29. a: { enter: 0, leave: 0 },
  30. b: { enter: 0, leave: 0 }
  31. }
  32. var vm = new Vue({
  33. el: el,
  34. template: '<div v-show="show" v-transition="{{trans}}"></div>',
  35. data: {
  36. show: true,
  37. trans: 'a'
  38. },
  39. transitions: {
  40. a: {
  41. enter: function (el, done) {
  42. calls.a.enter++
  43. done()
  44. },
  45. leave: function (el, done) {
  46. calls.a.leave++
  47. done()
  48. }
  49. },
  50. b: {
  51. enter: function (el, done) {
  52. calls.b.enter++
  53. done()
  54. },
  55. leave: function (el, done) {
  56. calls.b.leave++
  57. done()
  58. }
  59. }
  60. }
  61. })
  62. assertCalls(0, 0, 0, 0)
  63. vm.show = false
  64. _.nextTick(function () {
  65. assertCalls(0, 1, 0, 0)
  66. vm.trans = 'b'
  67. vm.show = true
  68. _.nextTick(function () {
  69. assertCalls(0, 1, 1, 0)
  70. vm.show = false
  71. _.nextTick(function () {
  72. assertCalls(0, 1, 1, 1)
  73. vm.trans = 'a'
  74. vm.show = true
  75. _.nextTick(function () {
  76. assertCalls(1, 1, 1, 1)
  77. done()
  78. })
  79. })
  80. })
  81. })
  82. function assertCalls (a, b, c, d) {
  83. expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
  84. expect(calls.a.enter).toBe(a)
  85. expect(calls.a.leave).toBe(b)
  86. expect(calls.b.enter).toBe(c)
  87. expect(calls.b.leave).toBe(d)
  88. }
  89. })
  90. })
  91. }