index.spec.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { EMPTY_ARR } from '@vue/shared'
  2. import { createApp, nextTick, reactive, ref } from '../src'
  3. describe('compiler + runtime integration', () => {
  4. it('should support runtime template compilation', () => {
  5. const container = document.createElement('div')
  6. const App = {
  7. template: `{{ count }}`,
  8. data() {
  9. return {
  10. count: 0,
  11. }
  12. },
  13. }
  14. createApp(App).mount(container)
  15. expect(container.innerHTML).toBe(`0`)
  16. })
  17. it('keep-alive with compiler + runtime integration', async () => {
  18. const container = document.createElement('div')
  19. const one = {
  20. name: 'one',
  21. template: 'one',
  22. created: vi.fn(),
  23. mounted: vi.fn(),
  24. activated: vi.fn(),
  25. deactivated: vi.fn(),
  26. unmounted: vi.fn(),
  27. }
  28. const toggle = ref(true)
  29. const App = {
  30. template: `
  31. <keep-alive>
  32. <one v-if="toggle"></one>
  33. </keep-alive>
  34. `,
  35. data() {
  36. return {
  37. toggle,
  38. }
  39. },
  40. components: {
  41. One: one,
  42. },
  43. }
  44. createApp(App).mount(container)
  45. expect(container.innerHTML).toBe(`one`)
  46. expect(one.created).toHaveBeenCalledTimes(1)
  47. expect(one.mounted).toHaveBeenCalledTimes(1)
  48. expect(one.activated).toHaveBeenCalledTimes(1)
  49. expect(one.deactivated).toHaveBeenCalledTimes(0)
  50. expect(one.unmounted).toHaveBeenCalledTimes(0)
  51. toggle.value = false
  52. await nextTick()
  53. expect(container.innerHTML).toBe(`<!--v-if-->`)
  54. expect(one.created).toHaveBeenCalledTimes(1)
  55. expect(one.mounted).toHaveBeenCalledTimes(1)
  56. expect(one.activated).toHaveBeenCalledTimes(1)
  57. expect(one.deactivated).toHaveBeenCalledTimes(1)
  58. expect(one.unmounted).toHaveBeenCalledTimes(0)
  59. toggle.value = true
  60. await nextTick()
  61. expect(container.innerHTML).toBe(`one`)
  62. expect(one.created).toHaveBeenCalledTimes(1)
  63. expect(one.mounted).toHaveBeenCalledTimes(1)
  64. expect(one.activated).toHaveBeenCalledTimes(2)
  65. expect(one.deactivated).toHaveBeenCalledTimes(1)
  66. expect(one.unmounted).toHaveBeenCalledTimes(0)
  67. })
  68. it('should support runtime template via CSS ID selector', () => {
  69. const container = document.createElement('div')
  70. const template = document.createElement('div')
  71. template.id = 'template'
  72. template.innerHTML = '{{ count }}'
  73. document.body.appendChild(template)
  74. const App = {
  75. template: `#template`,
  76. data() {
  77. return {
  78. count: 0,
  79. }
  80. },
  81. }
  82. createApp(App).mount(container)
  83. expect(container.innerHTML).toBe(`0`)
  84. })
  85. it('should support runtime template via direct DOM node', () => {
  86. const container = document.createElement('div')
  87. const template = document.createElement('div')
  88. template.id = 'template'
  89. template.innerHTML = '{{ count }}'
  90. const App = {
  91. template,
  92. data() {
  93. return {
  94. count: 0,
  95. }
  96. },
  97. }
  98. createApp(App).mount(container)
  99. expect(container.innerHTML).toBe(`0`)
  100. })
  101. it('should warn template compilation errors with codeframe', () => {
  102. const container = document.createElement('div')
  103. const App = {
  104. template: `<div v-if>`,
  105. }
  106. createApp(App).mount(container)
  107. expect(
  108. `Template compilation error: Element is missing end tag`,
  109. ).toHaveBeenWarned()
  110. expect(
  111. `
  112. 1 | <div v-if>
  113. | ^`.trim(),
  114. ).toHaveBeenWarned()
  115. expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
  116. expect(
  117. `
  118. 1 | <div v-if>
  119. | ^^^^`.trim(),
  120. ).toHaveBeenWarned()
  121. })
  122. it('should support custom element via config.isCustomElement (deprecated)', () => {
  123. const app = createApp({
  124. template: '<custom></custom>',
  125. })
  126. const container = document.createElement('div')
  127. app.config.isCustomElement = tag => tag === 'custom'
  128. app.mount(container)
  129. expect(container.innerHTML).toBe('<custom></custom>')
  130. })
  131. it('should support custom element via config.compilerOptions.isCustomElement', () => {
  132. const app = createApp({
  133. template: '<custom></custom>',
  134. })
  135. const container = document.createElement('div')
  136. app.config.compilerOptions.isCustomElement = tag => tag === 'custom'
  137. app.mount(container)
  138. expect(container.innerHTML).toBe('<custom></custom>')
  139. })
  140. it('should support using element innerHTML as template', () => {
  141. const app = createApp({
  142. data: () => ({
  143. msg: 'hello',
  144. }),
  145. })
  146. const container = document.createElement('div')
  147. container.innerHTML = '{{msg}}'
  148. app.mount(container)
  149. expect(container.innerHTML).toBe('hello')
  150. })
  151. it('should support selector of rootContainer', () => {
  152. const container = document.createElement('div')
  153. const origin = document.querySelector
  154. document.querySelector = vi.fn().mockReturnValue(container)
  155. const App = {
  156. template: `{{ count }}`,
  157. data() {
  158. return {
  159. count: 0,
  160. }
  161. },
  162. }
  163. createApp(App).mount('#app')
  164. expect(container.innerHTML).toBe(`0`)
  165. document.querySelector = origin
  166. })
  167. it('should warn when template is not available', () => {
  168. const app = createApp({
  169. template: {},
  170. })
  171. const container = document.createElement('div')
  172. app.mount(container)
  173. expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
  174. })
  175. it('should warn when template is is not found', () => {
  176. const app = createApp({
  177. template: '#not-exist-id',
  178. })
  179. const container = document.createElement('div')
  180. app.mount(container)
  181. expect(
  182. '[Vue warn]: Template element not found or is empty: #not-exist-id',
  183. ).toHaveBeenWarned()
  184. })
  185. it('should warn when container is not found', () => {
  186. const origin = document.querySelector
  187. document.querySelector = vi.fn().mockReturnValue(null)
  188. const App = {
  189. template: `{{ count }}`,
  190. data() {
  191. return {
  192. count: 0,
  193. }
  194. },
  195. }
  196. createApp(App).mount('#not-exist-id')
  197. expect(
  198. '[Vue warn]: Failed to mount app: mount target selector "#not-exist-id" returned null.',
  199. ).toHaveBeenWarned()
  200. document.querySelector = origin
  201. })
  202. // #1813
  203. it('should not report an error when "0" as patchFlag value', async () => {
  204. const container = document.createElement('div')
  205. const target = document.createElement('div')
  206. const count = ref(0)
  207. const origin = document.querySelector
  208. document.querySelector = vi.fn().mockReturnValue(target)
  209. const App = {
  210. template: `
  211. <teleport v-if="count < 2" to="#target">
  212. <div>
  213. <div>{{ count }}</div>
  214. </div>
  215. </teleport>
  216. `,
  217. data() {
  218. return {
  219. count,
  220. }
  221. },
  222. }
  223. createApp(App).mount(container)
  224. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  225. expect(target.innerHTML).toBe(`<div><div>0</div></div>`)
  226. count.value++
  227. await nextTick()
  228. expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  229. expect(target.innerHTML).toBe(`<div><div>1</div></div>`)
  230. count.value++
  231. await nextTick()
  232. expect(container.innerHTML).toBe(`<!--v-if-->`)
  233. expect(target.innerHTML).toBe(``)
  234. document.querySelector = origin
  235. })
  236. test('v-if + v-once', async () => {
  237. const ok = ref(true)
  238. const App = {
  239. setup() {
  240. return { ok }
  241. },
  242. template: `<div>{{ ok }}<div v-if="ok" v-once>{{ ok }}</div></div>`,
  243. }
  244. const container = document.createElement('div')
  245. createApp(App).mount(container)
  246. expect(container.innerHTML).toBe(`<div>true<div>true</div></div>`)
  247. ok.value = false
  248. await nextTick()
  249. expect(container.innerHTML).toBe(`<div>false<div>true</div></div>`)
  250. })
  251. test('v-for + v-once', async () => {
  252. const list = reactive([1])
  253. const App = {
  254. setup() {
  255. return { list }
  256. },
  257. template: `<div>{{ list.length }}<div v-for="i in list" v-once>{{ i }}</div></div>`,
  258. }
  259. const container = document.createElement('div')
  260. createApp(App).mount(container)
  261. expect(container.innerHTML).toBe(`<div>1<div>1</div></div>`)
  262. list.push(2)
  263. await nextTick()
  264. expect(container.innerHTML).toBe(`<div>2<div>1</div></div>`)
  265. })
  266. // #2413
  267. it('EMPTY_ARR should not change', () => {
  268. const App = {
  269. template: `<div v-for="v of ['a']">{{ v }}</div>`,
  270. }
  271. const container = document.createElement('div')
  272. createApp(App).mount(container)
  273. expect(EMPTY_ARR.length).toBe(0)
  274. })
  275. test('BigInt support', () => {
  276. const app = createApp({
  277. template: `<div>{{ BigInt(BigInt(100000111)) + BigInt(2000000000n) * 30000000n }}</div>`,
  278. })
  279. const root = document.createElement('div')
  280. app.mount(root)
  281. expect(root.innerHTML).toBe('<div>60000000100000111</div>')
  282. })
  283. })