ref_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-ref', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. var components = {
  11. test: {
  12. id: 'test'
  13. },
  14. test2: {
  15. id: 'test2'
  16. }
  17. }
  18. it('normal', function () {
  19. var vm = new Vue({
  20. el: el,
  21. components: components,
  22. template: '<div v-component="test" v-ref="test"></div>'
  23. })
  24. expect(vm.$.test).toBeTruthy()
  25. expect(vm.$.test.$options.id).toBe('test')
  26. })
  27. it('with dynamic v-component', function (done) {
  28. var vm = new Vue({
  29. el: el,
  30. components: components,
  31. data: { test: 'test' },
  32. template: '<div v-component="{{test}}" v-ref="test"></div>'
  33. })
  34. expect(vm.$.test.$options.id).toBe('test')
  35. vm.test = 'test2'
  36. _.nextTick(function () {
  37. expect(vm.$.test.$options.id).toBe('test2')
  38. vm.test = ''
  39. _.nextTick(function () {
  40. expect(vm.$.test).toBeNull()
  41. done()
  42. })
  43. })
  44. })
  45. it('should also work in child template', function (done) {
  46. var vm = new Vue({
  47. el: el,
  48. data: { view: 'test1' },
  49. template: '<div v-component="{{view}}"></div>',
  50. components: {
  51. test1: {
  52. id: 'test1',
  53. template: '<div v-ref="test1"></div>',
  54. replace: true
  55. },
  56. test2: {
  57. id: 'test2',
  58. template: '<div v-ref="test2"></div>',
  59. replace: true
  60. }
  61. }
  62. })
  63. expect(vm.$.test1.$options.id).toBe('test1')
  64. vm.view = 'test2'
  65. _.nextTick(function () {
  66. expect(vm.$.test1).toBeNull()
  67. expect(vm.$.test2.$options.id).toBe('test2')
  68. done()
  69. })
  70. })
  71. it('with v-repeat', function (done) {
  72. var vm = new Vue({
  73. el: el,
  74. data: { items: [1,2,3,4,5] },
  75. template: '<div v-repeat="items" v-ref="test"></div>'
  76. })
  77. expect(vm.$.test).toBeTruthy()
  78. expect(Array.isArray(vm.$.test)).toBe(true)
  79. expect(vm.$.test[0].$value).toBe(1)
  80. expect(vm.$.test[4].$value).toBe(5)
  81. vm.items = []
  82. _.nextTick(function () {
  83. expect(vm.$.test.length).toBe(0)
  84. vm._directives[0].unbind()
  85. expect(vm.$.test).toBeNull()
  86. done()
  87. })
  88. })
  89. it('nested v-repeat', function () {
  90. var vm = new Vue({
  91. el: el,
  92. template: '<div v-component="c1" v-ref="c1"></div>',
  93. components: {
  94. c1: {
  95. template: '<div v-repeat="2" v-ref="c2"></div>'
  96. }
  97. }
  98. })
  99. expect(vm.$.c1 instanceof Vue).toBe(true)
  100. expect(vm.$.c2).toBeUndefined()
  101. expect(Array.isArray(vm.$.c1.$.c2)).toBe(true)
  102. expect(vm.$.c1.$.c2.length).toBe(2)
  103. })
  104. it('should warn on non-root', function () {
  105. var vm = new Vue({
  106. el: el,
  107. template: '<div v-ref="test"></div>'
  108. })
  109. expect(_.warn).toHaveBeenCalled()
  110. })
  111. })
  112. }