2
0

ref_spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. data: {
  23. ref: 'test2'
  24. },
  25. template: '<test ref="test"></test><test2 bind-ref="ref"></test2>'
  26. })
  27. expect(vm.$.test).toBeTruthy()
  28. expect(vm.$.test.$options.id).toBe('test')
  29. expect(vm.$.test2).toBeTruthy()
  30. expect(vm.$.test2.$options.id).toBe('test2')
  31. })
  32. it('with dynamic v-component', function (done) {
  33. var vm = new Vue({
  34. el: el,
  35. components: components,
  36. data: { test: 'test' },
  37. template: '<component bind-is="test" ref="test"></component>'
  38. })
  39. expect(vm.$.test.$options.id).toBe('test')
  40. vm.test = 'test2'
  41. _.nextTick(function () {
  42. expect(vm.$.test.$options.id).toBe('test2')
  43. vm.test = ''
  44. _.nextTick(function () {
  45. expect(vm.$.test).toBeNull()
  46. done()
  47. })
  48. })
  49. })
  50. it('should be reactive when bound by dynamic component and hoisted', function (done) {
  51. var vm = new Vue({
  52. el: el,
  53. data: { view: 'one' },
  54. template: '{{$.test.value}}<component bind-is="view" ref="test"></component>',
  55. components: {
  56. one: {
  57. id: 'one',
  58. replace: true,
  59. data: function () {
  60. return { value: 1 }
  61. }
  62. },
  63. two: {
  64. id: 'two',
  65. replace: true,
  66. data: function () {
  67. return { value: 2 }
  68. }
  69. }
  70. }
  71. })
  72. expect(vm.$.test.$options.id).toBe('one')
  73. expect(el.textContent).toBe('1')
  74. vm.view = 'two'
  75. _.nextTick(function () {
  76. expect(vm.$.test.$options.id).toBe('two')
  77. expect(el.textContent).toBe('2')
  78. vm.view = ''
  79. _.nextTick(function () {
  80. expect(vm.$.test).toBeNull()
  81. expect(el.textContent).toBe('')
  82. done()
  83. })
  84. })
  85. })
  86. // #1147
  87. it('should be able to reference host via ref inside transclusion content', function (done) {
  88. var vm = new Vue({
  89. el: el,
  90. template:
  91. '<div>' +
  92. '<comp ref="out">{{$.out.msg}}</comp>' +
  93. '</div>',
  94. components: {
  95. comp: {
  96. template: '<slot></slot>',
  97. data: function () {
  98. return { msg: 'hi' }
  99. }
  100. }
  101. }
  102. })
  103. expect(_.warn).not.toHaveBeenCalled()
  104. expect(vm.$el.textContent).toBe('hi')
  105. vm.$children[0].msg = 'ho'
  106. _.nextTick(function () {
  107. expect(vm.$el.textContent).toBe('ho')
  108. done()
  109. })
  110. })
  111. it('should also work in child template', function (done) {
  112. var vm = new Vue({
  113. el: el,
  114. data: { view: 'test1' },
  115. template: '<component is="{{view}}"></component>',
  116. components: {
  117. test1: {
  118. id: 'test1',
  119. template: '<div v-ref="test1"></div>',
  120. replace: true
  121. },
  122. test2: {
  123. id: 'test2',
  124. template: '<div v-ref="test2"></div>',
  125. replace: true
  126. }
  127. }
  128. })
  129. expect(vm.$.test1.$options.id).toBe('test1')
  130. vm.view = 'test2'
  131. _.nextTick(function () {
  132. expect(vm.$.test1).toBeNull()
  133. expect(vm.$.test2.$options.id).toBe('test2')
  134. done()
  135. })
  136. })
  137. it('should warn on non-root', function () {
  138. new Vue({
  139. el: el,
  140. template: '<div v-ref="test"></div>'
  141. })
  142. expect(hasWarned(_, 'should only be used on a component root element')).toBe(true)
  143. })
  144. })
  145. }