index.spec.ts 8.2 KB

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