ref_spec.js 3.9 KB

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