directives.spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Vue from 'vue'
  2. describe('Options directives', () => {
  3. it('basic usage', done => {
  4. const bindSpy = jasmine.createSpy('bind')
  5. const updateSpy = jasmine.createSpy('update')
  6. const componentUpdatedSpy = jasmine.createSpy('componentUpdated')
  7. const unbindSpy = jasmine.createSpy('unbind')
  8. const assertContext = (el, binding, vnode) => {
  9. expect(vnode.context).toBe(vm)
  10. expect(binding.arg).toBe('arg')
  11. expect(binding.modifiers).toEqual({ hello: true })
  12. }
  13. const vm = new Vue({
  14. template: '<div v-if="ok" v-test:arg.hello="a">{{ msg }}</div>',
  15. data: {
  16. msg: 'hi',
  17. a: 'foo',
  18. ok: true
  19. },
  20. directives: {
  21. test: {
  22. bind (el, binding, vnode) {
  23. bindSpy()
  24. assertContext(el, binding, vnode)
  25. expect(binding.value).toBe('foo')
  26. expect(binding.expression).toBe('a')
  27. expect(binding.oldValue).toBeUndefined()
  28. },
  29. update (el, binding, vnode, oldVnode) {
  30. updateSpy()
  31. assertContext(el, binding, vnode)
  32. expect(el).toBe(vm.$el)
  33. expect(oldVnode).toBe(vm._vnode)
  34. expect(oldVnode).not.toBe(vnode)
  35. expect(binding.expression).toBe('a')
  36. if (binding.value !== binding.oldValue) {
  37. expect(binding.value).toBe('bar')
  38. expect(binding.oldValue).toBe('foo')
  39. }
  40. },
  41. componentUpdated (el, binding, vnode) {
  42. componentUpdatedSpy()
  43. assertContext(el, binding, vnode)
  44. },
  45. unbind (el, binding, vnode) {
  46. unbindSpy()
  47. assertContext(el, binding, vnode)
  48. }
  49. }
  50. }
  51. })
  52. vm.$mount()
  53. expect(bindSpy).toHaveBeenCalled()
  54. expect(updateSpy).not.toHaveBeenCalled()
  55. expect(componentUpdatedSpy).not.toHaveBeenCalled()
  56. expect(unbindSpy).not.toHaveBeenCalled()
  57. vm.a = 'bar'
  58. waitForUpdate(() => {
  59. expect(updateSpy).toHaveBeenCalled()
  60. expect(componentUpdatedSpy).toHaveBeenCalled()
  61. expect(unbindSpy).not.toHaveBeenCalled()
  62. vm.msg = 'bye'
  63. }).then(() => {
  64. expect(componentUpdatedSpy.calls.count()).toBe(2)
  65. vm.ok = false
  66. }).then(() => {
  67. expect(unbindSpy).toHaveBeenCalled()
  68. }).then(done)
  69. })
  70. it('function shorthand', done => {
  71. const spy = jasmine.createSpy('directive')
  72. const vm = new Vue({
  73. template: '<div v-test:arg.hello="a"></div>',
  74. data: { a: 'foo' },
  75. directives: {
  76. test (el, binding, vnode) {
  77. expect(vnode.context).toBe(vm)
  78. expect(binding.arg).toBe('arg')
  79. expect(binding.modifiers).toEqual({ hello: true })
  80. spy(binding.value, binding.oldValue)
  81. }
  82. }
  83. })
  84. vm.$mount()
  85. expect(spy).toHaveBeenCalledWith('foo', undefined)
  86. vm.a = 'bar'
  87. waitForUpdate(() => {
  88. expect(spy).toHaveBeenCalledWith('bar', 'foo')
  89. }).then(done)
  90. })
  91. it('function shorthand (global)', done => {
  92. const spy = jasmine.createSpy('directive')
  93. Vue.directive('test', function (el, binding, vnode) {
  94. expect(vnode.context).toBe(vm)
  95. expect(binding.arg).toBe('arg')
  96. expect(binding.modifiers).toEqual({ hello: true })
  97. spy(binding.value, binding.oldValue)
  98. })
  99. const vm = new Vue({
  100. template: '<div v-test:arg.hello="a"></div>',
  101. data: { a: 'foo' }
  102. })
  103. vm.$mount()
  104. expect(spy).toHaveBeenCalledWith('foo', undefined)
  105. vm.a = 'bar'
  106. waitForUpdate(() => {
  107. expect(spy).toHaveBeenCalledWith('bar', 'foo')
  108. delete Vue.options.directives.test
  109. }).then(done)
  110. })
  111. it('warn non-existent', () => {
  112. new Vue({
  113. template: '<div v-test></div>'
  114. }).$mount()
  115. expect('Failed to resolve directive: test').toHaveBeenWarned()
  116. })
  117. })