2019-09-20 12:56:02 +08:00
|
|
|
import { createApp } from '../src'
|
2020-01-22 22:29:35 +08:00
|
|
|
import { mockWarn } from '@vue/shared'
|
2019-09-20 12:56:02 +08:00
|
|
|
|
2019-10-16 05:50:38 +08:00
|
|
|
describe('compiler + runtime integration', () => {
|
|
|
|
mockWarn()
|
|
|
|
|
2019-12-11 22:46:42 +08:00
|
|
|
it('should support runtime template compilation', () => {
|
2019-10-16 05:50:38 +08:00
|
|
|
const container = document.createElement('div')
|
|
|
|
const App = {
|
|
|
|
template: `{{ count }}`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
}
|
2019-09-20 12:56:02 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
createApp(App).mount(container)
|
2019-10-16 05:50:38 +08:00
|
|
|
expect(container.innerHTML).toBe(`0`)
|
|
|
|
})
|
2019-10-12 03:09:37 +08:00
|
|
|
|
2019-12-11 22:46:42 +08:00
|
|
|
it('should support runtime template via CSS ID selector', () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const template = document.createElement('div')
|
|
|
|
template.id = 'template'
|
|
|
|
template.innerHTML = '{{ count }}'
|
|
|
|
document.body.appendChild(template)
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
template: `#template`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
createApp(App).mount(container)
|
2019-12-11 22:46:42 +08:00
|
|
|
expect(container.innerHTML).toBe(`0`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support runtime template via direct DOM node', () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const template = document.createElement('div')
|
|
|
|
template.id = 'template'
|
|
|
|
template.innerHTML = '{{ count }}'
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
template,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
createApp(App).mount(container)
|
2019-12-11 22:46:42 +08:00
|
|
|
expect(container.innerHTML).toBe(`0`)
|
|
|
|
})
|
|
|
|
|
2019-10-16 05:50:38 +08:00
|
|
|
it('should warn template compilation errors with codeframe', () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const App = {
|
|
|
|
template: `<div v-if>`
|
2019-10-12 03:09:37 +08:00
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
createApp(App).mount(container)
|
2019-10-16 05:50:38 +08:00
|
|
|
expect(
|
2019-12-20 23:10:08 +08:00
|
|
|
`Template compilation error: Element is missing end tag`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
expect(
|
|
|
|
`
|
|
|
|
1 | <div v-if>
|
|
|
|
| ^`.trim()
|
2019-10-16 05:50:38 +08:00
|
|
|
).toHaveBeenWarned()
|
|
|
|
expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
|
|
|
|
expect(
|
|
|
|
`
|
|
|
|
1 | <div v-if>
|
|
|
|
| ^^^^`.trim()
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
2019-10-16 05:30:47 +08:00
|
|
|
|
2019-10-16 05:50:38 +08:00
|
|
|
it('should support custom element', () => {
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp({
|
2019-10-16 05:50:38 +08:00
|
|
|
template: '<custom></custom>'
|
2020-01-24 04:05:38 +08:00
|
|
|
})
|
|
|
|
const container = document.createElement('div')
|
2019-10-16 05:50:38 +08:00
|
|
|
app.config.isCustomElement = tag => tag === 'custom'
|
2020-01-24 04:05:38 +08:00
|
|
|
app.mount(container)
|
2019-10-16 05:50:38 +08:00
|
|
|
expect(container.innerHTML).toBe('<custom></custom>')
|
|
|
|
})
|
2019-10-25 22:18:46 +08:00
|
|
|
|
|
|
|
it('should support using element innerHTML as template', () => {
|
2020-01-24 04:05:38 +08:00
|
|
|
const app = createApp({
|
2019-10-25 22:18:46 +08:00
|
|
|
data: {
|
|
|
|
msg: 'hello'
|
|
|
|
}
|
2020-01-24 04:05:38 +08:00
|
|
|
})
|
|
|
|
const container = document.createElement('div')
|
|
|
|
container.innerHTML = '{{msg}}'
|
|
|
|
app.mount(container)
|
2019-10-25 22:18:46 +08:00
|
|
|
expect(container.innerHTML).toBe('hello')
|
|
|
|
})
|
2019-10-16 05:30:47 +08:00
|
|
|
})
|