if_spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.length).toBe(0)
  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 attachSpy = jasmine.createSpy()
  68. var detachSpy = jasmine.createSpy()
  69. var readySpy = jasmine.createSpy()
  70. var vm = new Vue({
  71. el: el,
  72. data: { ok: false },
  73. template: '<div v-component="test" v-if="ok"></div>',
  74. components: {
  75. test: {
  76. data: function () {
  77. return { a: 123 }
  78. },
  79. template: '{{a}}',
  80. ready: readySpy,
  81. attached: attachSpy,
  82. detached: detachSpy
  83. }
  84. }
  85. })
  86. vm.$appendTo(document.body)
  87. expect(el.innerHTML).toBe(wrap(''))
  88. expect(vm._children.length).toBe(0)
  89. vm.ok = true
  90. _.nextTick(function () {
  91. expect(el.innerHTML).toBe(wrap('<div>123</div><!--v-component-->'))
  92. expect(vm._children.length).toBe(1)
  93. expect(attachSpy).toHaveBeenCalled()
  94. expect(readySpy).toHaveBeenCalled()
  95. vm.ok = false
  96. _.nextTick(function () {
  97. expect(detachSpy).toHaveBeenCalled()
  98. expect(el.innerHTML).toBe(wrap(''))
  99. expect(vm._children.length).toBe(0)
  100. vm.$remove()
  101. done()
  102. })
  103. })
  104. })
  105. it('v-if + dynamic component', function (done) {
  106. var vm = new Vue({
  107. el: el,
  108. data: {
  109. ok: false,
  110. view: 'a'
  111. },
  112. template: '<div v-component="{{view}}" v-if="ok"></div>',
  113. components: {
  114. a: {
  115. template: 'AAA'
  116. },
  117. b: {
  118. template: 'BBB'
  119. }
  120. }
  121. })
  122. expect(el.innerHTML).toBe(wrap(''))
  123. expect(vm._children.length).toBe(0)
  124. // toggle if with lazy instantiation
  125. vm.ok = true
  126. _.nextTick(function () {
  127. expect(el.innerHTML).toBe(wrap('<div>AAA</div><!--v-component-->'))
  128. expect(vm._children.length).toBe(1)
  129. // switch view when if=true
  130. vm.view = 'b'
  131. _.nextTick(function () {
  132. expect(el.innerHTML).toBe(wrap('<div>BBB</div><!--v-component-->'))
  133. expect(vm._children.length).toBe(1)
  134. // toggle if when already instantiated
  135. vm.ok = false
  136. _.nextTick(function () {
  137. expect(el.innerHTML).toBe(wrap(''))
  138. expect(vm._children.length).toBe(0)
  139. // toggle if and switch view at the same time
  140. vm.view = 'a'
  141. vm.ok = true
  142. _.nextTick(function () {
  143. expect(el.innerHTML).toBe(wrap('<div>AAA</div><!--v-component-->'))
  144. expect(vm._children.length).toBe(1)
  145. done()
  146. })
  147. })
  148. })
  149. })
  150. })
  151. it('v-if with different truthy values', function (done) {
  152. var vm = new Vue({
  153. el: el,
  154. data: {
  155. a: 1
  156. },
  157. template: '<div v-if="a">{{a}}</div>'
  158. })
  159. expect(el.innerHTML).toBe(wrap('<div>1</div>'))
  160. vm.a = 2
  161. _.nextTick(function () {
  162. expect(el.innerHTML).toBe(wrap('<div>2</div>'))
  163. done()
  164. })
  165. })
  166. it('invalid warn', function () {
  167. el.setAttribute('v-if', 'test')
  168. var vm = new Vue({
  169. el: el
  170. })
  171. expect(_.warn).toHaveBeenCalled()
  172. })
  173. })
  174. }