index.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { createApp } from '../src'
  2. import { mockWarn } from '@vue/runtime-test'
  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().mount(App, 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().mount(App, 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().mount(App, 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().mount(App, container)
  57. expect(
  58. `Template compilation error: End tag was not found`
  59. ).toHaveBeenWarned()
  60. expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
  61. expect(
  62. `
  63. 1 | <div v-if>
  64. | ^^^^`.trim()
  65. ).toHaveBeenWarned()
  66. })
  67. it('should support custom element', () => {
  68. const app = createApp()
  69. const container = document.createElement('div')
  70. const App = {
  71. template: '<custom></custom>'
  72. }
  73. app.config.isCustomElement = tag => tag === 'custom'
  74. app.mount(App, container)
  75. expect(container.innerHTML).toBe('<custom></custom>')
  76. })
  77. it('should support using element innerHTML as template', () => {
  78. const app = createApp()
  79. const container = document.createElement('div')
  80. container.innerHTML = '{{msg}}'
  81. const App = {
  82. data: {
  83. msg: 'hello'
  84. }
  85. }
  86. app.mount(App, container)
  87. expect(container.innerHTML).toBe('hello')
  88. })
  89. })