directives.spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import Vue from 'vue'
  2. describe('Options directives', () => {
  3. it('basic usage', done => {
  4. const bindSpy = jasmine.createSpy('bind')
  5. const insertedSpy = jasmine.createSpy('inserted')
  6. const updateSpy = jasmine.createSpy('update')
  7. const componentUpdatedSpy = jasmine.createSpy('componentUpdated')
  8. const unbindSpy = jasmine.createSpy('unbind')
  9. const assertContext = (el, binding, vnode) => {
  10. expect(vnode.context).toBe(vm)
  11. expect(binding.arg).toBe('arg')
  12. expect(binding.modifiers).toEqual({ hello: true })
  13. }
  14. const vm = new Vue({
  15. template: '<div class="hi"><div v-if="ok" v-test:arg.hello="a">{{ msg }}</div></div>',
  16. data: {
  17. msg: 'hi',
  18. a: 'foo',
  19. ok: true
  20. },
  21. directives: {
  22. test: {
  23. bind (el, binding, vnode) {
  24. bindSpy()
  25. assertContext(el, binding, vnode)
  26. expect(binding.value).toBe('foo')
  27. expect(binding.expression).toBe('a')
  28. expect(binding.oldValue).toBeUndefined()
  29. expect(el.parentNode).toBeNull()
  30. },
  31. inserted (el, binding, vnode) {
  32. insertedSpy()
  33. assertContext(el, binding, vnode)
  34. expect(binding.value).toBe('foo')
  35. expect(binding.expression).toBe('a')
  36. expect(binding.oldValue).toBeUndefined()
  37. expect(el.parentNode.className).toBe('hi')
  38. },
  39. update (el, binding, vnode, oldVnode) {
  40. updateSpy()
  41. assertContext(el, binding, vnode)
  42. expect(el).toBe(vm.$el.children[0])
  43. expect(oldVnode).not.toBe(vnode)
  44. expect(binding.expression).toBe('a')
  45. if (binding.value !== binding.oldValue) {
  46. expect(binding.value).toBe('bar')
  47. expect(binding.oldValue).toBe('foo')
  48. }
  49. },
  50. componentUpdated (el, binding, vnode) {
  51. componentUpdatedSpy()
  52. assertContext(el, binding, vnode)
  53. },
  54. unbind (el, binding, vnode) {
  55. unbindSpy()
  56. assertContext(el, binding, vnode)
  57. }
  58. }
  59. }
  60. })
  61. vm.$mount()
  62. expect(bindSpy).toHaveBeenCalled()
  63. expect(insertedSpy).toHaveBeenCalled()
  64. expect(updateSpy).not.toHaveBeenCalled()
  65. expect(componentUpdatedSpy).not.toHaveBeenCalled()
  66. expect(unbindSpy).not.toHaveBeenCalled()
  67. vm.a = 'bar'
  68. waitForUpdate(() => {
  69. expect(updateSpy).toHaveBeenCalled()
  70. expect(componentUpdatedSpy).toHaveBeenCalled()
  71. expect(unbindSpy).not.toHaveBeenCalled()
  72. vm.msg = 'bye'
  73. }).then(() => {
  74. expect(componentUpdatedSpy.calls.count()).toBe(2)
  75. vm.ok = false
  76. }).then(() => {
  77. expect(unbindSpy).toHaveBeenCalled()
  78. }).then(done)
  79. })
  80. it('function shorthand', done => {
  81. const spy = jasmine.createSpy('directive')
  82. const vm = new Vue({
  83. template: '<div v-test:arg.hello="a"></div>',
  84. data: { a: 'foo' },
  85. directives: {
  86. test (el, binding, vnode) {
  87. expect(vnode.context).toBe(vm)
  88. expect(binding.arg).toBe('arg')
  89. expect(binding.modifiers).toEqual({ hello: true })
  90. spy(binding.value, binding.oldValue)
  91. }
  92. }
  93. })
  94. vm.$mount()
  95. expect(spy).toHaveBeenCalledWith('foo', undefined)
  96. vm.a = 'bar'
  97. waitForUpdate(() => {
  98. expect(spy).toHaveBeenCalledWith('bar', 'foo')
  99. }).then(done)
  100. })
  101. it('function shorthand (global)', done => {
  102. const spy = jasmine.createSpy('directive')
  103. Vue.directive('test', function (el, binding, vnode) {
  104. expect(vnode.context).toBe(vm)
  105. expect(binding.arg).toBe('arg')
  106. expect(binding.modifiers).toEqual({ hello: true })
  107. spy(binding.value, binding.oldValue)
  108. })
  109. const vm = new Vue({
  110. template: '<div v-test:arg.hello="a"></div>',
  111. data: { a: 'foo' }
  112. })
  113. vm.$mount()
  114. expect(spy).toHaveBeenCalledWith('foo', undefined)
  115. vm.a = 'bar'
  116. waitForUpdate(() => {
  117. expect(spy).toHaveBeenCalledWith('bar', 'foo')
  118. delete Vue.options.directives.test
  119. }).then(done)
  120. })
  121. it('should teardown directives on old vnodes when new vnodes have none', done => {
  122. const vm = new Vue({
  123. data: {
  124. ok: true
  125. },
  126. template: `
  127. <div>
  128. <div v-if="ok" v-test>a</div>
  129. <div v-else class="b">b</div>
  130. </div>
  131. `,
  132. directives: {
  133. test: {
  134. bind: el => { el.id = 'a' },
  135. unbind: el => { el.id = '' }
  136. }
  137. }
  138. }).$mount()
  139. expect(vm.$el.children[0].id).toBe('a')
  140. vm.ok = false
  141. waitForUpdate(() => {
  142. expect(vm.$el.children[0].id).toBe('')
  143. expect(vm.$el.children[0].className).toBe('b')
  144. }).then(done)
  145. })
  146. it('warn non-existent', () => {
  147. new Vue({
  148. template: '<div v-test></div>'
  149. }).$mount()
  150. expect('Failed to resolve directive: test').toHaveBeenWarned()
  151. })
  152. })