ref_spec.js 3.5 KB

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