ref_spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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: '<test v-ref="test"></test>'
  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: '<component is="{{test}}" v-ref="test"></component>'
  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: '<component is="{{view}}"></component>',
  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('object v-repeat', function (done) {
  90. var vm = new Vue({
  91. el: el,
  92. data: {
  93. items: {
  94. a: 1,
  95. b: 2
  96. }
  97. },
  98. template: '<div v-repeat="items" v-ref="test"></div>'
  99. })
  100. expect(vm.$.test).toBeTruthy()
  101. expect(_.isPlainObject(vm.$.test)).toBe(true)
  102. expect(vm.$.test.a.$value).toBe(1)
  103. expect(vm.$.test.b.$value).toBe(2)
  104. vm.items = { c: 3 }
  105. _.nextTick(function () {
  106. expect(Object.keys(vm.$.test).length).toBe(1)
  107. expect(vm.$.test.c.$value).toBe(3)
  108. vm._directives[0].unbind()
  109. expect(vm.$.test).toBeNull()
  110. done()
  111. })
  112. })
  113. it('nested v-repeat', function () {
  114. var vm = new Vue({
  115. el: el,
  116. template: '<c1 v-ref="c1"></c1>',
  117. components: {
  118. c1: {
  119. template: '<div v-repeat="2" v-ref="c2"></div>'
  120. }
  121. }
  122. })
  123. expect(vm.$.c1 instanceof Vue).toBe(true)
  124. expect(vm.$.c2).toBeUndefined()
  125. expect(Array.isArray(vm.$.c1.$.c2)).toBe(true)
  126. expect(vm.$.c1.$.c2.length).toBe(2)
  127. })
  128. it('should warn on non-root', function () {
  129. var vm = new Vue({
  130. el: el,
  131. template: '<div v-ref="test"></div>'
  132. })
  133. expect(hasWarned(_, 'should only be used on a component root element')).toBe(true)
  134. })
  135. })
  136. }