directives.spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.value).toBe('bar')
  36. expect(binding.oldValue).toBe('foo')
  37. expect(binding.expression).toBe('a')
  38. },
  39. componentUpdated (el, binding, vnode) {
  40. componentUpdatedSpy()
  41. assertContext(el, binding, vnode)
  42. },
  43. unbind (el, binding, vnode) {
  44. unbindSpy()
  45. assertContext(el, binding, vnode)
  46. }
  47. }
  48. }
  49. })
  50. vm.$mount()
  51. expect(bindSpy).toHaveBeenCalled()
  52. expect(updateSpy).not.toHaveBeenCalled()
  53. expect(componentUpdatedSpy).not.toHaveBeenCalled()
  54. expect(unbindSpy).not.toHaveBeenCalled()
  55. vm.a = 'bar'
  56. waitForUpdate(() => {
  57. expect(updateSpy).toHaveBeenCalled()
  58. expect(componentUpdatedSpy).toHaveBeenCalled()
  59. expect(unbindSpy).not.toHaveBeenCalled()
  60. vm.msg = 'bye'
  61. }).then(() => {
  62. expect(componentUpdatedSpy.calls.count()).toBe(2)
  63. vm.ok = false
  64. }).then(() => {
  65. expect(unbindSpy).toHaveBeenCalled()
  66. }).then(done)
  67. })
  68. it('function shorthand', done => {
  69. const spy = jasmine.createSpy('directive')
  70. const vm = new Vue({
  71. template: '<div v-test:arg.hello="a"></div>',
  72. data: { a: 'foo' },
  73. directives: {
  74. test (el, binding, vnode) {
  75. expect(vnode.context).toBe(vm)
  76. expect(binding.arg).toBe('arg')
  77. expect(binding.modifiers).toEqual({ hello: true })
  78. spy(binding.value, binding.oldValue)
  79. }
  80. }
  81. })
  82. vm.$mount()
  83. expect(spy).toHaveBeenCalledWith('foo', undefined)
  84. vm.a = 'bar'
  85. waitForUpdate(() => {
  86. expect(spy).toHaveBeenCalledWith('bar', 'foo')
  87. }).then(done)
  88. })
  89. it('warn non-existent', () => {
  90. new Vue({
  91. template: '<div v-test></div>'
  92. }).$mount()
  93. expect('Failed to resolve directive: test').toHaveBeenWarned()
  94. })
  95. })