index.spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. })