index.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { createApp } 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('should support runtime template via CSS ID selector', () => {
  19. const container = document.createElement('div')
  20. const template = document.createElement('div')
  21. template.id = 'template'
  22. template.innerHTML = '{{ count }}'
  23. document.body.appendChild(template)
  24. const App = {
  25. template: `#template`,
  26. data() {
  27. return {
  28. count: 0
  29. }
  30. }
  31. }
  32. createApp(App).mount(container)
  33. expect(container.innerHTML).toBe(`0`)
  34. })
  35. it('should support runtime template via direct DOM node', () => {
  36. const container = document.createElement('div')
  37. const template = document.createElement('div')
  38. template.id = 'template'
  39. template.innerHTML = '{{ count }}'
  40. const App = {
  41. template,
  42. data() {
  43. return {
  44. count: 0
  45. }
  46. }
  47. }
  48. createApp(App).mount(container)
  49. expect(container.innerHTML).toBe(`0`)
  50. })
  51. it('should warn template compilation errors with codeframe', () => {
  52. const container = document.createElement('div')
  53. const App = {
  54. template: `<div v-if>`
  55. }
  56. createApp(App).mount(container)
  57. expect(
  58. `Template compilation error: Element is missing end tag`
  59. ).toHaveBeenWarned()
  60. expect(
  61. `
  62. 1 | <div v-if>
  63. | ^`.trim()
  64. ).toHaveBeenWarned()
  65. expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
  66. expect(
  67. `
  68. 1 | <div v-if>
  69. | ^^^^`.trim()
  70. ).toHaveBeenWarned()
  71. })
  72. it('should support custom element', () => {
  73. const app = createApp({
  74. template: '<custom></custom>'
  75. })
  76. const container = document.createElement('div')
  77. app.config.isCustomElement = tag => tag === 'custom'
  78. app.mount(container)
  79. expect(container.innerHTML).toBe('<custom></custom>')
  80. })
  81. it('should support using element innerHTML as template', () => {
  82. const app = createApp({
  83. data: () => ({
  84. msg: 'hello'
  85. })
  86. })
  87. const container = document.createElement('div')
  88. container.innerHTML = '{{msg}}'
  89. app.mount(container)
  90. expect(container.innerHTML).toBe('hello')
  91. })
  92. it('should support selector of rootContainer', () => {
  93. const container = document.createElement('div')
  94. const origin = document.querySelector
  95. document.querySelector = jest.fn().mockReturnValue(container)
  96. const App = {
  97. template: `{{ count }}`,
  98. data() {
  99. return {
  100. count: 0
  101. }
  102. }
  103. }
  104. createApp(App).mount('#app')
  105. expect(container.innerHTML).toBe(`0`)
  106. document.querySelector = origin
  107. })
  108. it('should warn when template is not available', () => {
  109. const app = createApp({
  110. template: {}
  111. })
  112. const container = document.createElement('div')
  113. app.mount(container)
  114. expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
  115. })
  116. it('should warn when template is is not found', () => {
  117. const app = createApp({
  118. template: '#not-exist-id'
  119. })
  120. const container = document.createElement('div')
  121. app.mount(container)
  122. expect(
  123. '[Vue warn]: Template element not found or is empty: #not-exist-id'
  124. ).toHaveBeenWarned()
  125. })
  126. it('should warn when container is not found', () => {
  127. const origin = document.querySelector
  128. document.querySelector = jest.fn().mockReturnValue(null)
  129. const App = {
  130. template: `{{ count }}`,
  131. data() {
  132. return {
  133. count: 0
  134. }
  135. }
  136. }
  137. createApp(App).mount('#not-exist-id')
  138. expect(
  139. '[Vue warn]: Failed to mount app: mount target selector returned null.'
  140. ).toHaveBeenWarned()
  141. document.querySelector = origin
  142. })
  143. })