index.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { createApp, ref, nextTick } from '../src'
  2. describe('compiler + runtime integration', () => {
  3. it('should support runtime template compilation', () => {
  4. const container = document.createElement('div')
  5. const App = {
  6. template: `{{ count }}`,
  7. data() {
  8. return {
  9. count: 0
  10. }
  11. }
  12. }
  13. createApp(App).mount(container)
  14. expect(container.innerHTML).toBe(`0`)
  15. })
  16. it('keep-alive with compiler + runtime integration', async () => {
  17. const container = document.createElement('div')
  18. const one = {
  19. name: 'one',
  20. template: 'one',
  21. created: jest.fn(),
  22. mounted: jest.fn(),
  23. activated: jest.fn(),
  24. deactivated: jest.fn(),
  25. destroyed: jest.fn()
  26. }
  27. const toggle = ref(true)
  28. const App = {
  29. template: `
  30. <keep-alive>
  31. <one v-if="toggle"></one>
  32. </keep-alive>
  33. `,
  34. data() {
  35. return {
  36. toggle
  37. }
  38. },
  39. components: {
  40. One: one
  41. }
  42. }
  43. createApp(App).mount(container)
  44. expect(container.innerHTML).toBe(`one`)
  45. expect(one.created).toHaveBeenCalledTimes(1)
  46. expect(one.mounted).toHaveBeenCalledTimes(1)
  47. expect(one.activated).toHaveBeenCalledTimes(1)
  48. expect(one.deactivated).toHaveBeenCalledTimes(0)
  49. expect(one.destroyed).toHaveBeenCalledTimes(0)
  50. toggle.value = false
  51. await nextTick()
  52. expect(container.innerHTML).toBe(`<!--v-if-->`)
  53. expect(one.created).toHaveBeenCalledTimes(1)
  54. expect(one.mounted).toHaveBeenCalledTimes(1)
  55. expect(one.activated).toHaveBeenCalledTimes(1)
  56. expect(one.deactivated).toHaveBeenCalledTimes(1)
  57. expect(one.destroyed).toHaveBeenCalledTimes(0)
  58. toggle.value = true
  59. await nextTick()
  60. expect(container.innerHTML).toBe(`one`)
  61. expect(one.created).toHaveBeenCalledTimes(1)
  62. expect(one.mounted).toHaveBeenCalledTimes(1)
  63. expect(one.activated).toHaveBeenCalledTimes(2)
  64. expect(one.deactivated).toHaveBeenCalledTimes(1)
  65. expect(one.destroyed).toHaveBeenCalledTimes(0)
  66. })
  67. it('should support runtime template via CSS ID selector', () => {
  68. const container = document.createElement('div')
  69. const template = document.createElement('div')
  70. template.id = 'template'
  71. template.innerHTML = '{{ count }}'
  72. document.body.appendChild(template)
  73. const App = {
  74. template: `#template`,
  75. data() {
  76. return {
  77. count: 0
  78. }
  79. }
  80. }
  81. createApp(App).mount(container)
  82. expect(container.innerHTML).toBe(`0`)
  83. })
  84. it('should support runtime template via direct DOM node', () => {
  85. const container = document.createElement('div')
  86. const template = document.createElement('div')
  87. template.id = 'template'
  88. template.innerHTML = '{{ count }}'
  89. const App = {
  90. template,
  91. data() {
  92. return {
  93. count: 0
  94. }
  95. }
  96. }
  97. createApp(App).mount(container)
  98. expect(container.innerHTML).toBe(`0`)
  99. })
  100. it('should warn template compilation errors with codeframe', () => {
  101. const container = document.createElement('div')
  102. const App = {
  103. template: `<div v-if>`
  104. }
  105. createApp(App).mount(container)
  106. expect(
  107. `Template compilation error: Element is missing end tag`
  108. ).toHaveBeenWarned()
  109. expect(
  110. `
  111. 1 | <div v-if>
  112. | ^`.trim()
  113. ).toHaveBeenWarned()
  114. expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
  115. expect(
  116. `
  117. 1 | <div v-if>
  118. | ^^^^`.trim()
  119. ).toHaveBeenWarned()
  120. })
  121. it('should support custom element', () => {
  122. const app = createApp({
  123. template: '<custom></custom>'
  124. })
  125. const container = document.createElement('div')
  126. app.config.isCustomElement = tag => tag === 'custom'
  127. app.mount(container)
  128. expect(container.innerHTML).toBe('<custom></custom>')
  129. })
  130. it('should support using element innerHTML as template', () => {
  131. const app = createApp({
  132. data: () => ({
  133. msg: 'hello'
  134. })
  135. })
  136. const container = document.createElement('div')
  137. container.innerHTML = '{{msg}}'
  138. app.mount(container)
  139. expect(container.innerHTML).toBe('hello')
  140. })
  141. it('should support selector of rootContainer', () => {
  142. const container = document.createElement('div')
  143. const origin = document.querySelector
  144. document.querySelector = jest.fn().mockReturnValue(container)
  145. const App = {
  146. template: `{{ count }}`,
  147. data() {
  148. return {
  149. count: 0
  150. }
  151. }
  152. }
  153. createApp(App).mount('#app')
  154. expect(container.innerHTML).toBe(`0`)
  155. document.querySelector = origin
  156. })
  157. it('should warn when template is not available', () => {
  158. const app = createApp({
  159. template: {}
  160. })
  161. const container = document.createElement('div')
  162. app.mount(container)
  163. expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
  164. })
  165. it('should warn when template is is not found', () => {
  166. const app = createApp({
  167. template: '#not-exist-id'
  168. })
  169. const container = document.createElement('div')
  170. app.mount(container)
  171. expect(
  172. '[Vue warn]: Template element not found or is empty: #not-exist-id'
  173. ).toHaveBeenWarned()
  174. })
  175. it('should warn when container is not found', () => {
  176. const origin = document.querySelector
  177. document.querySelector = jest.fn().mockReturnValue(null)
  178. const App = {
  179. template: `{{ count }}`,
  180. data() {
  181. return {
  182. count: 0
  183. }
  184. }
  185. }
  186. createApp(App).mount('#not-exist-id')
  187. expect(
  188. '[Vue warn]: Failed to mount app: mount target selector returned null.'
  189. ).toHaveBeenWarned()
  190. document.querySelector = origin
  191. })
  192. // #1813
  193. it('should not report an error when "0" as patchFlag value', async () => {
  194. const container = document.createElement('div')
  195. const target = document.createElement('div')
  196. const count = ref(0)
  197. const origin = document.querySelector
  198. document.querySelector = jest.fn().mockReturnValue(target)
  199. const App = {
  200. template: `
  201. <teleport v-if="count < 2" to="#target">
  202. <div>
  203. <div>{{ count }}</div>
  204. </div>
  205. </teleport>
  206. `,
  207. data() {
  208. return {
  209. count
  210. }
  211. }
  212. }
  213. createApp(App).mount(container)
  214. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  215. expect(target.innerHTML).toBe(`<div><div>0</div></div>`)
  216. count.value++
  217. await nextTick()
  218. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  219. expect(target.innerHTML).toBe(`<div><div>1</div></div>`)
  220. count.value++
  221. await nextTick()
  222. expect(container.innerHTML).toBe(`<!--v-if-->`)
  223. expect(target.innerHTML).toBe(``)
  224. document.querySelector = origin
  225. })
  226. })