2020-10-20 06:08:54 +08:00
|
|
|
import { EMPTY_ARR } from '@vue/shared'
|
2020-09-03 00:29:07 +08:00
|
|
|
import { createApp, ref, nextTick, reactive } from '../src'
|
2019-09-20 12:56:02 +08:00
|
|
|
|
2019-10-16 05:50:38 +08:00
|
|
|
describe('compiler + runtime integration', () => {
|
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
|
|
|
|
2020-07-20 01:31:07 +08:00
|
|
|
it('keep-alive with compiler + runtime integration', async () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const one = {
|
|
|
|
name: 'one',
|
|
|
|
template: 'one',
|
|
|
|
created: jest.fn(),
|
|
|
|
mounted: jest.fn(),
|
|
|
|
activated: jest.fn(),
|
|
|
|
deactivated: jest.fn(),
|
2020-09-15 21:36:27 +08:00
|
|
|
unmounted: jest.fn()
|
2020-07-20 01:31:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
template: `
|
|
|
|
<keep-alive>
|
|
|
|
<one v-if="toggle"></one>
|
|
|
|
</keep-alive>
|
|
|
|
`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
toggle
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
One: one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createApp(App).mount(container)
|
|
|
|
expect(container.innerHTML).toBe(`one`)
|
|
|
|
expect(one.created).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.mounted).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.activated).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.deactivated).toHaveBeenCalledTimes(0)
|
2020-09-15 21:36:27 +08:00
|
|
|
expect(one.unmounted).toHaveBeenCalledTimes(0)
|
2020-07-20 01:31:07 +08:00
|
|
|
|
2020-07-28 10:58:37 +08:00
|
|
|
toggle.value = false
|
2020-07-20 01:31:07 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`<!--v-if-->`)
|
|
|
|
expect(one.created).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.mounted).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.activated).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.deactivated).toHaveBeenCalledTimes(1)
|
2020-09-15 21:36:27 +08:00
|
|
|
expect(one.unmounted).toHaveBeenCalledTimes(0)
|
2020-07-20 01:31:07 +08:00
|
|
|
|
2020-07-28 10:58:37 +08:00
|
|
|
toggle.value = true
|
2020-07-20 01:31:07 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`one`)
|
|
|
|
expect(one.created).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.mounted).toHaveBeenCalledTimes(1)
|
|
|
|
expect(one.activated).toHaveBeenCalledTimes(2)
|
|
|
|
expect(one.deactivated).toHaveBeenCalledTimes(1)
|
2020-09-15 21:36:27 +08:00
|
|
|
expect(one.unmounted).toHaveBeenCalledTimes(0)
|
2020-07-20 01:31:07 +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
|
|
|
|
2021-09-27 22:52:59 +08:00
|
|
|
it('should support custom element via config.isCustomElement (deprecated)', () => {
|
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
|
|
|
|
2021-09-27 22:52:59 +08:00
|
|
|
it('should support custom element via config.compilerOptions.isCustomElement', () => {
|
|
|
|
const app = createApp({
|
|
|
|
template: '<custom></custom>'
|
|
|
|
})
|
|
|
|
const container = document.createElement('div')
|
|
|
|
app.config.compilerOptions.isCustomElement = tag => tag === 'custom'
|
|
|
|
app.mount(container)
|
|
|
|
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({
|
2020-03-13 04:13:12 +08:00
|
|
|
data: () => ({
|
2019-10-25 22:18:46 +08:00
|
|
|
msg: 'hello'
|
2020-03-13 04:13:12 +08:00
|
|
|
})
|
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')
|
|
|
|
})
|
2020-06-15 21:29:58 +08:00
|
|
|
|
2020-06-18 04:01:35 +08:00
|
|
|
it('should support selector of rootContainer', () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const origin = document.querySelector
|
|
|
|
document.querySelector = jest.fn().mockReturnValue(container)
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
template: `{{ count }}`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createApp(App).mount('#app')
|
|
|
|
expect(container.innerHTML).toBe(`0`)
|
|
|
|
document.querySelector = origin
|
|
|
|
})
|
|
|
|
|
2020-07-08 18:32:42 +08:00
|
|
|
it('should warn when template is not available', () => {
|
2020-06-15 21:29:58 +08:00
|
|
|
const app = createApp({
|
|
|
|
template: {}
|
|
|
|
})
|
|
|
|
const container = document.createElement('div')
|
|
|
|
app.mount(container)
|
|
|
|
expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn when template is is not found', () => {
|
|
|
|
const app = createApp({
|
|
|
|
template: '#not-exist-id'
|
|
|
|
})
|
|
|
|
const container = document.createElement('div')
|
|
|
|
app.mount(container)
|
|
|
|
expect(
|
|
|
|
'[Vue warn]: Template element not found or is empty: #not-exist-id'
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
2020-06-18 04:01:35 +08:00
|
|
|
|
|
|
|
it('should warn when container is not found', () => {
|
|
|
|
const origin = document.querySelector
|
|
|
|
document.querySelector = jest.fn().mockReturnValue(null)
|
|
|
|
const App = {
|
|
|
|
template: `{{ count }}`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createApp(App).mount('#not-exist-id')
|
|
|
|
|
|
|
|
expect(
|
2020-12-05 07:25:13 +08:00
|
|
|
'[Vue warn]: Failed to mount app: mount target selector "#not-exist-id" returned null.'
|
2020-06-18 04:01:35 +08:00
|
|
|
).toHaveBeenWarned()
|
|
|
|
document.querySelector = origin
|
|
|
|
})
|
2020-08-15 05:26:32 +08:00
|
|
|
|
|
|
|
// #1813
|
|
|
|
it('should not report an error when "0" as patchFlag value', async () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
const target = document.createElement('div')
|
|
|
|
const count = ref(0)
|
|
|
|
const origin = document.querySelector
|
|
|
|
document.querySelector = jest.fn().mockReturnValue(target)
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
template: `
|
|
|
|
<teleport v-if="count < 2" to="#target">
|
|
|
|
<div>
|
|
|
|
<div>{{ count }}</div>
|
|
|
|
</div>
|
|
|
|
</teleport>
|
|
|
|
`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createApp(App).mount(container)
|
|
|
|
expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
|
|
|
|
expect(target.innerHTML).toBe(`<div><div>0</div></div>`)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
|
|
|
|
expect(target.innerHTML).toBe(`<div><div>1</div></div>`)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`<!--v-if-->`)
|
|
|
|
expect(target.innerHTML).toBe(``)
|
|
|
|
|
|
|
|
document.querySelector = origin
|
|
|
|
})
|
2020-09-03 00:29:07 +08:00
|
|
|
|
|
|
|
test('v-if + v-once', async () => {
|
|
|
|
const ok = ref(true)
|
|
|
|
const App = {
|
|
|
|
setup() {
|
|
|
|
return { ok }
|
|
|
|
},
|
|
|
|
template: `<div>{{ ok }}<div v-if="ok" v-once>{{ ok }}</div></div>`
|
|
|
|
}
|
|
|
|
const container = document.createElement('div')
|
|
|
|
createApp(App).mount(container)
|
|
|
|
|
|
|
|
expect(container.innerHTML).toBe(`<div>true<div>true</div></div>`)
|
|
|
|
ok.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`<div>false<div>true</div></div>`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-for + v-once', async () => {
|
|
|
|
const list = reactive([1])
|
|
|
|
const App = {
|
|
|
|
setup() {
|
|
|
|
return { list }
|
|
|
|
},
|
|
|
|
template: `<div>{{ list.length }}<div v-for="i in list" v-once>{{ i }}</div></div>`
|
|
|
|
}
|
|
|
|
const container = document.createElement('div')
|
|
|
|
createApp(App).mount(container)
|
|
|
|
|
|
|
|
expect(container.innerHTML).toBe(`<div>1<div>1</div></div>`)
|
|
|
|
list.push(2)
|
|
|
|
await nextTick()
|
|
|
|
expect(container.innerHTML).toBe(`<div>2<div>1</div></div>`)
|
|
|
|
})
|
2020-10-20 06:08:54 +08:00
|
|
|
|
|
|
|
// #2413
|
|
|
|
it('EMPTY_ARR should not change', () => {
|
|
|
|
const App = {
|
|
|
|
template: `<div v-for="v of ['a']">{{ v }}</div>`
|
|
|
|
}
|
|
|
|
const container = document.createElement('div')
|
|
|
|
createApp(App).mount(container)
|
|
|
|
expect(EMPTY_ARR.length).toBe(0)
|
|
|
|
})
|
2021-02-04 02:28:46 +08:00
|
|
|
|
|
|
|
test('BigInt support', () => {
|
|
|
|
const app = createApp({
|
|
|
|
template: `<div>{{ BigInt(BigInt(100000111)) + BigInt(2000000000n) * 30000000n }}</div>`
|
|
|
|
})
|
|
|
|
const root = document.createElement('div')
|
|
|
|
app.mount(root)
|
|
|
|
expect(root.innerHTML).toBe('<div>60000000100000111</div>')
|
|
|
|
})
|
2019-10-16 05:30:47 +08:00
|
|
|
})
|