index.spec.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { vi } from 'vitest'
  2. import { EMPTY_ARR } from '@vue/shared'
  3. import { createApp, ref, nextTick, reactive } from '../src'
  4. describe('compiler + runtime integration', () => {
  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: vi.fn(),
  24. mounted: vi.fn(),
  25. activated: vi.fn(),
  26. deactivated: vi.fn(),
  27. unmounted: vi.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.unmounted).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.unmounted).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.unmounted).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 via config.isCustomElement (deprecated)', () => {
  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 custom element via config.compilerOptions.isCustomElement', () => {
  133. const app = createApp({
  134. template: '<custom></custom>'
  135. })
  136. const container = document.createElement('div')
  137. app.config.compilerOptions.isCustomElement = tag => tag === 'custom'
  138. app.mount(container)
  139. expect(container.innerHTML).toBe('<custom></custom>')
  140. })
  141. it('should support using element innerHTML as template', () => {
  142. const app = createApp({
  143. data: () => ({
  144. msg: 'hello'
  145. })
  146. })
  147. const container = document.createElement('div')
  148. container.innerHTML = '{{msg}}'
  149. app.mount(container)
  150. expect(container.innerHTML).toBe('hello')
  151. })
  152. it('should support selector of rootContainer', () => {
  153. const container = document.createElement('div')
  154. const origin = document.querySelector
  155. document.querySelector = vi.fn().mockReturnValue(container)
  156. const App = {
  157. template: `{{ count }}`,
  158. data() {
  159. return {
  160. count: 0
  161. }
  162. }
  163. }
  164. createApp(App).mount('#app')
  165. expect(container.innerHTML).toBe(`0`)
  166. document.querySelector = origin
  167. })
  168. it('should warn when template is not available', () => {
  169. const app = createApp({
  170. template: {}
  171. })
  172. const container = document.createElement('div')
  173. app.mount(container)
  174. expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
  175. })
  176. it('should warn when template is is not found', () => {
  177. const app = createApp({
  178. template: '#not-exist-id'
  179. })
  180. const container = document.createElement('div')
  181. app.mount(container)
  182. expect(
  183. '[Vue warn]: Template element not found or is empty: #not-exist-id'
  184. ).toHaveBeenWarned()
  185. })
  186. it('should warn when container is not found', () => {
  187. const origin = document.querySelector
  188. document.querySelector = vi.fn().mockReturnValue(null)
  189. const App = {
  190. template: `{{ count }}`,
  191. data() {
  192. return {
  193. count: 0
  194. }
  195. }
  196. }
  197. createApp(App).mount('#not-exist-id')
  198. expect(
  199. '[Vue warn]: Failed to mount app: mount target selector "#not-exist-id" returned null.'
  200. ).toHaveBeenWarned()
  201. document.querySelector = origin
  202. })
  203. // #1813
  204. it('should not report an error when "0" as patchFlag value', async () => {
  205. const container = document.createElement('div')
  206. const target = document.createElement('div')
  207. const count = ref(0)
  208. const origin = document.querySelector
  209. document.querySelector = vi.fn().mockReturnValue(target)
  210. const App = {
  211. template: `
  212. <teleport v-if="count < 2" to="#target">
  213. <div>
  214. <div>{{ count }}</div>
  215. </div>
  216. </teleport>
  217. `,
  218. data() {
  219. return {
  220. count
  221. }
  222. }
  223. }
  224. createApp(App).mount(container)
  225. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  226. expect(target.innerHTML).toBe(`<div><div>0</div></div>`)
  227. count.value++
  228. await nextTick()
  229. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  230. expect(target.innerHTML).toBe(`<div><div>1</div></div>`)
  231. count.value++
  232. await nextTick()
  233. expect(container.innerHTML).toBe(`<!--v-if-->`)
  234. expect(target.innerHTML).toBe(``)
  235. document.querySelector = origin
  236. })
  237. test('v-if + v-once', async () => {
  238. const ok = ref(true)
  239. const App = {
  240. setup() {
  241. return { ok }
  242. },
  243. template: `<div>{{ ok }}<div v-if="ok" v-once>{{ ok }}</div></div>`
  244. }
  245. const container = document.createElement('div')
  246. createApp(App).mount(container)
  247. expect(container.innerHTML).toBe(`<div>true<div>true</div></div>`)
  248. ok.value = false
  249. await nextTick()
  250. expect(container.innerHTML).toBe(`<div>false<div>true</div></div>`)
  251. })
  252. test('v-for + v-once', async () => {
  253. const list = reactive([1])
  254. const App = {
  255. setup() {
  256. return { list }
  257. },
  258. template: `<div>{{ list.length }}<div v-for="i in list" v-once>{{ i }}</div></div>`
  259. }
  260. const container = document.createElement('div')
  261. createApp(App).mount(container)
  262. expect(container.innerHTML).toBe(`<div>1<div>1</div></div>`)
  263. list.push(2)
  264. await nextTick()
  265. expect(container.innerHTML).toBe(`<div>2<div>1</div></div>`)
  266. })
  267. // #2413
  268. it('EMPTY_ARR should not change', () => {
  269. const App = {
  270. template: `<div v-for="v of ['a']">{{ v }}</div>`
  271. }
  272. const container = document.createElement('div')
  273. createApp(App).mount(container)
  274. expect(EMPTY_ARR.length).toBe(0)
  275. })
  276. test('BigInt support', () => {
  277. const app = createApp({
  278. template: `<div>{{ BigInt(BigInt(100000111)) + BigInt(2000000000n) * 30000000n }}</div>`
  279. })
  280. const root = document.createElement('div')
  281. app.mount(root)
  282. expect(root.innerHTML).toBe('<div>60000000100000111</div>')
  283. })
  284. })