index.spec.ts 5.6 KB

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