directives.spec.js 2.9 KB

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