if_spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-if', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. function wrap (content) {
  11. return '<!--v-if-start-->' + content + '<!--v-if-end-->'
  12. }
  13. it('normal', function (done) {
  14. var vm = new Vue({
  15. el: el,
  16. data: { test: false, a: 'A' },
  17. template: '<div v-if="test"><div v-component="test"></div></div>',
  18. components: {
  19. test: {
  20. inherit: true,
  21. template: '{{a}}'
  22. }
  23. }
  24. })
  25. // lazy instantitation
  26. expect(el.innerHTML).toBe(wrap(''))
  27. expect(vm._children).toBeNull()
  28. vm.test = true
  29. _.nextTick(function () {
  30. expect(el.innerHTML).toBe(wrap('<div><div>A</div><!--v-component--></div>'))
  31. expect(vm._children.length).toBe(1)
  32. vm.test = false
  33. _.nextTick(function () {
  34. expect(el.innerHTML).toBe(wrap(''))
  35. expect(vm._children.length).toBe(0)
  36. vm.test = true
  37. _.nextTick(function () {
  38. expect(el.innerHTML).toBe(wrap('<div><div>A</div><!--v-component--></div>'))
  39. expect(vm._children.length).toBe(1)
  40. var child = vm._children[0]
  41. vm.$destroy()
  42. expect(child._isDestroyed).toBe(true)
  43. done()
  44. })
  45. })
  46. })
  47. })
  48. it('template block', function (done) {
  49. var vm = new Vue({
  50. el: el,
  51. data: { test: false, a: 'A', b: 'B' },
  52. template: '<template v-if="test"><p>{{a}}</p><p>{{b}}</p></template>'
  53. })
  54. // lazy instantitation
  55. expect(el.innerHTML).toBe(wrap(''))
  56. vm.test = true
  57. _.nextTick(function () {
  58. expect(el.innerHTML).toBe(wrap('<p>A</p><p>B</p>'))
  59. vm.test = false
  60. _.nextTick(function () {
  61. expect(el.innerHTML).toBe(wrap(''))
  62. done()
  63. })
  64. })
  65. })
  66. it('v-if + v-component', function (done) {
  67. var vm = new Vue({
  68. el: el,
  69. data: { ok: false },
  70. template: '<div v-component="test" v-if="ok"></div>',
  71. components: {
  72. test: {
  73. data: function () {
  74. return { a: 123 }
  75. },
  76. template: '{{a}}'
  77. }
  78. }
  79. })
  80. expect(el.innerHTML).toBe(wrap(''))
  81. expect(vm._children).toBeNull()
  82. vm.ok = true
  83. _.nextTick(function () {
  84. expect(el.innerHTML).toBe(wrap('<div>123</div><!--v-component-->'))
  85. expect(vm._children.length).toBe(1)
  86. vm.ok = false
  87. _.nextTick(function () {
  88. expect(el.innerHTML).toBe(wrap(''))
  89. expect(vm._children.length).toBe(0)
  90. done()
  91. })
  92. })
  93. })
  94. it('v-if + dynamic component', function (done) {
  95. var vm = new Vue({
  96. el: el,
  97. data: {
  98. ok: false,
  99. view: 'a'
  100. },
  101. template: '<div v-component="{{view}}" v-if="ok"></div>',
  102. components: {
  103. a: {
  104. template: 'AAA'
  105. },
  106. b: {
  107. template: 'BBB'
  108. }
  109. }
  110. })
  111. expect(el.innerHTML).toBe(wrap(''))
  112. expect(vm._children).toBeNull()
  113. // toggle if with lazy instantiation
  114. vm.ok = true
  115. _.nextTick(function () {
  116. expect(el.innerHTML).toBe(wrap('<div>AAA</div><!--v-component-->'))
  117. expect(vm._children.length).toBe(1)
  118. // switch view when if=true
  119. vm.view = 'b'
  120. _.nextTick(function () {
  121. expect(el.innerHTML).toBe(wrap('<div>BBB</div><!--v-component-->'))
  122. expect(vm._children.length).toBe(1)
  123. // toggle if when already instantiated
  124. vm.ok = false
  125. _.nextTick(function () {
  126. expect(el.innerHTML).toBe(wrap(''))
  127. expect(vm._children.length).toBe(0)
  128. // toggle if and switch view at the same time
  129. vm.view = 'a'
  130. vm.ok = true
  131. _.nextTick(function () {
  132. expect(el.innerHTML).toBe(wrap('<div>AAA</div><!--v-component-->'))
  133. expect(vm._children.length).toBe(1)
  134. done()
  135. })
  136. })
  137. })
  138. })
  139. })
  140. it('invalid warn', function () {
  141. el.setAttribute('v-if', 'test')
  142. var vm = new Vue({
  143. el: el
  144. })
  145. expect(_.warn).toHaveBeenCalled()
  146. })
  147. })
  148. }